Skip to content

Commit

Permalink
optimize: 优化超时时间和失败重试
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed Jun 11, 2024
1 parent 137c7cd commit f91bdbe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

# 0.12.6

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/run/ikaros/plugin/alist/AListClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
Expand All @@ -33,7 +36,7 @@
@Slf4j
@Component
public class AListClient implements InitializingBean, DisposableBean {
private final RestTemplate restTemplate = new RestTemplate();
private RestTemplate restTemplate;
private HttpHeaders httpHeaders = new HttpHeaders();
private AListToken token;
private Disposable refreshTokenTaskDisposable;
Expand Down Expand Up @@ -102,7 +105,10 @@ private Mono<Void> createAttachmentRecursively(List<String> paths, Long parentId
.then();
}

@Retryable
@Retryable(
maxAttempts = 8,
backoff = @Backoff(delay = 2000)
)
public AListAttachment[] fetchAttachments(List<String> paths) {
Map<String, String> bodyMap = new HashMap<>();
String path = getPathByPathArr(paths);
Expand Down Expand Up @@ -180,7 +186,10 @@ public Mono<AListToken> refreshToken() {
return refreshToken(false);
}

@Retryable
@Retryable(
maxAttempts = 4,
backoff = @Backoff(delay = 2000)
)
public Mono<AListToken> refreshToken(boolean refresh) {
if (StringUtils.isBlank(token.getUrl())
|| StringUtils.isBlank(token.getUsername())
Expand Down Expand Up @@ -243,7 +252,10 @@ private String getPathByPathArr(List<String> paths) {
return pStr;
}

@Retryable
@Retryable(
maxAttempts = 5,
backoff = @Backoff(delay = 2000)
)
private AListAttachment fetchAttachmentDetail(String path, AListAttachment attachment) {
if (StringUtils.isEmpty(path)) {
return attachment;
Expand Down Expand Up @@ -356,6 +368,12 @@ public void afterPropertiesSet() throws Exception {
// token is null, get config from db.
updateOperateByToken().subscribe();
}

// init rest temp
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(10000); // 设置连接超时时间,10s
factory.setReadTimeout(10000);
restTemplate = new RestTemplate(factory);
}


Expand Down

0 comments on commit f91bdbe

Please sign in to comment.