|
22 | 22 | import io.rsocket.RSocket; |
23 | 23 | import io.rsocket.transport.ClientTransport; |
24 | 24 | import io.rsocket.util.ByteBufPayload; |
| 25 | +import org.reactivestreams.Publisher; |
25 | 26 | import org.slf4j.Logger; |
26 | 27 | import org.slf4j.LoggerFactory; |
27 | 28 | import reactor.core.Disposable; |
| 29 | +import reactor.core.publisher.Mono; |
28 | 30 | import reactor.core.publisher.MonoProcessor; |
29 | | -import reactor.retry.Retry; |
30 | 31 |
|
31 | 32 | import java.net.InetSocketAddress; |
32 | 33 | import java.net.SocketAddress; |
33 | 34 | import java.time.Duration; |
34 | 35 | import java.util.*; |
| 36 | +import java.util.concurrent.atomic.AtomicLong; |
35 | 37 | import java.util.function.Function; |
36 | | -import java.util.function.Supplier; |
37 | 38 |
|
38 | 39 | public class DefaultProteusBrokerService implements ProteusBrokerService, Disposable { |
39 | 40 | private static final Logger logger = LoggerFactory.getLogger(DefaultProteusBrokerService.class); |
@@ -117,14 +118,43 @@ public DefaultProteusBrokerService( |
117 | 118 |
|
118 | 119 | this.client = new BrokerInfoServiceClient(unwrappedGroup("com.netifi.proteus.brokerServices")); |
119 | 120 | this.presenceNotifier = new BrokerInfoPresenceNotifier(client); |
120 | | - |
| 121 | + |
121 | 122 | Disposable disposable = |
122 | 123 | client |
123 | 124 | .streamBrokerEvents(Empty.getDefaultInstance()) |
124 | 125 | .doOnNext(this::handleBrokerEvent) |
125 | 126 | .filter(event -> event.getType() == Event.Type.JOIN) |
126 | 127 | .doOnNext(event -> createConnection()) |
127 | | - .doOnError(t -> logger.error("error streaming broker events", t)) |
| 128 | + .doOnError( |
| 129 | + t -> { |
| 130 | + logger.warn( |
| 131 | + "error streaming broker events - make sure access key {} has a valid access token", |
| 132 | + accessKey); |
| 133 | + logger.trace("error streaming broker events", t); |
| 134 | + }) |
| 135 | + .onErrorResume( |
| 136 | + new Function<Throwable, Publisher<? extends Event>>() { |
| 137 | + long attempts = 0; |
| 138 | + long lastAttempt = System.currentTimeMillis(); |
| 139 | + |
| 140 | + @Override |
| 141 | + public synchronized Publisher<? extends Event> apply(Throwable throwable) { |
| 142 | + if (Duration.ofMillis(System.currentTimeMillis() - lastAttempt).getSeconds() |
| 143 | + > 30) { |
| 144 | + attempts = 0; |
| 145 | + } |
| 146 | + |
| 147 | + Mono<Event> then = |
| 148 | + Mono.delay(Duration.ofMillis(attempts * 500)).then(Mono.error(throwable)); |
| 149 | + if (attempts < 30) { |
| 150 | + attempts++; |
| 151 | + } |
| 152 | + |
| 153 | + lastAttempt = System.currentTimeMillis(); |
| 154 | + |
| 155 | + return then; |
| 156 | + } |
| 157 | + }) |
128 | 158 | .retry() |
129 | 159 | .subscribe(); |
130 | 160 |
|
@@ -278,7 +308,7 @@ private ProteusSocket unwrappedGroup(String group) { |
278 | 308 | }, |
279 | 309 | this::selectRSocket); |
280 | 310 | } |
281 | | - |
| 311 | + |
282 | 312 | private ProteusSocket unwrappedBroadcast(String group) { |
283 | 313 | return new DefaultProteusSocket( |
284 | 314 | payload -> { |
@@ -308,12 +338,12 @@ public ProteusSocket destination(String destination, String group) { |
308 | 338 | public ProteusSocket group(String group) { |
309 | 339 | return PresenceAwareRSocket.wrap(unwrappedGroup(group), null, group, presenceNotifier); |
310 | 340 | } |
311 | | - |
| 341 | + |
312 | 342 | @Override |
313 | 343 | public ProteusSocket broadcast(String group) { |
314 | 344 | return PresenceAwareRSocket.wrap(unwrappedBroadcast(group), null, group, presenceNotifier); |
315 | 345 | } |
316 | | - |
| 346 | + |
317 | 347 | @Override |
318 | 348 | public void dispose() { |
319 | 349 | onClose.onComplete(); |
@@ -445,7 +475,7 @@ private WeightedClientTransportSupplier selectClientTransportSupplier() { |
445 | 475 | } |
446 | 476 | } |
447 | 477 | } |
448 | | - |
| 478 | + |
449 | 479 | logger.info("selecting socket {} with weight {}", supplier.toString(), supplier.weight()); |
450 | 480 | if (logger.isDebugEnabled()) { |
451 | 481 | logger.debug("selecting socket {} with weight {}", supplier.toString(), supplier.weight()); |
|
0 commit comments