From 1d26ea440b50d6c1a3b288a9b46e1000221496a2 Mon Sep 17 00:00:00 2001 From: Blizard Date: Tue, 19 Oct 2021 14:57:26 +0800 Subject: [PATCH] fix: kcp protocol cause delay release resource (#2621) Co-authored-by: blizard863 <760076784@qq.com> --- client/control.go | 9 ++++++++- client/service.go | 8 ++++++-- cmd/frpc/sub/root.go | 3 +-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/client/control.go b/client/control.go index 50cca40..9bf3109 100644 --- a/client/control.go +++ b/client/control.go @@ -182,9 +182,16 @@ func (ctl *Control) HandleNewProxyResp(inMsg *msg.NewProxyResp) { } func (ctl *Control) Close() error { + return ctl.GracefulClose(0) +} + +func (ctl *Control) GracefulClose(t time.Duration) error { ctl.pm.Close() - ctl.conn.Close() ctl.vm.Close() + + time.Sleep(t) + + ctl.conn.Close() if ctl.session != nil { ctl.session.Close() } diff --git a/client/service.go b/client/service.go index b31e07b..4af6e07 100644 --- a/client/service.go +++ b/client/service.go @@ -311,10 +311,14 @@ func (svr *Service) ReloadConf(pxyCfgs map[string]config.ProxyConf, visitorCfgs return svr.ctl.ReloadConf(pxyCfgs, visitorCfgs) } -func (svr *Service) Close() { +func (svr *Service) Close(t time.Duration) { atomic.StoreUint32(&svr.exit, 1) if svr.ctl != nil { - svr.ctl.Close() + if t > 0 { + svr.ctl.GracefulClose(t) + } else { + svr.ctl.Close() + } } svr.cancel() } diff --git a/cmd/frpc/sub/root.go b/cmd/frpc/sub/root.go index 2c1b3e3..3c56cea 100644 --- a/cmd/frpc/sub/root.go +++ b/cmd/frpc/sub/root.go @@ -124,8 +124,7 @@ func handleSignal(svr *client.Service) { ch := make(chan os.Signal) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) <-ch - svr.Close() - time.Sleep(250 * time.Millisecond) + svr.Close(time.Millisecond * 250) close(kcpDoneCh) }