refactor(live): 清理心跳与上麦逻辑中的死代码和无效依赖

LiveMicGoUpCmdExe:
- 删除遗留的 main 方法和 createAgoraTokenGoUp 死方法(后者每次调用都 new 线程池,有资源泄漏风险)
- handleRoomDailyTask 提前短路:非邀请类型或非房主邀请时直接 return,避免无效 HTTP 调用
- roomMemberDTO 查询后移到确认需要时才执行

UserHeartbeatListener:
- 移除 userProfileClient 无效注入(逻辑已注释)
- 删除 getRoomProfileDto 死方法(上次重构后已无调用)
- 清理 StringUtils/RegexConstant/Profile/ResultResponse/RedisTemplate 等无用 import

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
(cherry picked from commit 6f210e34c83a10721ea5f772f5951ed0547c8e46)
This commit is contained in:
tianfeng 2026-04-07 18:07:13 +08:00
parent 4eb2f8330d
commit e36a28cf85
2 changed files with 8 additions and 88 deletions

View File

@ -27,8 +27,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.TimeUnit;
/**
* 上麦克风.
@ -77,20 +76,21 @@ public class LiveMicGoUpCmdExe {
private void handleRoomDailyTask(MicUpDownCmd cmd) {
try {
if (!cmd.eventTypeEqInvite()) {
return;
}
RoomProfileDTO roomProfile = roomManagerClient.getById(cmd.getRoomId()).getBody();
if (Objects.isNull(roomProfile)) {
return;
}
if (!Objects.equals(roomProfile.getUserId(), cmd.getInviterId())) {
return;
}
RoomMemberDTO roomMemberDTO = roomMemberClient.getMember(cmd.getRoomId(), cmd.getReqUserId()).getBody();
if (Objects.isNull(roomMemberDTO)) {
return;
}
boolean invite = cmd.eventTypeEqInvite();
if (invite && Objects.equals(roomProfile.getUserId(), cmd.getInviterId())) {
Long roomOwnerId = roomProfile.getUserId();
handleInviteMicTask(roomOwnerId, cmd.requiredReqUserId(), RoomDailyTaskCode.ROOM_OWNER_INVITE_MIC);
}
handleInviteMicTask(roomProfile.getUserId(), cmd.requiredReqUserId(), RoomDailyTaskCode.ROOM_OWNER_INVITE_MIC);
} catch (Exception exception){
log.error("handleRoomDailyTask exception: {}", exception.getMessage());
}
@ -126,60 +126,4 @@ public class LiveMicGoUpCmdExe {
}
}
/**
* 异步拿声网token5分钟拿不到就返回默认一串字符串
* @param cmd
* @return
*/
private String createAgoraTokenGoUp(MicUpDownCmd cmd) {
ExecutorService executor = Executors.newSingleThreadExecutor();
AtomicReference<String> agoraToken = new AtomicReference<>("BaHrIypJdFWqwWtoZLToRucRyoGrVgYCONBFvGynmlJsevFYsWBfEIcaWUUGijQBjycTtmSvkmBINoSltJMmgiPeBnkaWErixaxg");
Future<?> future = executor.submit(() -> {
agoraToken.set(trtcClient.createAgoraTokenGoUp(cmd.getReqUserId(), cmd.getRoomId().toString()).getBody());
});
try {
future.get(3, TimeUnit.SECONDS); // 等待最多3秒
} catch (TimeoutException e) {
// 处理超时
log.warn("上麦获取声网token超时: {}", e.getMessage());
} catch (InterruptedException | ExecutionException e) {
// 处理其他异常
log.warn("上麦获取声网token其他异常: {}", e.getMessage());
}
executor.shutdown();
return agoraToken.get();
}
public static void main(String[] args) {
System.out.println("开始等待...");
ExecutorService executor = Executors.newSingleThreadExecutor();
AtomicReference<String> agoraToken = new AtomicReference<>("");
Future<?> future = executor.submit(() -> {
// 等待6秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
agoraToken.set("拿到值啦!!!!!");
});
try {
future.get(5, TimeUnit.SECONDS); // 等待最多3秒
} catch (TimeoutException e) {
// 处理超时
log.warn("上麦获取声网token超时: {}", e);
} catch (InterruptedException | ExecutionException e) {
// 处理其他异常
log.warn("上麦获取声网token其他异常: {}", e);
}
executor.shutdown();
System.out.println(agoraToken.get());
System.out.println("结束...");
}
}

View File

@ -7,7 +7,6 @@ import com.red.circle.component.mq.service.Action;
import com.red.circle.component.mq.service.ConsumerMessage;
import com.red.circle.component.mq.service.MessageListener;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.live.app.dto.cmd.HeartbeatStatusEnum;
import com.red.circle.live.domain.gateway.LiveMicrophoneGateway;
import com.red.circle.live.domain.live.LiveMicrophone;
@ -23,7 +22,6 @@ import com.red.circle.other.inner.endpoint.live.RoomManagerClient;
import com.red.circle.other.inner.endpoint.live.RoomMemberClient;
import com.red.circle.other.inner.endpoint.task.RoomDailyTaskClient;
import com.red.circle.other.inner.endpoint.user.user.UserOnlineClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.enums.live.RoomUserRolesEnum;
import com.red.circle.other.inner.enums.task.RoomDailyTaskCode;
import com.red.circle.other.inner.model.cmd.live.MemberRolesQryCmd;
@ -35,9 +33,7 @@ import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
import com.red.circle.tool.core.parse.DataTypeUtils;
import com.red.circle.tool.core.regex.RegexConstant;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -47,8 +43,6 @@ import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
@ -63,7 +57,6 @@ import org.springframework.stereotype.Component;
public class UserHeartbeatListener implements MessageListener {
private final UserOnlineClient userOnlineClient;
private final UserProfileClient userProfileClient;
private final RoomManagerClient roomManagerClient;
private final LiveMicCacheService liveMicCacheService;
private final LiveMicrophoneGateway liveMicrophoneGateway;
@ -207,23 +200,6 @@ public class UserHeartbeatListener implements MessageListener {
refreshMicActiveTime(event, roomProfile, userProfile);
}
private RoomProfileDTO getRoomProfileDto(Long roomId) {
String roomAccount = liveRoomCacheService.getRoomAccount(roomId);
if (Objects.isNull(roomAccount)) {
RoomProfileDTO roomProfile = roomManagerClient.getById(roomId).getBody();
if (Objects.nonNull(roomProfile)) {
liveRoomCacheService.save(roomProfile.getRoomAccount(), roomId);
roomAccount = roomProfile.getRoomAccount();
}
}
RoomProfileDTO roomProfile = new RoomProfileDTO();
roomProfile.setId(roomId);
roomProfile.setRoomAccount(roomAccount);
return roomProfile;
}
private void addRoomOnlineUser(UserProfileDTO userProfile, RoomProfileDTO roomProfile) {
liveMicrophoneGateway.addRoomOnlineUser(roomProfile.getId(),