Преглед изворни кода

fix 阶梯式 限制慢速时间 以及上限

chenbiao пре 2 година
родитељ
комит
91af3b8898

+ 40 - 18
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -561,31 +561,53 @@ public class MidjourneyServiceImpl implements MidjourneyService {
      * 用户生图次数限制
      */
     public void userSubmitLimit(MidjourneyUser user,String action,Object customId) {
-        if (StringUtils.equals("modal",action)) {
+        if (StringUtils.equals("modal", action) || (customId != null && customId.toString().contains("upsample"))) {
             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 {
-            Integer white = midjourneyWhiteMapper.selectCount(Wrappers.lambdaQuery(MidjourneyWhite.class).eq(MidjourneyWhite::getUserId, user.getId()));
-            int limit = white == 0 ? 200 : 400;
-            if (redisService.incr(key, 1L) > limit) {
-                //24小时内提交次数超过200次 拉黑
+
+        Long userId = user.getId();
+        String key = RedisService.key.MIDJOURNEY_USER_TIME.getName() + userId;
+        Long submitCount = (Long) redisService.get(key);
+
+        if (submitCount == null) {
+            redisService.set(key, 1L, RedisService.key.MIDJOURNEY_USER_TIME.getTimeout());
+            submitCount = 1L;
+        } else {
+            submitCount = redisService.incr(key, 1L);
+            int limit = 260;
+            Integer white = midjourneyWhiteMapper.selectCount(Wrappers.lambdaQuery(MidjourneyWhite.class).eq(MidjourneyWhite::getUserId, userId));
+            if (white > 0) {
+                limit = 400;
+            }
+            if (submitCount > limit) {
+                // 24小时内提交次数超过限制,拉黑用户
                 user.setIsBlack(true);
                 midjourneyUserMapper.updateById(user);
+                return;
             }
         }
 
-        //用户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);
+        // 根据提交次数设置提交间隔
+        long submitInterval;
+        if (submitCount >= 140) {
+            submitInterval = 100L + (long) (Math.random() * 21); // 140次后随机间隔100-120秒
+        } else if (submitCount >= 100) {
+            submitInterval = 60L; // 100次后60秒
+        } else if (submitCount >= 60) {
+            submitInterval = 40L; // 60次后40秒
+        } else if (submitCount >= 30) {
+            submitInterval = 30L; // 30次后30秒
+        } else {
+            submitInterval = 20L; // 默认20秒
+        }
+
+        // 用户只能在指定间隔内提交一次
+        String submitKey = RedisService.key.MIDJOURNEY_USER_SUBMIT.getName() + userId;
+        if (redisService.hasKey(submitKey)) {
+            long expire = redisService.getExpire(submitKey);
+            throw BusinessRuntimeException.getInstance("操作太快了,请等待" + expire + "秒后再试。为了防止爬虫,让大家有个良好的使用环境,relax模式命令最短时间间隔20-120秒,fast模式最短间隔5秒");
+        } else {
+            redisService.set(submitKey, 1L, submitInterval);
         }
     }