Skip to content

Commit 94edea8

Browse files
committed
fix: timeout use class variable instead of static
1 parent 3f9ea44 commit 94edea8

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

client/src/main/java/io/micrometer/prometheus/rsocket/PrometheusRSocketClient.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,36 @@ public class PrometheusRSocketClient {
6565
private volatile boolean requestedDisconnect = false;
6666
private RSocket sendingSocket;
6767

68-
private static Duration timeout = Duration.ofSeconds(5);
68+
private Duration timeout = Duration.ofSeconds(5);
6969

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+
*/
7094
private PrometheusRSocketClient(MeterRegistryAndScrape<?> registryAndScrape,
7195
ClientTransport transport,
7296
Retry retry,
97+
Duration timeout,
7398
Runnable onKeyReceived) {
7499
this.registryAndScrape = registryAndScrape;
75100

@@ -118,8 +143,8 @@ public Mono<Void> fireAndForget(Payload payload) {
118143
.increment())
119144
.doOnNext(connection -> this.connection = connection)
120145
.flatMap(socket -> socket.onClose()
121-
.map(v -> 1) // https://github.com/rsocket/rsocket-java/issues/819
122-
.onErrorReturn(1))
146+
.map(v -> 1) // https://github.com/rsocket/rsocket-java/issues/819
147+
.onErrorReturn(1))
123148
.repeat(() -> !requestedDisconnect)
124149
.subscribe();
125150
}
@@ -170,6 +195,7 @@ public void pushAndCloseBlockingly() {
170195

171196
/**
172197
* Pushes the data in a blocking way and closes the connection.
198+
*
173199
* @param timeout the amount of time to wait for the data to be sent
174200
*/
175201
public void pushAndCloseBlockingly(Duration timeout) {
@@ -188,8 +214,7 @@ public void pushAndCloseBlockingly(Duration timeout) {
188214
.doOnCancel(() -> LOGGER.warn("Pushing data to RSocket Proxy before closing the connection was cancelled!"))
189215
.doFinally(signalType -> latch.countDown())
190216
.subscribe();
191-
}
192-
catch (Exception exception) {
217+
} catch (Exception exception) {
193218
latch.countDown();
194219
LOGGER.warn("Sending the payload failed!", exception);
195220
}
@@ -198,8 +223,7 @@ public void pushAndCloseBlockingly(Duration timeout) {
198223
if (!latch.await(timeout.toMillis(), MILLISECONDS)) {
199224
LOGGER.warn("Sending the payload timed out!");
200225
}
201-
}
202-
catch (InterruptedException exception) {
226+
} catch (InterruptedException exception) {
203227
LOGGER.warn("Waiting for sending the payload was interrupted!", exception);
204228
}
205229
}
@@ -220,8 +244,7 @@ public void pushAndClose() {
220244
.doOnError(throwable -> LOGGER.warn("Pushing data to RSocket Proxy before closing the connection failed!", throwable))
221245
.doOnCancel(() -> LOGGER.warn("Pushing data to RSocket Proxy before closing the connection was cancelled!"))
222246
.subscribe();
223-
}
224-
catch (Exception exception) {
247+
} catch (Exception exception) {
225248
LOGGER.warn("Sending the payload failed!", exception);
226249
}
227250
}
@@ -278,8 +301,10 @@ public static class Builder {
278301
private Retry retry = Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(10))
279302
.maxBackoff(Duration.ofMinutes(10));
280303

304+
private Duration timeout;
281305

282-
private Runnable onKeyReceived = () -> { };
306+
private Runnable onKeyReceived = () -> {
307+
};
283308

284309
<M extends MeterRegistry> Builder(M registry, Supplier<String> scrape, ClientTransport clientTransport) {
285310
this.registryAndScrape = new MeterRegistryAndScrape<>(registry, scrape);
@@ -290,7 +315,6 @@ <M extends MeterRegistry> Builder(M registry, Supplier<String> scrape, ClientTra
290315
* Configures the retry for {@link PrometheusRSocketClient}.
291316
*
292317
* @param retry the retry configuration
293-
*
294318
* @return the {@link Builder}
295319
*/
296320
public Builder retry(Retry retry) {
@@ -305,7 +329,7 @@ public Builder retry(Retry retry) {
305329
* @return the {@link Builder}
306330
*/
307331
public Builder timeout(Long timeout) {
308-
PrometheusRSocketClient.timeout = Duration.ofSeconds(timeout);
332+
this.timeout = Duration.ofSeconds(timeout);
309333
return this;
310334
}
311335

@@ -331,6 +355,7 @@ public PrometheusRSocketClient connect() {
331355
registryAndScrape,
332356
clientTransport,
333357
retry,
358+
timeout,
334359
() -> {
335360
LOGGER.info("Connected to RSocket Proxy!");
336361
onKeyReceived.run();
@@ -370,8 +395,7 @@ public PrometheusRSocketClient connectBlockingly(Duration timeout) {
370395
if (!latch.await(timeout.toMillis(), MILLISECONDS)) {
371396
LOGGER.warn("Creating the connection and receiving the key timed out!");
372397
}
373-
}
374-
catch (InterruptedException exception) {
398+
} catch (InterruptedException exception) {
375399
LOGGER.warn("Waiting for receiving the key was interrupted!", exception);
376400
}
377401

0 commit comments

Comments
 (0)