31
31
import java .util .concurrent .ExecutorService ;
32
32
import java .util .concurrent .Executors ;
33
33
import java .util .concurrent .Future ;
34
- import java .util .concurrent .TimeUnit ;
35
34
import okhttp3 .Call ;
36
- import okhttp3 .OkHttpClient ;
37
35
import org .apache .commons .collections4 .MapUtils ;
38
36
39
37
/** SharedInformerFactory class constructs and caches informers for api types. */
@@ -49,7 +47,7 @@ public class SharedInformerFactory {
49
47
50
48
/** Constructor w/ default thread pool. */
51
49
public SharedInformerFactory () {
52
- this (Configuration .getDefaultApiClient (), Executors .newCachedThreadPool ());
50
+ this (Configuration .getDefaultApiClient (). setReadTimeout ( 0 ) , Executors .newCachedThreadPool ());
53
51
}
54
52
55
53
/** Constructor w/ api client specified and default thread pool. */
@@ -63,7 +61,7 @@ public SharedInformerFactory(ApiClient apiClient) {
63
61
* @param threadPool specified thread pool
64
62
*/
65
63
public SharedInformerFactory (ExecutorService threadPool ) {
66
- this (Configuration .getDefaultApiClient (), threadPool );
64
+ this (Configuration .getDefaultApiClient (). setReadTimeout ( 0 ) , threadPool );
67
65
}
68
66
69
67
/**
@@ -73,6 +71,10 @@ public SharedInformerFactory(ExecutorService threadPool) {
73
71
* @param threadPool specified thread pool
74
72
*/
75
73
public SharedInformerFactory (ApiClient client , ExecutorService threadPool ) {
74
+ if (client .getReadTimeout () != 0 ) {
75
+ throw new IllegalArgumentException ("read timeout of ApiClient must be zero" );
76
+ }
77
+
76
78
apiClient = client ;
77
79
informerExecutor = threadPool ;
78
80
informers = new HashMap <>();
@@ -169,11 +171,9 @@ ListerWatcher<ApiType, ApiListType> listerWatcherFor(
169
171
CallGenerator callGenerator ,
170
172
Class <ApiType > apiTypeClass ,
171
173
Class <ApiListType > apiListTypeClass ) {
172
- if (apiClient .getHttpClient (). readTimeoutMillis () > 0 ) {
174
+ if (apiClient .getReadTimeout () > 0 ) {
173
175
// set read timeout zero to ensure client doesn't time out
174
- OkHttpClient httpClient =
175
- apiClient .getHttpClient ().newBuilder ().readTimeout (0 , TimeUnit .MILLISECONDS ).build ();
176
- apiClient .setHttpClient (httpClient );
176
+ apiClient .setReadTimeout (0 );
177
177
}
178
178
return new ListerWatcher <ApiType , ApiListType >() {
179
179
@ Override
@@ -185,6 +185,8 @@ public ApiListType list(CallGeneratorParams params) throws ApiException {
185
185
@ Override
186
186
public Watch <ApiType > watch (CallGeneratorParams params ) throws ApiException {
187
187
Call call = callGenerator .generate (params );
188
+ // bind call with private http client to make sure read timeout is zero.
189
+ call = apiClient .getHttpClient ().newCall (call .request ());
188
190
return Watch .createWatch (
189
191
apiClient ,
190
192
call ,
@@ -196,12 +198,11 @@ public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
196
198
private <ApiType extends KubernetesObject , ApiListType extends KubernetesListObject >
197
199
ListerWatcher <ApiType , ApiListType > listerWatcherFor (
198
200
GenericKubernetesApi <ApiType , ApiListType > genericKubernetesApi ) {
199
- if (apiClient .getHttpClient (). readTimeoutMillis () > 0 ) {
201
+ if (apiClient .getReadTimeout () > 0 ) {
200
202
// set read timeout zero to ensure client doesn't time out
201
- OkHttpClient httpClient =
202
- apiClient .getHttpClient ().newBuilder ().readTimeout (0 , TimeUnit .MILLISECONDS ).build ();
203
- apiClient .setHttpClient (httpClient );
203
+ apiClient .setReadTimeout (0 );
204
204
}
205
+ // TODO: it seems read timeout is determined by genericKubernetesApi instead of above apiClient.
205
206
return new ListerWatcher <ApiType , ApiListType >() {
206
207
public ApiListType list (CallGeneratorParams params ) throws ApiException {
207
208
return genericKubernetesApi
0 commit comments