mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-05-07 11:29:58 +08:00
update 桩端连接相关内容,解决频繁登录
This commit is contained in:
@@ -22,9 +22,17 @@ public class PileChannelEntity {
|
||||
|
||||
/**
|
||||
* 管理一个全局map,保存连接进服务端的通道数量
|
||||
* key: pileSn, value: ChannelHandlerContext
|
||||
*/
|
||||
private static final ConcurrentHashMap<String, ChannelHandlerContext> manager = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 反向映射:channelId -> pileSn
|
||||
* 优化查询性能,避免线性遍历 manager.entrySet()
|
||||
* 当连接数很多时,使用这个映射可以将 getPileSnByChannelId() 从 O(n) 优化到 O(1)
|
||||
*/
|
||||
private static final ConcurrentHashMap<String, String> channelIdToPileSnMap = new ConcurrentHashMap<>();
|
||||
|
||||
// 桩号--channelId 一对多
|
||||
public static final ConcurrentHashMap<String, List<ChannelHandlerContext>> pileMap = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -32,10 +40,14 @@ public class PileChannelEntity {
|
||||
* 校验channel是否保存
|
||||
*/
|
||||
public static void checkChannel(String pileSn, ChannelHandlerContext ctx) {
|
||||
String channelId = ctx.channel().id().asLongText();
|
||||
|
||||
if (!manager.containsKey(pileSn)) {
|
||||
// 如果manager中不存在pileSn的连接,则保存
|
||||
log.info("checkChannel-manager中不存在pileSn:{}的连接,保存新的channel:{}", pileSn, ctx.channel().id().asLongText());
|
||||
log.info("checkChannel-manager中不存在pileSn:{}的连接,保存新的channel:{}", pileSn, channelId);
|
||||
manager.put(pileSn, ctx);
|
||||
// 同步更新反向映射
|
||||
channelIdToPileSnMap.put(channelId, pileSn);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,18 +55,22 @@ public class PileChannelEntity {
|
||||
Channel sourceChannel = manager.get(pileSn).channel();
|
||||
if (sourceChannel == null) {
|
||||
// 为空就put
|
||||
log.info("checkChannel-manager中pileSn:{}的连接为空,保存新的channel:{}", pileSn, ctx.channel().id().asLongText());
|
||||
log.info("checkChannel-manager中pileSn:{}的连接为空,保存新的channel:{}", pileSn, channelId);
|
||||
manager.put(pileSn, ctx);
|
||||
// 同步更新反向映射
|
||||
channelIdToPileSnMap.put(channelId, pileSn);
|
||||
return;
|
||||
}
|
||||
|
||||
// 两个做对比
|
||||
String sourceChannelId = sourceChannel.id().asLongText();
|
||||
String channelId = ctx.channel().id().asLongText();
|
||||
if (!StringUtils.equals(sourceChannelId, channelId)) {
|
||||
// 不一致则更新
|
||||
log.info("checkChannel-manager中pileSn:{}的连接不一致, 老channelId:{}, 保存新的channel:{}", pileSn, sourceChannelId, channelId);
|
||||
manager.put(pileSn, ctx);
|
||||
// 移除旧的反向映射,添加新的
|
||||
channelIdToPileSnMap.remove(sourceChannelId);
|
||||
channelIdToPileSnMap.put(channelId, pileSn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,16 +118,12 @@ public class PileChannelEntity {
|
||||
|
||||
/**
|
||||
* 通过channelId获取桩编号
|
||||
* 优化:使用反向映射 channelIdToPileSnMap 实现 O(1) 查询
|
||||
* @param channelId
|
||||
* @return
|
||||
*/
|
||||
public static String getPileSnByChannelId(String channelId) {
|
||||
for (HashMap.Entry<String, ChannelHandlerContext> entry : manager.entrySet()) {
|
||||
if (entry.getValue().channel().id().asLongText().equals(channelId)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return channelIdToPileSnMap.get(channelId);
|
||||
}
|
||||
|
||||
// public static String getPileSnByCtx(ChannelHandlerContext ctx) {
|
||||
@@ -134,7 +146,11 @@ public class PileChannelEntity {
|
||||
if (StringUtils.isBlank(pileSn)) {
|
||||
return;
|
||||
}
|
||||
manager.remove(pileSn);
|
||||
ChannelHandlerContext ctx = manager.remove(pileSn);
|
||||
// 同步删除反向映射
|
||||
if (ctx != null) {
|
||||
channelIdToPileSnMap.remove(ctx.channel().id().asLongText());
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeByChannelId(String channelId){
|
||||
@@ -144,6 +160,8 @@ public class PileChannelEntity {
|
||||
String pileSn = getPileSnByChannelId(channelId);
|
||||
if (StringUtils.isNotBlank(pileSn)) {
|
||||
manager.remove(pileSn);
|
||||
// 同步删除反向映射
|
||||
channelIdToPileSnMap.remove(channelId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user