ソースを参照

fix 慢速限制

zwhui 2 年 前
コミット
bf17010288

+ 1 - 0
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -846,6 +846,7 @@ public class RedisService {
         MIDJOURNEY_ACCOUNT_USED("midjourney:account:used:", "midjourney account used", 60 * 60 * 48L),
         APPLY_MONEY_DAY_LIMIT_KEY("xxxx", "xxxxx", 60 * 60 * 48L),
         MIDJOURNEY_USER_TIME("midjourney:user:time:", "midjourney user time", 60 * 60 * 24L),
+        MIDJOURNEY_USER_SUBMIT("midjourney:user:submit:", "midjourney user submit", 30L),
         ;
 
         private String name;

+ 31 - 12
netflix-service/src/main/java/com/cyksj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -272,18 +272,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             param.put("accountFilter",MapUtil.builder(new HashMap<String,Object>())
                     .put("instanceId",accountWithMinUsage).build());
             Object customId = param.get("customId");
-            if (customId != null && !customId.toString().contains("upsample") && !action.equals("modal")){
-                String key = RedisService.key.MIDJOURNEY_USER_TIME.getName() + user.getId();
-                Object count = redisService.get(key);
-                if (count == null) {
-                    redisService.set(key, 1L,RedisService.key.MIDJOURNEY_USER_TIME.getTimeout());
-                }else {
-                    if (redisService.incr(key, 1L) > 200) {
-                        user.setIsBlack(true);
-                        midjourneyUserMapper.updateById(user);
-                    }
-                }
-            }
+            userSubmitLimit(user,action,customId);
         }
         url = url + getActionUrl(action);
         param.put("notifyHook",midjourneyHost +(EnvCommonService.active.equals(envCommonService.getEnv()) ? "/8081":"/8082") + "/api/applets/midjourney/notifyHook");
@@ -367,6 +356,36 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             default:  throw new IllegalArgumentException("Unknown action: " + action);
         }
     }
+
+    /**
+     * 用户生图次数限制
+     */
+    public void userSubmitLimit(MidjourneyUser user,String action,Object customId) {
+        if (StringUtils.equals("modal",action)) {
+            return;
+        }
+        if (customId != null && customId.toString().contains("upsample")) {
+            return;
+        }
+        String key = RedisService.key.MIDJOURNEY_USER_TIME.getName() + user.getId();
+        Object count = redisService.get(key);
+        if (count == null) {
+            redisService.set(key, 1L,RedisService.key.MIDJOURNEY_USER_TIME.getTimeout());
+        }else {
+            if (redisService.incr(key, 1L) > 200) {
+                //24小时内提交次数超过200次 拉黑
+                user.setIsBlack(true);
+                midjourneyUserMapper.updateById(user);
+            }
+        }
+
+        //用户30秒内只能提交一次
+        if (redisService.hasKey(RedisService.key.MIDJOURNEY_USER_SUBMIT.getName() + user.getId())) {
+            throw BusinessRuntimeException.getInstance("操作太快了,请等待"+redisService.getExpire(RedisService.key.MIDJOURNEY_USER_SUBMIT.getName() + user.getId())+"秒后再试.为了防止爬虫,让大家有个良好的使用环境,relax模式命令最短时间间隔30秒,fast模式最短间隔5秒");
+        }else {
+            redisService.set(RedisService.key.MIDJOURNEY_USER_SUBMIT.getName() + user.getId(), 1L, 30L);
+        }
+    }
     @Override
     public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<Long> ids) {
         List<MidjourneyUserConversation> list = new ArrayList<>();