zyp 4 days ago
parent
commit
d63dbc63be

+ 21 - 14
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -1080,16 +1080,20 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 //        if (isWithin10Minutes){
 //            return R.error("非有效期内,不允许领取!");
 //        }
+        // 生成锁的key,基于用户ID和视频ID确保同一用户同一视频的请求被锁定
+        String lockKey = "reward_lock:user:" + param.getUserId() + ":video:" + param.getVideoId();
+        RLock lock = redissonClient.getLock(lockKey);
 
-        RLock lock = redissonClient.getLock("sendReward");
-
-        try{
-            boolean getLock = lock.tryLock(100,10000, TimeUnit.MILLISECONDS);
-            if (!getLock) {
-                return R.error("服务器繁忙,请稍候再试!");
+        try {
+            // 尝试获取锁,等待时间5秒,锁过期时间30秒
+            boolean isLocked = lock.tryLock(5, 60, TimeUnit.SECONDS);
+            if (!isLocked) {
+                logger.warn("获取锁失败,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
+                return R.error("操作频繁,请稍后再试!");
             }
-            // 获取用户信息
 
+            logger.info("成功获取锁,开始处理奖励发放,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
+            // 获取用户信息
             FsUser user = fsUserMapper.selectFsUserByUserId(param.getUserId());
 //        if (StringUtils.isEmpty(user.getMpOpenId())){
 //            return R.error("未识别到领取信息");
@@ -1155,14 +1159,17 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
                 default:
                     return R.error("参数错误!");
             }
+//            return executeSendRewardBusiness(param);
 
-        }
-        catch (Exception e){
-            throw new CustomException(e.getMessage());
-        }
-        finally {
-            if(null != lock && lock.isLocked() && lock.isHeldByCurrentThread()){
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            logger.error("获取锁被中断,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId(), e);
+            return R.error("系统繁忙,请重试!");
+        } finally {
+            // 释放锁
+            if (lock.isHeldByCurrentThread()) {
                 lock.unlock();
+                logger.info("释放锁成功,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
             }
         }
 
@@ -1199,7 +1206,7 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 
         try {
             // 尝试获取锁,等待时间5秒,锁过期时间30秒
-            boolean isLocked = lock.tryLock(5, 30, TimeUnit.SECONDS);
+            boolean isLocked = lock.tryLock(5, 60, TimeUnit.SECONDS);
             if (!isLocked) {
                 logger.warn("获取锁失败,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
                 return R.error("操作频繁,请稍后再试!");