소스 검색

fix:解决内存泄漏问题

李亚非 2 달 전
부모
커밋
3d469535cc

+ 10 - 4
alien-lawyer/src/main/java/shop/alien/lawyer/config/WebSocketProcess.java

@@ -157,6 +157,8 @@ public class WebSocketProcess implements ApplicationContextAware {
         try {
             //每新建立一个连接,就把当前客户id为key,this为value存储到map中
             this.session = session;
+            // 设置 5 分钟没有任何消息往来则自动关闭连接,防止长连接堆积
+            this.session.setMaxIdleTimeout(5 * 60 * 1000);
             concurrentHashMap.put(id, this);
             log.info("WebSocketProcess.onOpen() Open a websocket. id={}", id);
 
@@ -177,10 +179,12 @@ public class WebSocketProcess implements ApplicationContextAware {
     @OnClose
     public void onClose(Session session, @PathParam("sendId") String id) {
         try {
-            //客户端连接关闭时,移除map中存储的键值对
-            concurrentHashMap.remove(id);
-            log.info("WebSocketProcess.onClose() close a websocket, concurrentHashMap remove sessionId= {}", id);
-            if (baseRedisService.hasKey("blackList_" + id)) baseRedisService.delete("blackList_" + id);
+            //客户端连接关闭时,安全移除map中存储的键值对
+            boolean removed = concurrentHashMap.remove(id, this);
+            if (removed) {
+                log.info("WebSocketProcess.onClose() close a websocket, concurrentHashMap remove sessionId= {}", id);
+                if (baseRedisService.hasKey("blackList_" + id)) baseRedisService.delete("blackList_" + id);
+            }
         } catch (Exception e) {
             log.error("WebSocketProcess.onClose()----Exception----Message={}", e.getMessage());
         }
@@ -193,6 +197,8 @@ public class WebSocketProcess implements ApplicationContextAware {
     public void onError(@PathParam("sendId") String id, Throwable error) {
         try {
             log.error("WebSocketProcess.onError() Error,id={}, Msg=", id, error);
+            // 发生错误时主动移除,防止僵尸连接
+            concurrentHashMap.remove(id, this);
         } catch (Exception e) {
             log.error("WebSocketProcess.onError()----Exception----Message={}", e.getMessage());
         }

+ 12 - 4
alien-store/src/main/java/shop/alien/store/config/WebSocketProcess.java

@@ -210,6 +210,8 @@ public class WebSocketProcess implements ApplicationContextAware {
         try {
             //每新建立一个连接,就把当前客户id为key,this为value存储到map中
             this.session = session;
+            // 设置 5 分钟没有任何消息往来则自动关闭连接,防止长连接堆积
+            this.session.setMaxIdleTimeout(5 * 60 * 1000);
             concurrentHashMap.put(id, this);
             
             // 从Session的UserProperties中获取IP地址和User-Agent(由WebSocketConfigurator在握手时设置)
@@ -242,10 +244,13 @@ public class WebSocketProcess implements ApplicationContextAware {
     @OnClose
     public void onClose(Session session, @PathParam("sendId") String id) {
         try {
-            //客户端连接关闭时,移除map中存储的键值对
-            concurrentHashMap.remove(id);
-            log.info("WebSocketProcess.onClose() close a websocket, concurrentHashMap remove sessionId= {}", id);
-            if (baseRedisService.hasKey("blackList_" + id)) baseRedisService.delete("blackList_" + id);
+            //客户端连接关闭时,安全移除map中存储的键值对
+            boolean removed = concurrentHashMap.remove(id, this);
+            if (removed) {
+                log.info("WebSocketProcess.onClose() close a websocket, concurrentHashMap remove sessionId= {}", id);
+                if (baseRedisService.hasKey("blackList_" + id)) baseRedisService.delete("blackList_" + id);
+                connectionInfoMap.remove(id);
+            }
         } catch (Exception e) {
             log.error("WebSocketProcess.onClose()----Exception----Message={}", e.getMessage());
         }
@@ -258,6 +263,9 @@ public class WebSocketProcess implements ApplicationContextAware {
     public void onError(@PathParam("sendId") String id, Throwable error) {
         try {
             log.error("WebSocketProcess.onError() Error,id={}, Msg=", id, error);
+            // 发生错误时主动移除,防止僵尸连接
+            concurrentHashMap.remove(id, this);
+            connectionInfoMap.remove(id);
         } catch (Exception e) {
             log.error("WebSocketProcess.onError()----Exception----Message={}", e.getMessage());
         }