Skip to content

Commit f91bdbe

Browse files
author
li-guohao
committed
optimize: 优化超时时间和失败重试
1 parent 137c7cd commit f91bdbe

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

CHANGELOG.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- 启动时自动加载alist的token
1010
- 递归导入逻辑由同步到并行
11+
- 优化超时时间和失败重试
1112

1213
# 0.12.6
1314

src/main/java/run/ikaros/plugin/alist/AListClient.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import org.springframework.http.HttpHeaders;
1010
import org.springframework.http.MediaType;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
13+
import org.springframework.retry.annotation.Backoff;
1214
import org.springframework.retry.annotation.Retryable;
1315
import org.springframework.stereotype.Component;
16+
import org.springframework.web.client.ResourceAccessException;
1417
import org.springframework.web.client.RestTemplate;
1518
import reactor.core.Disposable;
1619
import reactor.core.publisher.Flux;
@@ -33,7 +36,7 @@
3336
@Slf4j
3437
@Component
3538
public class AListClient implements InitializingBean, DisposableBean {
36-
private final RestTemplate restTemplate = new RestTemplate();
39+
private RestTemplate restTemplate;
3740
private HttpHeaders httpHeaders = new HttpHeaders();
3841
private AListToken token;
3942
private Disposable refreshTokenTaskDisposable;
@@ -102,7 +105,10 @@ private Mono<Void> createAttachmentRecursively(List<String> paths, Long parentId
102105
.then();
103106
}
104107

105-
@Retryable
108+
@Retryable(
109+
maxAttempts = 8,
110+
backoff = @Backoff(delay = 2000)
111+
)
106112
public AListAttachment[] fetchAttachments(List<String> paths) {
107113
Map<String, String> bodyMap = new HashMap<>();
108114
String path = getPathByPathArr(paths);
@@ -180,7 +186,10 @@ public Mono<AListToken> refreshToken() {
180186
return refreshToken(false);
181187
}
182188

183-
@Retryable
189+
@Retryable(
190+
maxAttempts = 4,
191+
backoff = @Backoff(delay = 2000)
192+
)
184193
public Mono<AListToken> refreshToken(boolean refresh) {
185194
if (StringUtils.isBlank(token.getUrl())
186195
|| StringUtils.isBlank(token.getUsername())
@@ -243,7 +252,10 @@ private String getPathByPathArr(List<String> paths) {
243252
return pStr;
244253
}
245254

246-
@Retryable
255+
@Retryable(
256+
maxAttempts = 5,
257+
backoff = @Backoff(delay = 2000)
258+
)
247259
private AListAttachment fetchAttachmentDetail(String path, AListAttachment attachment) {
248260
if (StringUtils.isEmpty(path)) {
249261
return attachment;
@@ -356,6 +368,12 @@ public void afterPropertiesSet() throws Exception {
356368
// token is null, get config from db.
357369
updateOperateByToken().subscribe();
358370
}
371+
372+
// init rest temp
373+
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
374+
factory.setConnectTimeout(10000); // 设置连接超时时间,10s
375+
factory.setReadTimeout(10000);
376+
restTemplate = new RestTemplate(factory);
359377
}
360378

361379

0 commit comments

Comments
 (0)