Skip to content

Commit 38a5b2e

Browse files
committed
websocket improvements
1 parent dcecccb commit 38a5b2e

File tree

8 files changed

+56
-17
lines changed

8 files changed

+56
-17
lines changed

src/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.sportradar.mbs.sdk</groupId>
66
<artifactId>mbs-sdk-parent</artifactId>
77
<packaging>pom</packaging>
8-
<version>0.9.7-SNAPSHOT</version>
8+
<version>0.9.6-SNAPSHOT</version>
99
<name>MBS SDK</name>
1010
<description>MBS SDK is a client library that enables easier integration with the Sportradar MBS</description>
1111
<inceptionYear>2024</inceptionYear>
@@ -34,7 +34,7 @@
3434
<scm>
3535
<url>https://github.com/sportradar/MbsSdkJava</url>
3636
<connection>scm:git:[email protected]:sportradar/MbsSdkJava.git</connection>
37-
<tag>v0.9.7</tag>
37+
<tag>v0.9.6</tag>
3838
</scm>
3939

4040
<modules>

src/sdk/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.sportradar.mbs.sdk</groupId>
55
<artifactId>mbs-sdk-parent</artifactId>
6-
<version>0.9.7-SNAPSHOT</version>
6+
<version>0.9.6-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

src/sdk/src/main/java/com/sportradar/mbs/sdk/MbsSdkConfig.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class MbsSdkConfig {
3131
private Duration wsReceiveMessageTimeout;
3232
private Duration wsConsumerGraceTimeout;
3333
private Duration wsRefreshConnectionTimeout;
34+
private Duration wsPingInterval;
3435
private Integer wsNumberOfConnections;
3536
private BiConsumer<MbsSdk, Exception> unhandledExceptionHandler;
3637

@@ -377,12 +378,30 @@ public Duration getWsRefreshConnectionTimeout() {
377378
/**
378379
* Sets the timeout for refreshing the WebSocket connection.
379380
*
380-
* @param wsRefreshConnectionTimeout The timeout for refreshing the WebSocket connection to set.
381+
* @param wsRefreshConnectionTimeout The timeout for refreshing the WebSocket connection.
381382
*/
382383
public void setWsRefreshConnectionTimeout(Duration wsRefreshConnectionTimeout) {
383384
this.wsRefreshConnectionTimeout = wsRefreshConnectionTimeout;
384385
}
385386

387+
/**
388+
* Gets the interval of WebSocket connection ping.
389+
*
390+
* @return The interval of WebSocket connection ping.
391+
*/
392+
public Duration getWsPingInterval() {
393+
return wsPingInterval;
394+
}
395+
396+
/**
397+
* Sets the interval of WebSocket connection ping.
398+
*
399+
* @param wsPingInterval The interval of WebSocket connection ping.
400+
*/
401+
public void setWsPingInterval(Duration wsPingInterval) {
402+
this.wsPingInterval = wsPingInterval;
403+
}
404+
386405
/**
387406
* Gets the number of connections for the WebSocket.
388407
*

src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/config/ImmutableConfig.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class ImmutableConfig implements
3737
private final Duration wsReceiveMessageTimeout;
3838
private final Duration wsConsumerGraceTimeout;
3939
private final Duration wsRefreshConnectionTimeout;
40+
private final Duration wsPingInterval;
4041
private final int wsNumberOfConnections;
4142

4243
public ImmutableConfig(final MbsSdkConfig config) {
@@ -47,20 +48,21 @@ public ImmutableConfig(final MbsSdkConfig config) {
4748
this.wsServer = notNull(config.getWsServer(), "wsServer");
4849
this.operatorId = config.getOperatorId();
4950
this.authRequestTimeout = withDefault(config.getAuthRetryDelay(), Duration.ofSeconds(5), MIN_DURATION);
50-
this.authRetryDelay = withDefault(config.getAuthRetryDelay(), Duration.ofMillis(500), MIN_DURATION);
51+
this.authRetryDelay = withDefault(config.getAuthRetryDelay(), Duration.ofSeconds(1), MIN_DURATION);
5152
this.wsNumberOfConnections = withDefault(config.getWsNumberOfConnections(), 1, 1);
5253
this.wsReconnectTimeout = withDefault(config.getWsReconnectTimeout(), Duration.ofSeconds(10), MIN_DURATION);
5354
this.wsFetchMessageTimeout = withDefault(config.getWsFetchMessageTimeout(), Duration.ofSeconds(1), MIN_DURATION);
5455
this.wsSendMessageTimeout = withDefault(config.getWsSendMessageTimeout(), Duration.ofSeconds(1), MIN_DURATION);
5556
this.wsReceiveMessageTimeout = withDefault(config.getWsReceiveMessageTimeout(), Duration.ofSeconds(30), MIN_DURATION);
56-
this.wsConsumerGraceTimeout = withDefault(config.getWsConsumerGraceTimeout(), Duration.ofMinutes(1), MIN_DURATION);
57+
this.wsConsumerGraceTimeout = withDefault(config.getWsConsumerGraceTimeout(), Duration.ofMinutes(10), MIN_DURATION);
5758
this.wsRefreshConnectionTimeout = withDefault(config.getWsRefreshConnectionTimeout(), Duration.ofMinutes(100), MIN_DURATION);
5859
this.protocolRetryCount = withDefault(config.getProtocolRetryCount(), 0, 0);
5960
this.protocolMaxSendBufferSize = withDefault(config.getProtocolMaxSendBufferSize(), 1_000, 1);
6061
this.protocolConnectTimeout = withDefault(config.getProtocolConnectTimeout(), Duration.ofSeconds(10), MIN_DURATION);
6162
this.protocolReceiveResponseTimeout = withDefault(config.getProtocolReceiveResponseTimeout(), Duration.ofSeconds(20), MIN_DURATION);
6263
this.protocolEnqueueTimeout = withDefault(config.getProtocolEnqueueTimeout(), Duration.ofMillis(100), MIN_DURATION);
6364
this.protocolDequeueTimeout = withDefault(config.getProtocolDequeueTimeout(), Duration.ofSeconds(1), MIN_DURATION);
65+
this.wsPingInterval = withDefault(config.getWsPingInterval(), Duration.ofMinutes(1), MIN_DURATION);
6466
this.protocolNumberOfDispatchers = withDefault(config.getProtocolNumberOfDispatchers(), 1, 1);
6567
}
6668

@@ -173,4 +175,9 @@ public Duration getWsRefreshConnectionTimeout() {
173175
public int getWsNumberOfConnections() {
174176
return wsNumberOfConnections;
175177
}
178+
179+
@Override
180+
public Duration getWsPingInterval() {
181+
return wsPingInterval;
182+
}
176183
}

src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/config/WebSocketConnectionConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public interface WebSocketConnectionConfig {
1818
Duration getWsConsumerGraceTimeout();
1919

2020
Duration getWsRefreshConnectionTimeout();
21+
22+
Duration getWsPingInterval();
2123
}

src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/connection/WebSocketConnection.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void sendLoop() {
100100

101101
private void sendMsg(final WebSocket ws, final List<ByteBuffer> msgs) {
102102
for (int i = 0; i < msgs.size(); i++) {
103-
ws.sendFragmentedFrame(Opcode.TEXT, msgs.get(i), i == (msgs.size() - 1));
103+
ws.sendFragmentedFrame(Opcode.TEXT, msgs.get(i).duplicate(), i == (msgs.size() - 1));
104104
}
105105
}
106106

@@ -110,7 +110,7 @@ private void reconnectWebSocket(final WebSocket ws, final boolean throwExc) {
110110
}
111111
final WebSocket newWs;
112112
try {
113-
newWs = new WebSocket(this, config.getWsServer(), tokenProvider.getToken());
113+
newWs = new WebSocket(this);
114114
if (!newWs.connectBlocking(config.getWsReconnectTimeout().toMillis(), MILLISECONDS)) {
115115
throw new WebSocketConnectionException("Socket connect failed.");
116116
}
@@ -126,9 +126,9 @@ private void reconnectWebSocket(final WebSocket ws, final boolean throwExc) {
126126
}
127127
if (this.webSocket.compareAndSet(ws, newWs)) {
128128
if (ws != null) {
129-
delay(ws::close, config.getWsConsumerGraceTimeout().toMillis(), MILLISECONDS);
129+
delay(ws::close, config.getWsConsumerGraceTimeout());
130130
}
131-
delay(() -> reconnectWebSocket(newWs, false), config.getWsRefreshConnectionTimeout().toMillis(), MILLISECONDS);
131+
delay(() -> reconnectWebSocket(newWs, false), config.getWsRefreshConnectionTimeout());
132132
} else {
133133
newWs.close();
134134
}
@@ -156,12 +156,23 @@ public static class WebSocket extends WebSocketClient {
156156

157157
private final WebSocketConnection connection;
158158

159-
public WebSocket(final WebSocketConnection connection, final URI serverUri, final String token) {
160-
super(serverUri, headers(token));
159+
public WebSocket(final WebSocketConnection connection) {
160+
super(serverUri(connection), headers(connection));
161+
this.setConnectionLostTimeout(getPingInterval(connection));
161162
this.connection = connection;
162163
}
163164

164-
private static Map<String, String> headers(final String token) {
165+
private static URI serverUri(final WebSocketConnection connection) {
166+
return connection.config.getWsServer();
167+
}
168+
169+
private static int getPingInterval(final WebSocketConnection connection) {
170+
final long interval = connection.config.getWsPingInterval().toSeconds();
171+
return (int) Math.max(10, Math.min(300, interval));
172+
}
173+
174+
private static Map<String, String> headers(final WebSocketConnection connection) {
175+
final String token = connection.tokenProvider.getToken();
165176
final Map<String, String> headers = new HashMap<>();
166177
headers.put("Authorization", "Bearer " + token);
167178
return headers;

src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/protocol/ProtocolEngine.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ private <R extends ContentResponse> void enqueueSendMsg(final Awaiter<R> awaiter
132132
sendQueue.add(awaiter.getSendWsInputMessage());
133133

134134
final int nextRetryCount = retryCount + 1;
135-
delay(() -> enqueueSendMsg(awaiter, nextRetryCount),
136-
config.getProtocolReceiveResponseTimeout().toMillis(), TimeUnit.MILLISECONDS);
135+
delay(() -> enqueueSendMsg(awaiter, nextRetryCount), config.getProtocolReceiveResponseTimeout());
137136
}
138137

139138
private List<ByteBuffer> createFrames(final Request request) throws JsonProcessingException {

src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/utils/Delayer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sportradar.mbs.sdk.internal.utils;
22

3+
import java.time.Duration;
34
import java.util.concurrent.ScheduledFuture;
45
import java.util.concurrent.ScheduledThreadPoolExecutor;
56
import java.util.concurrent.ThreadFactory;
@@ -15,8 +16,8 @@ public class Delayer {
1516
EXECUTOR = executor;
1617
}
1718

18-
public static ScheduledFuture<?> delay(final Runnable command, final long delay, final TimeUnit unit) {
19-
return EXECUTOR.schedule(command, delay, unit);
19+
public static ScheduledFuture<?> delay(final Runnable command, final Duration delay) {
20+
return EXECUTOR.schedule(command, delay.toMillis(), TimeUnit.MILLISECONDS);
2021
}
2122

2223
private static final class SdkDelayerThreadFactory implements ThreadFactory {

0 commit comments

Comments
 (0)