dynamically adjust connection timeout

better fix for https://github.com/chrislusf/seaweedfs/issues/2541
This commit is contained in:
chrislu 2021-12-29 22:44:39 -08:00
parent 5788bf2270
commit fb434318e3
2 changed files with 7 additions and 3 deletions

View file

@ -192,7 +192,7 @@ func (s3opt *S3Options) startS3Server() bool {
httpS := &http.Server{Handler: router} httpS := &http.Server{Handler: router}
listenAddress := fmt.Sprintf("%s:%d", *s3opt.bindIp, *s3opt.port) listenAddress := fmt.Sprintf("%s:%d", *s3opt.bindIp, *s3opt.port)
s3ApiListener, err := util.NewListener(listenAddress, time.Duration(30)*time.Second) s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
if err != nil { if err != nil {
glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err) glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
} }

View file

@ -36,11 +36,13 @@ type Conn struct {
ReadTimeout time.Duration ReadTimeout time.Duration
WriteTimeout time.Duration WriteTimeout time.Duration
isClosed bool isClosed bool
bytesRead int64
bytesWritten int64
} }
func (c *Conn) Read(b []byte) (count int, e error) { func (c *Conn) Read(b []byte) (count int, e error) {
if c.ReadTimeout != 0 { if c.ReadTimeout != 0 {
err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout * time.Duration(c.bytesRead/40000+1)))
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -48,6 +50,7 @@ func (c *Conn) Read(b []byte) (count int, e error) {
count, e = c.Conn.Read(b) count, e = c.Conn.Read(b)
if e == nil { if e == nil {
stats.BytesIn(int64(count)) stats.BytesIn(int64(count))
c.bytesRead += int64(count)
} }
return return
} }
@ -55,7 +58,7 @@ func (c *Conn) Read(b []byte) (count int, e error) {
func (c *Conn) Write(b []byte) (count int, e error) { func (c *Conn) Write(b []byte) (count int, e error) {
if c.WriteTimeout != 0 { if c.WriteTimeout != 0 {
// minimum 4KB/s // minimum 4KB/s
err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout * time.Duration(len(b)/40000+1))) err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout * time.Duration(c.bytesWritten/40000+1)))
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -63,6 +66,7 @@ func (c *Conn) Write(b []byte) (count int, e error) {
count, e = c.Conn.Write(b) count, e = c.Conn.Write(b)
if e == nil { if e == nil {
stats.BytesOut(int64(count)) stats.BytesOut(int64(count))
c.bytesWritten += int64(count)
} }
return return
} }