supeng 7 hours ago
parent
commit
db62c007d6

+ 4 - 2
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/DeepSeekGenerateContentAction.java

@@ -37,6 +37,8 @@ public class DeepSeekGenerateContentAction implements Action {
     public static final String MODEL_REASONER = "deepseek-reasoner";
     public static final Double TEMPERATURE = 1.0;
 
+    private static final Integer RETRY_TIMES = 3;
+
     @Override
     public String execute(ActionParam param) {
         Integer type = param.getType();
@@ -55,7 +57,7 @@ public class DeepSeekGenerateContentAction implements Action {
         deepSeekParam.setTemperature(TEMPERATURE);
         //reasonser模型不支持 json format
 //        deepSeekParam.setResponseFormat(ResponseFormatTypeEnum.JSON.getValue());
-        return RetryUtil.executeWithRetry(()-> {
+        return RetryUtil.executeWithRetry(() -> {
             try {
                 log.info("deepSeekGenerateContentAction deepSeekParam = {}", JSON.toJSONString(deepSeekParam));
                 Optional<String> optionalS = httpPoolClient.postJson(deepSeekApiUrl, JSON.toJSONString(deepSeekParam));
@@ -77,6 +79,6 @@ public class DeepSeekGenerateContentAction implements Action {
                 log.error("deepSeekGenerateContentAction error deepSeekParam = {}", deepSeekParam);
                 throw new RuntimeException("deepSeekGenerateContentAction request error", e);
             }
-        }, "deepSeekGenerateContentAction");
+        }, RETRY_TIMES, "deepSeekGenerateContentAction", i -> (long) (Math.pow(5, i) * 1000));
     }
 }

+ 1 - 1
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/GeminiGenerateContentAction.java

@@ -80,6 +80,6 @@ public class GeminiGenerateContentAction implements Action {
                 log.error("geminiGenerateContentAction error param = {}", JSON.toJSONString(geminiParam), e);
                 throw new RuntimeException("geminiGenerateContentAction request error", e);
             }
-        }, RETRY_TIMES, "geminiGenerateContentAction");
+        }, RETRY_TIMES, "geminiGenerateContentAction", i -> (long) (Math.pow(5, i) * 1000));
     }
 }

+ 26 - 1
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/util/RetryUtil.java

@@ -2,7 +2,9 @@ package com.tzld.piaoquan.content.understanding.util;
 
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.Objects;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 import java.util.function.LongSupplier;
 import java.util.function.Predicate;
 import java.util.function.Supplier;
@@ -21,7 +23,7 @@ public class RetryUtil {
      * @return
      */
     public static <T> T executeWithRetry(Supplier<T> action, String actionName) {
-        return executeWithRetry(action, MAX_RETRIES, actionName);
+        return executeWithRetry(action, MAX_RETRIES, actionName, null);
     }
 
     /**
@@ -34,12 +36,35 @@ public class RetryUtil {
      * @return
      */
     public static <T> T executeWithRetry(Supplier<T> action, Integer maxRetries, String actionName) {
+        return executeWithRetry(action, maxRetries, actionName, null);
+    }
+
+    /**
+     * 重试
+     * @param action
+     * @param maxRetries
+     * @param actionName
+     * @param retryIntervalProvider 毫秒
+     * @return
+     * @param <T>
+     */
+    public static <T> T executeWithRetry(Supplier<T> action, Integer maxRetries, String actionName, Function<Integer, Long> retryIntervalProvider) {
         for (int i = 1; i <= maxRetries; i++) {
             try {
                 return action.get();
             } catch (Exception e) {
                 log.error("{} failed on attempt {}: {}", actionName, i, e.getMessage(), e);
                 if (i < maxRetries) {
+                    if (Objects.nonNull(retryIntervalProvider) && Objects.nonNull(retryIntervalProvider.apply(i))) {
+                        long interval = retryIntervalProvider.apply(i);
+                        if (interval > 0) {
+                            try {
+                                TimeUnit.MILLISECONDS.sleep(interval);
+                            } catch (InterruptedException ex) {
+                                log.error("{} failed on attempt {}: {}", actionName, i, ex.getMessage(), ex);
+                            }
+                        }
+                    }
                     log.info("{} will be retried (attempt {}/{})", actionName, i + 1, maxRetries);
                 } else {
                     log.error("{} failed after {} retries due to non-IO error.", actionName, maxRetries);