Skip to content

Commit

Permalink
optimize: connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Apr 29, 2024
1 parent e77a44f commit bdecda2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
16 changes: 9 additions & 7 deletions easyss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions easyss_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions remote_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()))
}
}()

Expand All @@ -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()))
}
}()

Expand Down

0 comments on commit bdecda2

Please sign in to comment.