task-67: 复用 RustFS/MinIO 客户端与 HTTP 连接池
- buildClient 改为 double-check 懒加载缓存共享 MinioClient(volatile 单例), 同一实例持有同一 OkHttpClient,连接池随实例共享,不再每次操作新建客户端 - supplier 注入路径优先且不写共享缓存,测试隔离不受污染 - 未配置/空白 endpoint 时不创建实例,拒绝操作并保持缓存为空
This commit is contained in:
+20
-6
@@ -49,6 +49,11 @@ public class RustfsObjectStorageService {
|
||||
private volatile long failureWindowStartedAtMillis;
|
||||
private volatile long circuitOpenUntilMillis;
|
||||
private volatile OkHttpClient httpClient;
|
||||
/**
|
||||
* 懒加载缓存的共享 MinioClient(double-check 单例):复用同一实例与同一
|
||||
* OkHttpClient(连接池随实例共享),避免每次操作创建客户端。
|
||||
*/
|
||||
private volatile MinioClient sharedMinioClient;
|
||||
|
||||
@Autowired
|
||||
public RustfsObjectStorageService(TransientStorageProperties properties,
|
||||
@@ -275,12 +280,21 @@ public class RustfsObjectStorageService {
|
||||
if (minioClientSupplier != null) {
|
||||
return minioClientSupplier.get();
|
||||
}
|
||||
return MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKeyId(), properties.getAccessKeySecret())
|
||||
.region(properties.getRegion())
|
||||
.httpClient(getHttpClient(deadlineNanos))
|
||||
.build();
|
||||
MinioClient shared = sharedMinioClient;
|
||||
if (shared != null) {
|
||||
return shared;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (sharedMinioClient == null) {
|
||||
sharedMinioClient = MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKeyId(), properties.getAccessKeySecret())
|
||||
.region(properties.getRegion())
|
||||
.httpClient(getHttpClient(deadlineNanos))
|
||||
.build();
|
||||
}
|
||||
return sharedMinioClient;
|
||||
}
|
||||
}
|
||||
|
||||
OkHttpClient getHttpClient() {
|
||||
|
||||
Reference in New Issue
Block a user