|
4 | 4 |
|
5 | 5 | package oracle.kubernetes.operator.helpers;
|
6 | 6 |
|
| 7 | +import com.squareup.okhttp.Dispatcher; |
7 | 8 | import io.kubernetes.client.ApiClient;
|
8 | 9 | import io.kubernetes.client.Configuration;
|
9 | 10 | import io.kubernetes.client.util.Config;
|
10 | 11 | import java.io.IOException;
|
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.SynchronousQueue; |
| 14 | +import java.util.concurrent.ThreadFactory; |
| 15 | +import java.util.concurrent.ThreadPoolExecutor; |
| 16 | +import java.util.concurrent.TimeUnit; |
11 | 17 | import java.util.concurrent.atomic.AtomicBoolean;
|
12 | 18 | import oracle.kubernetes.operator.logging.LoggingFacade;
|
13 | 19 | import oracle.kubernetes.operator.logging.LoggingFactory;
|
|
18 | 24 | public class ClientPool extends Pool<ApiClient> {
|
19 | 25 | private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
|
20 | 26 | private static ClientPool SINGLETON = new ClientPool();
|
| 27 | + private static ThreadFactory threadFactory; |
21 | 28 |
|
22 | 29 | private static final ClientFactory FACTORY = new DefaultClientFactory();
|
23 | 30 |
|
| 31 | + public static void initialize(ThreadFactory threadFactory) { |
| 32 | + ClientPool.threadFactory = |
| 33 | + (r) -> { |
| 34 | + return threadFactory.newThread( |
| 35 | + () -> { |
| 36 | + try { |
| 37 | + r.run(); |
| 38 | + } catch (Throwable t) { |
| 39 | + // These will almost always be spurious exceptions |
| 40 | + LOGGER.fine(MessageKeys.EXCEPTION, t); |
| 41 | + } |
| 42 | + }); |
| 43 | + }; |
| 44 | + } |
| 45 | + |
24 | 46 | public static ClientPool getInstance() {
|
25 | 47 | return SINGLETON;
|
26 | 48 | }
|
@@ -71,6 +93,19 @@ public ApiClient get() {
|
71 | 93 | if (first.getAndSet(false)) {
|
72 | 94 | Configuration.setDefaultApiClient(client);
|
73 | 95 | }
|
| 96 | + |
| 97 | + if (threadFactory != null) { |
| 98 | + ExecutorService exec = |
| 99 | + new ThreadPoolExecutor( |
| 100 | + 0, |
| 101 | + Integer.MAX_VALUE, |
| 102 | + 60, |
| 103 | + TimeUnit.SECONDS, |
| 104 | + new SynchronousQueue<Runnable>(), |
| 105 | + threadFactory); |
| 106 | + client.getHttpClient().setDispatcher(new Dispatcher(exec)); |
| 107 | + } |
| 108 | + |
74 | 109 | return client;
|
75 | 110 | } catch (IOException e) {
|
76 | 111 | throw new RuntimeException(e);
|
|
0 commit comments