wangyunpeng 1 неделя назад
Родитель
Сommit
982b4a6152
1 измененных файлов с 35 добавлено и 0 удалено
  1. 35 0
      core/src/main/java/com/tzld/videoVector/api/DashScopeEmbeddingApiService.java

+ 35 - 0
core/src/main/java/com/tzld/videoVector/api/DashScopeEmbeddingApiService.java

@@ -13,6 +13,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.List;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 
 
 /**
 /**
  * 阿里云 DashScope 多模态 Embedding API 服务
  * 阿里云 DashScope 多模态 Embedding API 服务
@@ -34,8 +35,19 @@ public class DashScopeEmbeddingApiService {
     @Value("${dashscope.embedding.dashscope.timeout:60}")
     @Value("${dashscope.embedding.dashscope.timeout:60}")
     private int timeout;
     private int timeout;
 
 
+    /**
+     * 触发限流后暂停时间(秒)
+     */
+    @Value("${dashscope.embedding.dashscope.pause-seconds:20}")
+    private long pauseSeconds;
+
     private OkHttpClient client;
     private OkHttpClient client;
 
 
+    /**
+     * 限流恢复时间戳(毫秒),当前时间超过此值才允许请求
+     */
+    private final AtomicLong throttleUntil = new AtomicLong(0);
+
     @PostConstruct
     @PostConstruct
     public void init() {
     public void init() {
         client = new OkHttpClient.Builder()
         client = new OkHttpClient.Builder()
@@ -58,6 +70,21 @@ public class DashScopeEmbeddingApiService {
             return Collections.emptyList();
             return Collections.emptyList();
         }
         }
 
 
+        // 如果处于限流暂停期,等待恢复
+        long resumeAt = throttleUntil.get();
+        long now = System.currentTimeMillis();
+        if (now < resumeAt) {
+            long waitMs = resumeAt - now;
+            log.info("DashScope Embedding 处于限流暂停期,还需等待{}ms后恢复", waitMs);
+            try {
+                Thread.sleep(waitMs);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                log.error("DashScope Embedding 限流等待被中断", e);
+                return Collections.emptyList();
+            }
+        }
+
         try {
         try {
             // 构造请求体:contents 数组中放 text 对象
             // 构造请求体:contents 数组中放 text 对象
             JSONObject contentItem = new JSONObject();
             JSONObject contentItem = new JSONObject();
@@ -87,6 +114,14 @@ public class DashScopeEmbeddingApiService {
                     .build();
                     .build();
 
 
             try (Response response = client.newCall(request).execute()) {
             try (Response response = client.newCall(request).execute()) {
+                if (response.code() == 429) {
+                    String errorBody = response.body() != null ? response.body().string() : "无";
+                    // 触发限流,设置暂停时间
+                    long pauseUntil = System.currentTimeMillis() + pauseSeconds * 1000;
+                    throttleUntil.set(pauseUntil);
+                    log.error("DashScope Embedding 触发限流(429),暂停{}秒后恢复,错误: {}", pauseSeconds, errorBody);
+                    return Collections.emptyList();
+                }
                 if (!response.isSuccessful()) {
                 if (!response.isSuccessful()) {
                     String errorBody = response.body() != null ? response.body().string() : "无";
                     String errorBody = response.body() != null ? response.body().string() : "无";
                     log.error("DashScope Embedding 请求失败,HTTP状态码: {}, 错误信息: {}", response.code(), errorBody);
                     log.error("DashScope Embedding 请求失败,HTTP状态码: {}, 错误信息: {}", response.code(), errorBody);