From 9f23420ffed264e55bdcd053559b89097abcd266 Mon Sep 17 00:00:00 2001 From: qifanwang Date: Thu, 26 Dec 2024 14:17:35 +0800 Subject: [PATCH] proxy support stop --- .../xpipe/redis/proxy/DefaultProxyServer.java | 1 + .../proxy/controller/ProxyController.java | 25 +++++++++++++++++++ .../proxy/tunnel/DefaultTunnelManager.java | 9 +++++++ .../redis/proxy/tunnel/TunnelManager.java | 2 ++ 4 files changed, 37 insertions(+) diff --git a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/DefaultProxyServer.java b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/DefaultProxyServer.java index 82b75a0485..90102e6090 100644 --- a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/DefaultProxyServer.java +++ b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/DefaultProxyServer.java @@ -144,6 +144,7 @@ public void stop() { if(tlsFuture != null) { tlsFuture.channel().close(); } + tunnelManager.removeAll(); } diff --git a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/controller/ProxyController.java b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/controller/ProxyController.java index 13a628b2b1..bbacf405ce 100644 --- a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/controller/ProxyController.java +++ b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/controller/ProxyController.java @@ -1,6 +1,7 @@ package com.ctrip.xpipe.redis.proxy.controller; import com.ctrip.xpipe.codec.JsonCodec; +import com.ctrip.xpipe.redis.proxy.ProxyServer; import com.ctrip.xpipe.redis.proxy.Tunnel; import com.ctrip.xpipe.redis.proxy.model.TunnelMeta; import com.ctrip.xpipe.redis.proxy.monitor.TunnelMonitorManager; @@ -30,6 +31,30 @@ public class ProxyController { @Autowired private TunnelMonitorManager tunnelMonitorManager; + @Autowired + private ProxyServer proxyServer; + + @PostMapping(value = "/server/start") + public RetMessage startServer() { + try { + proxyServer.start(); + return RetMessage.createSuccessMessage(); + } catch (Throwable th) { + return RetMessage.createFailMessage(th.getMessage()); + } + } + + @PostMapping(value = "/server/stop") + public RetMessage stopServer() { + try { + proxyServer.stop(); + return RetMessage.createSuccessMessage(); + } catch (Throwable th) { + return RetMessage.createFailMessage(th.getMessage()); + } + } + + @RequestMapping(value = "/tunnels", method = RequestMethod.GET) public String getTunnelMetas() { List tunnels = tunnelManager.tunnels(); diff --git a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/DefaultTunnelManager.java b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/DefaultTunnelManager.java index 305161af07..97af5aea83 100644 --- a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/DefaultTunnelManager.java +++ b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/DefaultTunnelManager.java @@ -27,6 +27,7 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -154,6 +155,14 @@ public void remove(Channel frontendChannel) { } } + @Override + public void removeAll() { + Set toCloseChannels = new HashSet<>(cache.keySet()); + for (Channel channel: toCloseChannels) { + remove(channel); + } + } + @Override public List tunnels() { return Lists.newArrayList(cache.values()); diff --git a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/TunnelManager.java b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/TunnelManager.java index 11521aa791..8455959513 100644 --- a/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/TunnelManager.java +++ b/redis/redis-proxy/src/main/java/com/ctrip/xpipe/redis/proxy/tunnel/TunnelManager.java @@ -19,6 +19,8 @@ public interface TunnelManager extends Releasable, Observer { void remove(Channel frontendChannel); + void removeAll(); + List tunnels(); Tunnel getById(String id);