1
1
package com .getui .push .v2 .sdk ;
2
2
3
+ import com .getui .push .v2 .sdk .anno .method .GtDelete ;
4
+ import com .getui .push .v2 .sdk .anno .method .GtGet ;
5
+ import com .getui .push .v2 .sdk .anno .method .GtPost ;
6
+ import com .getui .push .v2 .sdk .anno .method .GtPut ;
7
+ import com .getui .push .v2 .sdk .api .PushApi ;
3
8
import com .getui .push .v2 .sdk .common .Assert ;
9
+ import com .getui .push .v2 .sdk .core .Configs ;
4
10
import org .apache .http .client .config .RequestConfig ;
5
11
6
12
import java .util .Arrays ;
7
13
import java .util .HashSet ;
14
+ import java .util .Map ;
8
15
import java .util .Set ;
16
+ import java .util .concurrent .ConcurrentHashMap ;
9
17
10
18
/**
11
19
* 应用相关配置信息
@@ -28,7 +36,7 @@ public class GtApiConfiguration {
28
36
*/
29
37
private String masterSecret ;
30
38
/**
31
- * 接口调用前缀, 可不包含 {@link #appId}
39
+ * 接口调用前缀, 不带 {@link #appId}
32
40
* eg. https://restapi.getui.com/v2
33
41
*/
34
42
private String domain = "https://restapi.getui.com/v2" ;
@@ -70,20 +78,23 @@ public class GtApiConfiguration {
70
78
*
71
79
* @see RequestConfig#getSocketTimeout()
72
80
*/
81
+ private final String SOCKET_TIMEOUT_KEY = "gt.socket.timeout" ;
73
82
private int soTimeout = 30000 ;
74
83
/**
75
84
* http连接超时时间,单位ms
76
85
*
77
86
* @see RequestConfig#getConnectTimeout()
78
87
*/
79
- private int connectTimeout = 60000 ;
88
+ private final String CONNECT_TIMEOUT_KEY = "gt.connect.timeout" ;
89
+ private int connectTimeout = 10000 ;
80
90
/**
81
91
* 从连接池中获取http连接的超时时间,单位ms
82
92
*/
83
93
private int connectionRequestTimeout = 0 ;
84
94
/**
85
- * http请求失败,最大尝试次数
95
+ * http请求失败,最大尝试次数,默认重试1次,0表示不重试
86
96
*/
97
+ private final String MAX_HTTP_TRY_TIME_KEY = "gt.max.http.try.times" ;
87
98
private int maxHttpTryTime = 1 ;
88
99
/**
89
100
* 保持长连接的时长,最大{@link #MAX_KEEP_ALIVE_SECONDS}
@@ -101,6 +112,33 @@ public class GtApiConfiguration {
101
112
*/
102
113
private GtHttpProxyConfig proxyConfig ;
103
114
115
+ /**
116
+ * 存储uri和socketTimeout,支持设置接口维度socketTimeout
117
+ */
118
+ private Map <String , Integer > uriToSocketTimeoutMap = new ConcurrentHashMap <>();
119
+ /**
120
+ * 最大失败次数,单位时间内达到此阈值,切换域名,默认10次
121
+ */
122
+ public final static String MAX_FAILED_NUM_KEY = "gt.max.failed.num" ;
123
+ private int maxFailedNum = 10 ;
124
+
125
+ /**
126
+ * 连续失败次数达到阈值,切换域名,默认3
127
+ */
128
+ public final static String CONTINUOUS_FAILED_NUM_KEY = "gt.continuous.failed.num" ;
129
+ private int continuousFailedNum = Configs .MAX_FAIL_CONTINUOUSLY ;
130
+
131
+ /**
132
+ * 重置最大失败次数的时间间隔,默认3s
133
+ */
134
+ public final static String CHECK_MAX_FAILED_NUM_INTERVAL_KEY = "gt.check.max.failed.num.interval" ;
135
+ private long checkMaxFailedNumInterval = 3000 ;
136
+ /**
137
+ * 域名检测的超时时间,单位ms,默认100ms
138
+ */
139
+ public final static String HTTP_CHECK_TIMEOUT_KEY = "gt.http.check.timeout" ;
140
+ private int httpCheckTimeout = 100 ;
141
+
104
142
/**
105
143
* @param domain 接口调用前缀, 可不含{@link #appId}
106
144
*/
@@ -209,15 +247,15 @@ public void setCheckHealthInterval(long checkHealthInterval) {
209
247
}
210
248
211
249
public int getSoTimeout () {
212
- return soTimeout ;
250
+ return Integer . getInteger ( SOCKET_TIMEOUT_KEY , soTimeout ) ;
213
251
}
214
252
215
253
public void setSoTimeout (int soTimeout ) {
216
254
this .soTimeout = soTimeout ;
217
255
}
218
256
219
257
public int getConnectTimeout () {
220
- return connectTimeout ;
258
+ return Integer . getInteger ( CONNECT_TIMEOUT_KEY , connectTimeout ) ;
221
259
}
222
260
223
261
public void setConnectTimeout (int connectTimeout ) {
@@ -233,7 +271,7 @@ public void setConnectionRequestTimeout(int connectionRequestTimeout) {
233
271
}
234
272
235
273
public int getMaxHttpTryTime () {
236
- return maxHttpTryTime ;
274
+ return Integer . getInteger ( MAX_HTTP_TRY_TIME_KEY , maxHttpTryTime ) ;
237
275
}
238
276
239
277
public void setMaxHttpTryTime (int maxHttpTryTime ) {
@@ -281,6 +319,38 @@ public void setProxyConfig(GtHttpProxyConfig proxyConfig) {
281
319
this .proxyConfig = proxyConfig ;
282
320
}
283
321
322
+ public int getContinuousFailedNum () {
323
+ return Integer .getInteger (CONTINUOUS_FAILED_NUM_KEY , continuousFailedNum );
324
+ }
325
+
326
+ public void setContinuousFailedNum (int continuousFailedNum ) {
327
+ this .continuousFailedNum = continuousFailedNum ;
328
+ }
329
+
330
+ public int getMaxFailedNum () {
331
+ return Integer .getInteger (MAX_FAILED_NUM_KEY , maxFailedNum );
332
+ }
333
+
334
+ public void setMaxFailedNum (int maxFailedNum ) {
335
+ this .maxFailedNum = maxFailedNum ;
336
+ }
337
+
338
+ public long getCheckMaxFailedNumInterval () {
339
+ return Long .getLong (CHECK_MAX_FAILED_NUM_INTERVAL_KEY , checkMaxFailedNumInterval );
340
+ }
341
+
342
+ public void setCheckMaxFailedNumInterval (long checkMaxFailedNumInterval ) {
343
+ this .checkMaxFailedNumInterval = checkMaxFailedNumInterval ;
344
+ }
345
+
346
+ public int getHttpCheckTimeout () {
347
+ return Integer .getInteger (HTTP_CHECK_TIMEOUT_KEY , httpCheckTimeout );
348
+ }
349
+
350
+ public void setHttpCheckTimeout (int httpCheckTimeout ) {
351
+ this .httpCheckTimeout = httpCheckTimeout ;
352
+ }
353
+
284
354
@ Override
285
355
public boolean equals (Object o ) {
286
356
if (this == o ) {
@@ -329,4 +399,30 @@ public String prefixOfKey() {
329
399
return String .format ("%s|%s" , this .getAppId (), this .getAppKey ());
330
400
}
331
401
402
+ /**
403
+ * 针对接口设置超时时间,可根据监控修改合理值
404
+ *
405
+ * @param uri {@link GtGet#uri()}, {@link GtPost#uri()}, {@link GtDelete#uri()}, {@link GtPut#uri()}
406
+ * @param socketTimeout {@link RequestConfig#getSocketTimeout()},单位: ms
407
+ */
408
+ public void setCustomSocketTimeout (String uri , int socketTimeout ) {
409
+ this .uriToSocketTimeoutMap .put (uri , socketTimeout );
410
+ }
411
+
412
+ public void resetConnectAndSocketTimeout () {
413
+ setConnectTimeout (3000 );
414
+ setCustomSocketTimeout (PushApi .singleCidUri , 3000 );
415
+ setCustomSocketTimeout (PushApi .singleAliasUri , 3000 );
416
+ setCustomSocketTimeout (PushApi .singleBatchCidUri , 6000 );
417
+ setCustomSocketTimeout (PushApi .singleBatchAliasUri , 6000 );
418
+ setCustomSocketTimeout (PushApi .singleBatchAliasUri , 6000 );
419
+ setCustomSocketTimeout (PushApi .pushListMessageUri , 3000 );
420
+ setCustomSocketTimeout (PushApi .pushListCidUri , 6000 );
421
+ setCustomSocketTimeout (PushApi .pushListAliasUri , 6000 );
422
+ }
423
+
424
+ public int getCustomSocketTimeout (String uri ) {
425
+ return Integer .getInteger (uri , this .uriToSocketTimeoutMap .getOrDefault (uri , 0 ));
426
+ }
427
+
332
428
}
0 commit comments