Skip to content

Commit 26582a9

Browse files
authoredSep 26, 2024
Merge pull request #155 from cmgyqjj/feature_Support_reconnect_backoff_interface
feature:Support_reconnect_backoff_interface
2 parents 29dcbea + 7197188 commit 26582a9

File tree

9 files changed

+108
-40
lines changed

9 files changed

+108
-40
lines changed
 

‎cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/ClientBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.crossoverjie.cim.client.sdk.io.MessageListener;
55
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck;
66
import java.util.concurrent.ThreadPoolExecutor;
7+
8+
import com.crossoverjie.cim.client.sdk.io.backoff.BackoffStrategy;
79
import okhttp3.OkHttpClient;
810

911
/**
@@ -20,4 +22,5 @@ public interface ClientBuilder {
2022
ClientBuilder okHttpClient(OkHttpClient okHttpClient);
2123
ClientBuilder messageListener(MessageListener messageListener);
2224
ClientBuilder callbackThreadPool(ThreadPoolExecutor callbackThreadPool);
25+
ClientBuilder backoffStrategy(BackoffStrategy backoffStrategy);
2326
}

‎cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/impl/ClientBuilderImpl.java

+13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
import com.crossoverjie.cim.client.sdk.Event;
66
import com.crossoverjie.cim.client.sdk.io.MessageListener;
77
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck;
8+
import com.crossoverjie.cim.client.sdk.io.backoff.BackoffStrategy;
9+
import com.crossoverjie.cim.client.sdk.io.backoff.RandomBackoff;
810
import com.crossoverjie.cim.common.util.StringUtil;
11+
12+
import java.lang.reflect.Constructor;
13+
import java.util.ServiceLoader;
914
import java.util.concurrent.ThreadPoolExecutor;
1015
import okhttp3.OkHttpClient;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1118

1219
public class ClientBuilderImpl implements ClientBuilder {
1320

@@ -79,4 +86,10 @@ public ClientBuilder callbackThreadPool(ThreadPoolExecutor callbackThreadPool) {
7986
this.conf.setCallbackThreadPool(callbackThreadPool);
8087
return this;
8188
}
89+
90+
@Override
91+
public ClientBuilder backoffStrategy(BackoffStrategy backoffStrategy) {
92+
this.conf.setBackoffStrategy(backoffStrategy);
93+
return this;
94+
}
8295
}

‎cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/impl/ClientConfigurationData.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.crossoverjie.cim.client.sdk.impl;
22

33
import com.crossoverjie.cim.client.sdk.Event;
4+
import com.crossoverjie.cim.client.sdk.io.backoff.BackoffStrategy;
45
import com.crossoverjie.cim.client.sdk.io.MessageListener;
6+
import com.crossoverjie.cim.client.sdk.io.backoff.RandomBackoff;
57
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck;
68
import com.fasterxml.jackson.annotation.JsonIgnore;
79
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -46,4 +48,7 @@ public static class Auth{
4648

4749
@JsonIgnore
4850
private ReconnectCheck reconnectCheck = (client) -> true;
51+
52+
@JsonIgnore
53+
private BackoffStrategy backoffStrategy = new RandomBackoff();
4954
}

‎cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/impl/ClientImpl.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ public void reconnect() throws Exception {
202202

203203
this.conf.getEvent().info("cim trigger reconnecting....");
204204

205-
// TODO: 2024/9/13 need a backoff interface
206-
int random = (int) (Math.random() * 7 + 3);
207-
TimeUnit.SECONDS.sleep(random);
205+
this.conf.getBackoffStrategy().runBackoff();
208206

209207
// don't set State ready, because when connect success, the State will be set to ready automate.
210208
connectServer(v -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.crossoverjie.cim.client.sdk.io.backoff;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author:qjj
7+
* @create: 2024-09-21 12:16
8+
* @Description: backoff strategy interface
9+
*/
10+
11+
public interface BackoffStrategy {
12+
/**
13+
* @return the backoff time in milliseconds
14+
*/
15+
long nextBackoff();
16+
17+
/**
18+
* Run the backoff strategy
19+
* @throws InterruptedException
20+
*/
21+
default void runBackoff() throws InterruptedException {
22+
TimeUnit.SECONDS.sleep(nextBackoff());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.crossoverjie.cim.client.sdk.io.backoff;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author:qjj
7+
* @create: 2024-09-21 12:22
8+
* @Description: random backoff strategy
9+
*/
10+
11+
public class RandomBackoff implements BackoffStrategy {
12+
13+
@Override
14+
public long nextBackoff() {
15+
int random = (int) (Math.random() * 7 + 3);
16+
return random;
17+
}
18+
19+
}

‎cim-client-sdk/src/test/java/com/crossoverjie/cim/client/sdk/ClientTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.crossoverjie.cim.client.sdk;
22

33
import com.crossoverjie.cim.client.sdk.impl.ClientConfigurationData;
4+
import com.crossoverjie.cim.client.sdk.io.backoff.RandomBackoff;
45
import com.crossoverjie.cim.client.sdk.route.AbstractRouteBaseTest;
56
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
67
import com.crossoverjie.cim.route.api.vo.req.P2PReqVO;
@@ -224,11 +225,13 @@ public void testReconnect() throws Exception {
224225
.userName(zs)
225226
.userId(zsId)
226227
.build();
228+
var backoffStrategy = new RandomBackoff();
227229

228230
@Cleanup
229231
Client client1 = Client.builder()
230232
.auth(auth1)
231233
.routeUrl(routeUrl)
234+
.backoffStrategy(backoffStrategy)
232235
.build();
233236
TimeUnit.SECONDS.sleep(3);
234237
ClientState.State state = client1.getState();
@@ -242,6 +245,7 @@ public void testReconnect() throws Exception {
242245
.auth(auth2)
243246
.routeUrl(routeUrl)
244247
.messageListener((client, message) -> client2Receive.set(message))
248+
.backoffStrategy(backoffStrategy)
245249
.build();
246250
TimeUnit.SECONDS.sleep(3);
247251
ClientState.State state2 = client2.getState();

‎cim-client/src/main/java/com/crossoverjie/cim/client/config/BeanConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.crossoverjie.cim.client.sdk.Client;
44
import com.crossoverjie.cim.client.sdk.Event;
55
import com.crossoverjie.cim.client.sdk.impl.ClientConfigurationData;
6+
import com.crossoverjie.cim.client.sdk.io.backoff.RandomBackoff;
67
import com.crossoverjie.cim.client.service.MsgLogger;
78
import com.crossoverjie.cim.client.service.ShutDownSign;
89
import com.crossoverjie.cim.client.service.impl.MsgCallBackListener;
@@ -61,6 +62,7 @@ public Client buildClient(@Qualifier("callBackThreadPool") ThreadPoolExecutor ca
6162
.okHttpClient(okHttpClient)
6263
.messageListener(new MsgCallBackListener(msgLogger))
6364
.callbackThreadPool(callbackThreadPool)
65+
.backoffStrategy(new RandomBackoff())
6466
.build();
6567
}
6668

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
spring:
2-
application:
3-
name: cim-client
4-
5-
# web port
6-
server:
7-
port: 8082
8-
9-
logging:
10-
level:
11-
root: error
12-
13-
# enable swagger
14-
springdoc:
15-
swagger-ui:
16-
enabled: true
17-
18-
# log path
19-
cim:
20-
msg:
21-
logger:
22-
path: /opt/logs/cim/
23-
route:
24-
url: http://localhost:8083 # route url suggested that this is Nginx address
25-
user: # cim userId and userName
26-
id: 1725714450795
27-
userName: cj4
28-
callback:
29-
thread:
30-
queue:
31-
size: 2
32-
pool:
33-
size: 2
34-
heartbeat:
35-
time: 60 # cim heartbeat time (seconds)
36-
reconnect:
37-
count: 3
1+
spring:
2+
application:
3+
name: cim-client
4+
5+
# web port
6+
server:
7+
port: 8082
8+
9+
logging:
10+
level:
11+
root: error
12+
13+
# enable swagger
14+
springdoc:
15+
swagger-ui:
16+
enabled: true
17+
18+
# log path
19+
cim:
20+
msg:
21+
logger:
22+
path: /opt/logs/cim/
23+
route:
24+
url: http://localhost:8083 # route url suggested that this is Nginx address
25+
user: # cim userId and userName
26+
id: 1725714450795
27+
userName: cj4
28+
callback:
29+
thread:
30+
queue:
31+
size: 2
32+
pool:
33+
size: 2
34+
heartbeat:
35+
time: 60 # cim heartbeat time (seconds)
36+
reconnect:
37+
count: 3

0 commit comments

Comments
 (0)