|
21 | 21 | */
|
22 | 22 |
|
23 | 23 | import com.devicehive.api.RequestResponseMatcher;
|
| 24 | +import com.devicehive.model.ServerEvent; |
24 | 25 | import com.devicehive.proxy.FrontendProxyClient;
|
25 | 26 | import com.devicehive.proxy.ProxyResponseHandler;
|
26 | 27 | import com.devicehive.proxy.api.NotificationHandler;
|
27 | 28 | import com.devicehive.proxy.client.WebSocketKafkaProxyClient;
|
28 |
| -import com.devicehive.proxy.config.WebSocketKafkaProxyConfig; |
29 | 29 | import com.devicehive.shim.api.client.RpcClient;
|
30 | 30 | import com.google.gson.Gson;
|
| 31 | +import com.lmax.disruptor.*; |
31 | 32 | import org.springframework.beans.factory.annotation.Value;
|
32 | 33 | import org.springframework.context.annotation.Bean;
|
33 | 34 | import org.springframework.context.annotation.ComponentScan;
|
|
42 | 43 | import java.util.Base64;
|
43 | 44 | import java.util.Optional;
|
44 | 45 | import java.util.UUID;
|
| 46 | +import java.util.concurrent.ExecutorService; |
| 47 | +import java.util.concurrent.Executors; |
| 48 | +import java.util.stream.IntStream; |
45 | 49 |
|
46 | 50 | import static com.devicehive.configuration.Constants.REQUEST_TOPIC;
|
47 | 51 |
|
@@ -80,16 +84,54 @@ public RequestResponseMatcher requestResponseMatcher() {
|
80 | 84 | }
|
81 | 85 |
|
82 | 86 | @Bean
|
83 |
| - public NotificationHandler notificationHandler(Gson gson, RequestResponseMatcher requestResponseMatcher) { |
84 |
| - return new ProxyResponseHandler(gson, requestResponseMatcher); |
| 87 | + public NotificationHandler notificationHandler(Gson gson, RequestResponseMatcher requestResponseMatcher, WebSocketKafkaProxyConfig proxyConfig) { |
| 88 | + return new ProxyResponseHandler(gson, REQUEST_TOPIC, RESPONSE_TOPIC, proxyConfig, requestResponseMatcher); |
85 | 89 | }
|
86 | 90 |
|
87 | 91 | @Bean
|
88 |
| - public RpcClient rpcClient(NotificationHandler notificationHandler, WebSocketKafkaProxyConfig proxyConfig, RequestResponseMatcher requestResponseMatcher, Gson gson) { |
| 92 | + public WorkerPool<ServerEvent> workerPool(Gson gson, RequestResponseMatcher requestResponseMatcher, WebSocketKafkaProxyConfig proxyConfig) { |
| 93 | + final ProxyResponseHandler[] workHandlers = new ProxyResponseHandler[proxyConfig.getWorkerThreads()]; |
| 94 | + IntStream.range(0, proxyConfig.getWorkerThreads()).forEach( |
| 95 | + nbr -> workHandlers[nbr] = new ProxyResponseHandler(gson, REQUEST_TOPIC, RESPONSE_TOPIC, proxyConfig, requestResponseMatcher) |
| 96 | + ); |
| 97 | + final RingBuffer<ServerEvent> ringBuffer = RingBuffer.createMultiProducer(ServerEvent::new, proxyConfig.getBufferSize(), getWaitStrategy(proxyConfig.getWaitStrategy())); |
| 98 | + final SequenceBarrier barrier = ringBuffer.newBarrier(); |
| 99 | + WorkerPool<ServerEvent> workerPool = new WorkerPool<>(ringBuffer, barrier, new FatalExceptionHandler(), workHandlers); |
| 100 | + ringBuffer.addGatingSequences(workerPool.getWorkerSequences()); |
| 101 | + return workerPool; |
| 102 | + } |
| 103 | + |
| 104 | + @Bean |
| 105 | + public RpcClient rpcClient(NotificationHandler notificationHandler, WebSocketKafkaProxyConfig proxyConfig, RequestResponseMatcher requestResponseMatcher, Gson gson, WorkerPool<ServerEvent> workerPool) { |
89 | 106 | WebSocketKafkaProxyClient proxyClient = new WebSocketKafkaProxyClient(notificationHandler);
|
90 | 107 | proxyClient.setWebSocketKafkaProxyConfig(proxyConfig);
|
91 |
| - RpcClient client = new FrontendProxyClient(REQUEST_TOPIC, RESPONSE_TOPIC, proxyClient, requestResponseMatcher, gson); |
| 108 | + final ExecutorService execService = Executors.newFixedThreadPool(proxyConfig.getWorkerThreads()); |
| 109 | + RingBuffer<ServerEvent> ringBuffer = workerPool.start(execService); |
| 110 | + RpcClient client = new FrontendProxyClient(REQUEST_TOPIC, RESPONSE_TOPIC, proxyClient, requestResponseMatcher, gson, ringBuffer); |
92 | 111 | client.start();
|
93 | 112 | return client;
|
94 | 113 | }
|
| 114 | + |
| 115 | + private WaitStrategy getWaitStrategy(String strategy) { |
| 116 | + WaitStrategy waitStrategy; |
| 117 | + |
| 118 | + switch (strategy) { |
| 119 | + case "blocking": |
| 120 | + waitStrategy = new BlockingWaitStrategy(); |
| 121 | + break; |
| 122 | + case "sleeping": |
| 123 | + waitStrategy = new SleepingWaitStrategy(); |
| 124 | + break; |
| 125 | + case "yielding": |
| 126 | + waitStrategy = new YieldingWaitStrategy(); |
| 127 | + break; |
| 128 | + case "busyspin": |
| 129 | + waitStrategy = new BusySpinWaitStrategy(); |
| 130 | + break; |
| 131 | + default: |
| 132 | + waitStrategy = new BlockingWaitStrategy(); |
| 133 | + break; |
| 134 | + } |
| 135 | + return waitStrategy; |
| 136 | + } |
95 | 137 | }
|
0 commit comments