@@ -65,11 +65,39 @@ public class PrometheusRSocketClient {
65
65
private volatile boolean requestedDisconnect = false ;
66
66
private RSocket sendingSocket ;
67
67
68
+ private Duration timeout ;
69
+
70
+ /**
71
+ * Creates a {@link PrometheusRSocketClient}.
72
+ *
73
+ * @param registryAndScrape the registry and scrape meter
74
+ * @param transport the client transport
75
+ * @param retry the retry configuration
76
+ * @param onKeyReceived the callback if a key has been received
77
+ */
78
+ private PrometheusRSocketClient (MeterRegistryAndScrape <?> registryAndScrape ,
79
+ ClientTransport transport ,
80
+ Retry retry ,
81
+ Runnable onKeyReceived ) {
82
+ this (registryAndScrape , transport , retry , Duration .ofSeconds (5 ), onKeyReceived );
83
+ }
84
+
85
+ /**
86
+ * Creates a {@link PrometheusRSocketClient}.
87
+ *
88
+ * @param registryAndScrape the registry and scrape meter
89
+ * @param transport the client transport
90
+ * @param retry the retry configuration
91
+ * @param timeout the timeout to connect and push the data
92
+ * @param onKeyReceived the callback if a key has been received
93
+ */
68
94
private PrometheusRSocketClient (MeterRegistryAndScrape <?> registryAndScrape ,
69
95
ClientTransport transport ,
70
96
Retry retry ,
97
+ Duration timeout ,
71
98
Runnable onKeyReceived ) {
72
99
this .registryAndScrape = registryAndScrape ;
100
+ this .timeout = timeout ;
73
101
74
102
RSocketConnector .create ()
75
103
.reconnect (new Retry () {
@@ -116,8 +144,8 @@ public Mono<Void> fireAndForget(Payload payload) {
116
144
.increment ())
117
145
.doOnNext (connection -> this .connection = connection )
118
146
.flatMap (socket -> socket .onClose ()
119
- .map (v -> 1 ) // https://github.com/rsocket/rsocket-java/issues/819
120
- .onErrorReturn (1 ))
147
+ .map (v -> 1 ) // https://github.com/rsocket/rsocket-java/issues/819
148
+ .onErrorReturn (1 ))
121
149
.repeat (() -> !requestedDisconnect )
122
150
.subscribe ();
123
151
}
@@ -149,6 +177,9 @@ public static <M extends MeterRegistry> Builder build(M meterRegistry, Supplier<
149
177
return new Builder (meterRegistry , scrape , clientTransport );
150
178
}
151
179
180
+ /**
181
+ * Closes the {@link PrometheusRSocketClient}
182
+ */
152
183
public void close () {
153
184
this .requestedDisconnect = true ;
154
185
if (this .connection != null ) {
@@ -160,11 +191,12 @@ public void close() {
160
191
* Pushes the data in a blocking way and closes the connection.
161
192
*/
162
193
public void pushAndCloseBlockingly () {
163
- pushAndCloseBlockingly (Duration . ofSeconds ( 5 ) );
194
+ pushAndCloseBlockingly (timeout );
164
195
}
165
196
166
197
/**
167
198
* Pushes the data in a blocking way and closes the connection.
199
+ *
168
200
* @param timeout the amount of time to wait for the data to be sent
169
201
*/
170
202
public void pushAndCloseBlockingly (Duration timeout ) {
@@ -183,8 +215,7 @@ public void pushAndCloseBlockingly(Duration timeout) {
183
215
.doOnCancel (() -> LOGGER .warn ("Pushing data to RSocket Proxy before closing the connection was cancelled!" ))
184
216
.doFinally (signalType -> latch .countDown ())
185
217
.subscribe ();
186
- }
187
- catch (Exception exception ) {
218
+ } catch (Exception exception ) {
188
219
latch .countDown ();
189
220
LOGGER .warn ("Sending the payload failed!" , exception );
190
221
}
@@ -193,8 +224,7 @@ public void pushAndCloseBlockingly(Duration timeout) {
193
224
if (!latch .await (timeout .toMillis (), MILLISECONDS )) {
194
225
LOGGER .warn ("Sending the payload timed out!" );
195
226
}
196
- }
197
- catch (InterruptedException exception ) {
227
+ } catch (InterruptedException exception ) {
198
228
LOGGER .warn ("Waiting for sending the payload was interrupted!" , exception );
199
229
}
200
230
}
@@ -215,8 +245,7 @@ public void pushAndClose() {
215
245
.doOnError (throwable -> LOGGER .warn ("Pushing data to RSocket Proxy before closing the connection failed!" , throwable ))
216
246
.doOnCancel (() -> LOGGER .warn ("Pushing data to RSocket Proxy before closing the connection was cancelled!" ))
217
247
.subscribe ();
218
- }
219
- catch (Exception exception ) {
248
+ } catch (Exception exception ) {
220
249
LOGGER .warn ("Sending the payload failed!" , exception );
221
250
}
222
251
}
@@ -263,54 +292,111 @@ private PublicKey decodePublicKey(ByteBuffer encodedKeyBuffer) {
263
292
}
264
293
}
265
294
295
+ /**
296
+ * Builder class to create a {@link PrometheusRSocketClient}.
297
+ */
266
298
public static class Builder {
267
299
private MeterRegistryAndScrape <?> registryAndScrape ;
268
300
private final ClientTransport clientTransport ;
269
301
270
302
private Retry retry = Retry .backoff (Long .MAX_VALUE , Duration .ofSeconds (10 ))
271
303
.maxBackoff (Duration .ofMinutes (10 ));
272
304
273
- private Runnable onKeyReceived = () -> { };
305
+ private Duration timeout = Duration .ofSeconds (5 );
306
+
307
+ private Runnable onKeyReceived = () -> {
308
+ };
274
309
275
310
<M extends MeterRegistry > Builder (M registry , Supplier <String > scrape , ClientTransport clientTransport ) {
276
311
this .registryAndScrape = new MeterRegistryAndScrape <>(registry , scrape );
277
312
this .clientTransport = clientTransport ;
278
313
}
279
314
315
+ /**
316
+ * Configures the retry for {@link PrometheusRSocketClient}.
317
+ *
318
+ * @param retry the retry configuration
319
+ * @return the {@link Builder}
320
+ */
280
321
public Builder retry (Retry retry ) {
281
322
this .retry = retry ;
282
323
return this ;
283
324
}
284
325
326
+ /**
327
+ * Timeout for the {@link PrometheusRSocketClient}.
328
+ *
329
+ * @param timeout the timeout in seconds
330
+ * @return the {@link Builder}
331
+ */
332
+ public Builder timeout (Duration timeout ) {
333
+ this .timeout = timeout ;
334
+ return this ;
335
+ }
336
+
337
+ /**
338
+ * Callback of {@link PrometheusRSocketClient} if a key is received.
339
+ *
340
+ * @param onKeyReceived callback which is executed if a key is received
341
+ * @return the {@link Builder}
342
+ */
285
343
public Builder doOnKeyReceived (Runnable onKeyReceived ) {
286
344
this .onKeyReceived = onKeyReceived ;
287
345
return this ;
288
346
}
289
347
348
+ /**
349
+ * Connects the {@link PrometheusRSocketClient}.
350
+ *
351
+ * @return the {@link PrometheusRSocketClient}
352
+ */
290
353
public PrometheusRSocketClient connect () {
354
+ return connect (timeout );
355
+ }
356
+
357
+ /**
358
+ * Connects the {@link PrometheusRSocketClient}.
359
+ *
360
+ * @param timeout the timeout for the client to connect
361
+ *
362
+ * @return the {@link PrometheusRSocketClient}
363
+ */
364
+ public PrometheusRSocketClient connect (Duration timeout ) {
291
365
LOGGER .debug ("Connecting to RSocket Proxy..." );
292
366
return new PrometheusRSocketClient (
293
367
registryAndScrape ,
294
368
clientTransport ,
295
369
retry ,
370
+ timeout ,
296
371
() -> {
297
372
LOGGER .info ("Connected to RSocket Proxy!" );
298
373
onKeyReceived .run ();
299
374
}
300
375
);
301
376
}
302
377
378
+ /**
379
+ * Connects the {@link PrometheusRSocketClient} blockingly.
380
+ *
381
+ * @return the {@link PrometheusRSocketClient}
382
+ */
303
383
public PrometheusRSocketClient connectBlockingly () {
304
- return connectBlockingly (Duration . ofSeconds ( 5 ) );
384
+ return connectBlockingly (timeout );
305
385
}
306
386
387
+ /**
388
+ * Connects the {@link PrometheusRSocketClient} blockingly with the given timeout.
389
+ *
390
+ * @return the {@link PrometheusRSocketClient}
391
+ */
307
392
public PrometheusRSocketClient connectBlockingly (Duration timeout ) {
308
393
LOGGER .debug ("Connecting to RSocket Proxy..." );
309
394
CountDownLatch latch = new CountDownLatch (1 );
310
395
PrometheusRSocketClient client = new PrometheusRSocketClient (
311
396
registryAndScrape ,
312
397
clientTransport ,
313
398
retry ,
399
+ timeout ,
314
400
() -> {
315
401
LOGGER .info ("Connected to RSocket Proxy!" );
316
402
onKeyReceived .run ();
@@ -322,8 +408,7 @@ public PrometheusRSocketClient connectBlockingly(Duration timeout) {
322
408
if (!latch .await (timeout .toMillis (), MILLISECONDS )) {
323
409
LOGGER .warn ("Creating the connection and receiving the key timed out!" );
324
410
}
325
- }
326
- catch (InterruptedException exception ) {
411
+ } catch (InterruptedException exception ) {
327
412
LOGGER .warn ("Waiting for receiving the key was interrupted!" , exception );
328
413
}
329
414
0 commit comments