From bdecda2eea331a170b5e9f5fd27e17ab59dd60c9 Mon Sep 17 00:00:00 2001 From: nange Date: Mon, 29 Apr 2024 17:45:37 +0800 Subject: [PATCH] optimize: connection timeout --- easyss.go | 16 +++++++++------- easyss_server.go | 4 ++++ relay.go | 4 ++-- remote.go | 4 ++-- remote_udp.go | 6 +++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/easyss.go b/easyss.go index 67497d7f..c693220f 100644 --- a/easyss.go +++ b/easyss.go @@ -48,10 +48,8 @@ const ( ) const ( - MaxCap int = 40 - MaxIdle int = 5 - IdleTime = time.Minute - MaxLifetime = 15 * time.Minute + MaxCap int = 50 + MaxIdle int = 5 ) const UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" @@ -511,8 +509,8 @@ func (ss *Easyss) InitTcpPool() error { InitialCap: MaxIdle, MaxCap: MaxCap, MaxIdle: MaxIdle, - Idletime: IdleTime, - MaxLifetime: MaxLifetime, + Idletime: ss.Timeout(), + MaxLifetime: ss.MaxLifeTime(), Factory: factory, } tcpPool, err := easypool.NewHeapPool(config) @@ -569,7 +567,7 @@ func (ss *Easyss) initHTTPOutboundClient() error { SetUserAgent(UserAgent) client. SetMaxIdleConns(MaxIdle). - SetIdleConnTimeout(MaxLifetime). + SetIdleConnTimeout(ss.MaxLifeTime()). SetMaxConnsPerHost(512). SetTLSHandshakeTimeout(ss.TLSTimeout()) client. @@ -705,6 +703,10 @@ func (ss *Easyss) LocalDeviceIndexV6() int { return ss.devIndexV6 } func (ss *Easyss) Timeout() time.Duration { return time.Duration(ss.currConfig.Timeout) * time.Second } +func (ss *Easyss) MaxLifeTime() time.Duration { + return 5 * ss.Timeout() +} + func (ss *Easyss) PingTimeout() time.Duration { timeout := ss.Timeout() / 5 if timeout < time.Second { diff --git a/easyss_server.go b/easyss_server.go index 32ba8321..518b7074 100644 --- a/easyss_server.go +++ b/easyss_server.go @@ -140,6 +140,10 @@ func (es *EasyServer) Timeout() time.Duration { return time.Duration(es.config.Timeout) * time.Second } +func (es *EasyServer) MaxConnWaitTimeout() time.Duration { + return 10 * es.Timeout() +} + func (es *EasyServer) CertPath() string { return es.config.CertPath } diff --git a/relay.go b/relay.go index fc06153b..d3ec894d 100644 --- a/relay.go +++ b/relay.go @@ -78,7 +78,7 @@ func copyCipherToPlainTxt(plainTxt, cipher net.Conn, timeout time.Duration, tryR err = errors.Join(err, ce) log.Warn("[REPAY] close write for plaintxt stream", "err", ce) } - if se := plainTxt.SetReadDeadline(time.Now().Add(5 * timeout)); se != nil { + if se := plainTxt.SetReadDeadline(time.Now().Add(3 * timeout)); se != nil { err = errors.Join(err, se) } @@ -109,7 +109,7 @@ func copyPlainTxtToCipher(cipher, plainTxt net.Conn, timeout time.Duration, tryR err = errors.Join(err, er) log.Warn("[REPAY] close write for cipher stream", "err", err) } - if er := cipher.SetReadDeadline(time.Now().Add(5 * timeout)); er != nil { + if er := cipher.SetReadDeadline(time.Now().Add(3 * timeout)); er != nil { err = errors.Join(err, er) } diff --git a/remote.go b/remote.go index e6b112a9..4c20099d 100644 --- a/remote.go +++ b/remote.go @@ -199,7 +199,7 @@ func (es *EasyServer) handShakeWithClient(conn net.Conn) (hsRes, error) { cs := csStream.(*cipherstream.CipherStream) defer cs.Release() - _ = csStream.SetDeadline(time.Now().Add(5 * es.Timeout())) + _ = csStream.SetDeadline(time.Now().Add(es.MaxConnWaitTimeout())) var frame *cipherstream.Frame for { @@ -208,7 +208,7 @@ func (es *EasyServer) handShakeWithClient(conn net.Conn) (hsRes, error) { return res, err } - _ = csStream.SetDeadline(time.Now().Add(5 * es.Timeout())) + _ = csStream.SetDeadline(time.Now().Add(es.MaxConnWaitTimeout())) if frame.IsPingFrame() { log.Debug("[REMOTE] got ping message", diff --git a/remote_udp.go b/remote_udp.go index eab87d20..761a2743 100644 --- a/remote_udp.go +++ b/remote_udp.go @@ -25,7 +25,7 @@ func (es *EasyServer) remoteUDPHandle(conn net.Conn, addrStr, method string, isD return fmt.Errorf("new cipherstream err:%v, method:%v", err, method) } - _ = csStream.SetDeadline(time.Now().Add(5 * es.Timeout())) + _ = csStream.SetDeadline(time.Now().Add(es.MaxConnWaitTimeout())) var _tryReuse bool @@ -62,7 +62,7 @@ func (es *EasyServer) remoteUDPHandle(conn net.Conn, addrStr, method string, isD log.Error("[REMOTE_UDP] write data to remote connection", "err", err) return } - _ = csStream.SetDeadline(time.Now().Add(5 * es.Timeout())) + _ = csStream.SetDeadline(time.Now().Add(es.MaxConnWaitTimeout())) } }() @@ -83,7 +83,7 @@ func (es *EasyServer) remoteUDPHandle(conn net.Conn, addrStr, method string, isD log.Error("[REMOTE_UDP] write data to tcp connection", "err", err) return } - _ = csStream.SetDeadline(time.Now().Add(5 * es.Timeout())) + _ = csStream.SetDeadline(time.Now().Add(es.MaxConnWaitTimeout())) } }()