File tree 9 files changed +108
-40
lines changed
java/com/crossoverjie/cim/client/config
main/java/com/crossoverjie/cim/client/sdk
test/java/com/crossoverjie/cim/client/sdk
9 files changed +108
-40
lines changed Original file line number Diff line number Diff line change 4
4
import com .crossoverjie .cim .client .sdk .io .MessageListener ;
5
5
import com .crossoverjie .cim .client .sdk .io .ReconnectCheck ;
6
6
import java .util .concurrent .ThreadPoolExecutor ;
7
+
8
+ import com .crossoverjie .cim .client .sdk .io .backoff .BackoffStrategy ;
7
9
import okhttp3 .OkHttpClient ;
8
10
9
11
/**
@@ -20,4 +22,5 @@ public interface ClientBuilder {
20
22
ClientBuilder okHttpClient (OkHttpClient okHttpClient );
21
23
ClientBuilder messageListener (MessageListener messageListener );
22
24
ClientBuilder callbackThreadPool (ThreadPoolExecutor callbackThreadPool );
25
+ ClientBuilder backoffStrategy (BackoffStrategy backoffStrategy );
23
26
}
Original file line number Diff line number Diff line change 5
5
import com .crossoverjie .cim .client .sdk .Event ;
6
6
import com .crossoverjie .cim .client .sdk .io .MessageListener ;
7
7
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 ;
8
10
import com .crossoverjie .cim .common .util .StringUtil ;
11
+
12
+ import java .lang .reflect .Constructor ;
13
+ import java .util .ServiceLoader ;
9
14
import java .util .concurrent .ThreadPoolExecutor ;
10
15
import okhttp3 .OkHttpClient ;
16
+ import org .slf4j .Logger ;
17
+ import org .slf4j .LoggerFactory ;
11
18
12
19
public class ClientBuilderImpl implements ClientBuilder {
13
20
@@ -79,4 +86,10 @@ public ClientBuilder callbackThreadPool(ThreadPoolExecutor callbackThreadPool) {
79
86
this .conf .setCallbackThreadPool (callbackThreadPool );
80
87
return this ;
81
88
}
89
+
90
+ @ Override
91
+ public ClientBuilder backoffStrategy (BackoffStrategy backoffStrategy ) {
92
+ this .conf .setBackoffStrategy (backoffStrategy );
93
+ return this ;
94
+ }
82
95
}
Original file line number Diff line number Diff line change 1
1
package com .crossoverjie .cim .client .sdk .impl ;
2
2
3
3
import com .crossoverjie .cim .client .sdk .Event ;
4
+ import com .crossoverjie .cim .client .sdk .io .backoff .BackoffStrategy ;
4
5
import com .crossoverjie .cim .client .sdk .io .MessageListener ;
6
+ import com .crossoverjie .cim .client .sdk .io .backoff .RandomBackoff ;
5
7
import com .crossoverjie .cim .client .sdk .io .ReconnectCheck ;
6
8
import com .fasterxml .jackson .annotation .JsonIgnore ;
7
9
import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
@@ -46,4 +48,7 @@ public static class Auth{
46
48
47
49
@ JsonIgnore
48
50
private ReconnectCheck reconnectCheck = (client ) -> true ;
51
+
52
+ @ JsonIgnore
53
+ private BackoffStrategy backoffStrategy = new RandomBackoff ();
49
54
}
Original file line number Diff line number Diff line change @@ -202,9 +202,7 @@ public void reconnect() throws Exception {
202
202
203
203
this .conf .getEvent ().info ("cim trigger reconnecting...." );
204
204
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 ();
208
206
209
207
// don't set State ready, because when connect success, the State will be set to ready automate.
210
208
connectServer (v -> {
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package com .crossoverjie .cim .client .sdk ;
2
2
3
3
import com .crossoverjie .cim .client .sdk .impl .ClientConfigurationData ;
4
+ import com .crossoverjie .cim .client .sdk .io .backoff .RandomBackoff ;
4
5
import com .crossoverjie .cim .client .sdk .route .AbstractRouteBaseTest ;
5
6
import com .crossoverjie .cim .common .pojo .CIMUserInfo ;
6
7
import com .crossoverjie .cim .route .api .vo .req .P2PReqVO ;
@@ -224,11 +225,13 @@ public void testReconnect() throws Exception {
224
225
.userName (zs )
225
226
.userId (zsId )
226
227
.build ();
228
+ var backoffStrategy = new RandomBackoff ();
227
229
228
230
@ Cleanup
229
231
Client client1 = Client .builder ()
230
232
.auth (auth1 )
231
233
.routeUrl (routeUrl )
234
+ .backoffStrategy (backoffStrategy )
232
235
.build ();
233
236
TimeUnit .SECONDS .sleep (3 );
234
237
ClientState .State state = client1 .getState ();
@@ -242,6 +245,7 @@ public void testReconnect() throws Exception {
242
245
.auth (auth2 )
243
246
.routeUrl (routeUrl )
244
247
.messageListener ((client , message ) -> client2Receive .set (message ))
248
+ .backoffStrategy (backoffStrategy )
245
249
.build ();
246
250
TimeUnit .SECONDS .sleep (3 );
247
251
ClientState .State state2 = client2 .getState ();
Original file line number Diff line number Diff line change 3
3
import com .crossoverjie .cim .client .sdk .Client ;
4
4
import com .crossoverjie .cim .client .sdk .Event ;
5
5
import com .crossoverjie .cim .client .sdk .impl .ClientConfigurationData ;
6
+ import com .crossoverjie .cim .client .sdk .io .backoff .RandomBackoff ;
6
7
import com .crossoverjie .cim .client .service .MsgLogger ;
7
8
import com .crossoverjie .cim .client .service .ShutDownSign ;
8
9
import com .crossoverjie .cim .client .service .impl .MsgCallBackListener ;
@@ -61,6 +62,7 @@ public Client buildClient(@Qualifier("callBackThreadPool") ThreadPoolExecutor ca
61
62
.okHttpClient (okHttpClient )
62
63
.messageListener (new MsgCallBackListener (msgLogger ))
63
64
.callbackThreadPool (callbackThreadPool )
65
+ .backoffStrategy (new RandomBackoff ())
64
66
.build ();
65
67
}
66
68
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments