Skip to content

Commit 77619a2

Browse files
authored
feat: add business error code (#199)
1 parent 96ef806 commit 77619a2

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/main/java/io/github/doocs/im/ClientConfiguration.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import io.github.doocs.im.util.VersionInfoUtil;
44
import okhttp3.ConnectionPool;
55

6+
import java.util.Collections;
7+
import java.util.HashSet;
68
import java.util.Objects;
9+
import java.util.Set;
710

811
/**
912
* 客户端配置类
@@ -36,6 +39,17 @@ public class ClientConfiguration {
3639
* 默认业务错误码重试开关关闭
3740
*/
3841
public static final boolean DEFAULT_ENABLE_BUSINESS_RETRY = false;
42+
43+
/**
44+
* 默认重试错误码
45+
*/
46+
public static final Set<Integer> DEFAULT_BUSINESS_RETRY_CODES =
47+
Collections.unmodifiableSet(new HashSet<Integer>() {{
48+
add(10002);
49+
add(20004);
50+
add(20005);
51+
}});
52+
3953
/**
4054
* 默认超时时间(毫秒)
4155
*/
@@ -63,14 +77,15 @@ public class ClientConfiguration {
6377
private long expireTime = DEFAULT_EXPIRE_TIME;
6478
private boolean autoRenewSig = DEFAULT_RENEW_SIG;
6579
private boolean enableBusinessRetry = DEFAULT_ENABLE_BUSINESS_RETRY;
80+
private Set<Integer> businessRetryCodes = DEFAULT_BUSINESS_RETRY_CODES;
6681
private String userAgent = DEFAULT_USER_AGENT;
6782
private ConnectionPool connectionPool = DEFAULT_CONNECTION_POOL;
6883

6984
public ClientConfiguration() {
7085
}
7186

7287
public ClientConfiguration(int maxRetries, long retryIntervalMs, long connectTimeout, long readTimeout, long writeTimeout,
73-
long callTimeout, long expireTime, boolean autoRenewSig, boolean enableBusinessRetry,
88+
long callTimeout, long expireTime, boolean autoRenewSig, boolean enableBusinessRetry, Set<Integer> businessRetryCodes,
7489
String userAgent, ConnectionPool connectionPool) {
7590
if (connectionPool == null) {
7691
connectionPool = DEFAULT_CONNECTION_POOL;
@@ -84,6 +99,7 @@ public ClientConfiguration(int maxRetries, long retryIntervalMs, long connectTim
8499
this.expireTime = expireTime;
85100
this.autoRenewSig = autoRenewSig;
86101
this.enableBusinessRetry = enableBusinessRetry;
102+
this.businessRetryCodes = businessRetryCodes;
87103
this.userAgent = userAgent;
88104
this.connectionPool = connectionPool;
89105
}
@@ -98,6 +114,7 @@ private ClientConfiguration(Builder builder) {
98114
this.expireTime = builder.expireTime;
99115
this.autoRenewSig = builder.autoRenewSig;
100116
this.enableBusinessRetry = builder.enableBusinessRetry;
117+
this.businessRetryCodes = builder.businessRetryCodes;
101118
this.userAgent = builder.userAgent;
102119
this.connectionPool = builder.connectionPool;
103120
}
@@ -178,6 +195,14 @@ public void setEnableBusinessRetry(boolean enableBusinessRetry) {
178195
this.enableBusinessRetry = enableBusinessRetry;
179196
}
180197

198+
public Set<Integer> getBusinessRetryCodes() {
199+
return businessRetryCodes;
200+
}
201+
202+
public void setBusinessRetryCodes(Set<Integer> businessRetryCodes) {
203+
this.businessRetryCodes = businessRetryCodes;
204+
}
205+
181206
public String getUserAgent() {
182207
return userAgent;
183208
}
@@ -234,6 +259,9 @@ public boolean equals(Object o) {
234259
if (enableBusinessRetry != that.enableBusinessRetry) {
235260
return false;
236261
}
262+
if (businessRetryCodes != that.businessRetryCodes) {
263+
return false;
264+
}
237265
if (!userAgent.equals(that.userAgent)) {
238266
return false;
239267
}
@@ -242,7 +270,7 @@ public boolean equals(Object o) {
242270

243271
@Override
244272
public int hashCode() {
245-
return Objects.hash(maxRetries, retryIntervalMs, connectTimeout, readTimeout, writeTimeout, callTimeout, expireTime, autoRenewSig, enableBusinessRetry, userAgent, connectionPool);
273+
return Objects.hash(maxRetries, retryIntervalMs, connectTimeout, readTimeout, writeTimeout, callTimeout, expireTime, autoRenewSig, enableBusinessRetry, businessRetryCodes, userAgent, connectionPool);
246274
}
247275

248276
public static final class Builder {
@@ -255,6 +283,7 @@ public static final class Builder {
255283
private long expireTime = DEFAULT_EXPIRE_TIME;
256284
private boolean autoRenewSig = DEFAULT_RENEW_SIG;
257285
private boolean enableBusinessRetry = DEFAULT_ENABLE_BUSINESS_RETRY;
286+
private Set<Integer> businessRetryCodes = DEFAULT_BUSINESS_RETRY_CODES;
258287
private String userAgent = DEFAULT_USER_AGENT;
259288
private ConnectionPool connectionPool = DEFAULT_CONNECTION_POOL;
260289

@@ -310,6 +339,11 @@ public Builder enableBusinessRetry(boolean enableBusinessRetry) {
310339
return this;
311340
}
312341

342+
public Builder businessRetryCodes(Set<Integer> businessRetryCodes) {
343+
this.businessRetryCodes = businessRetryCodes;
344+
return this;
345+
}
346+
313347
public Builder userAgent(String userAgent) {
314348
this.userAgent = userAgent;
315349
return this;

src/main/java/io/github/doocs/im/util/HttpUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class HttpUtil {
3232
.writeTimeout(DEFAULT_CONFIG.getWriteTimeout(), TimeUnit.MILLISECONDS)
3333
.callTimeout(DEFAULT_CONFIG.getCallTimeout(), TimeUnit.MILLISECONDS)
3434
.retryOnConnectionFailure(false)
35-
.addInterceptor(new RetryInterceptor(DEFAULT_CONFIG.getMaxRetries(), DEFAULT_CONFIG.getRetryIntervalMs(), null, DEFAULT_CONFIG.isEnableBusinessRetry()))
35+
.addInterceptor(new RetryInterceptor(DEFAULT_CONFIG.getMaxRetries(), DEFAULT_CONFIG.getRetryIntervalMs(), DEFAULT_CONFIG.getBusinessRetryCodes(), DEFAULT_CONFIG.isEnableBusinessRetry()))
3636
.build();
3737

3838
private HttpUtil() {
@@ -59,7 +59,7 @@ private static OkHttpClient getClient(ClientConfiguration config) {
5959
.writeTimeout(cfg.getWriteTimeout(), TimeUnit.MILLISECONDS)
6060
.callTimeout(cfg.getCallTimeout(), TimeUnit.MILLISECONDS)
6161
.retryOnConnectionFailure(false)
62-
.addInterceptor(new RetryInterceptor(cfg.getMaxRetries(), cfg.getRetryIntervalMs(), null, DEFAULT_CONFIG.isEnableBusinessRetry()))
62+
.addInterceptor(new RetryInterceptor(cfg.getMaxRetries(), cfg.getRetryIntervalMs(), DEFAULT_CONFIG.getBusinessRetryCodes(), DEFAULT_CONFIG.isEnableBusinessRetry()))
6363
.build());
6464
}
6565

0 commit comments

Comments
 (0)