From a32fde09ca0e32109fa71d0f6d26dcc6a76ab95c Mon Sep 17 00:00:00 2001 From: Ivan Andreev Date: Tue, 24 Aug 2021 17:40:24 +0300 Subject: [PATCH] fs/http: declutter code #5596 --- fs/fshttp/dialer.go | 52 +++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/fs/fshttp/dialer.go b/fs/fshttp/dialer.go index 261baeaa6..adeece185 100644 --- a/fs/fshttp/dialer.go +++ b/fs/fshttp/dialer.go @@ -43,20 +43,20 @@ func NewDialer(ctx context.Context) *Dialer { return dialer } -// Dial connects to the address on the named network. +// Dial connects to the network address. func (d *Dialer) Dial(network, address string) (net.Conn, error) { return d.DialContext(context.Background(), network, address) } var warnDSCPFail, warnDSCPWindows sync.Once -// DialContext connects to the address on the named network using -// the provided context. +// DialContext connects to the network address using the provided context. func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { c, err := d.Dialer.DialContext(ctx, network, address) if err != nil { return c, err } + if d.tclass != 0 { // IPv6 addresses must have two or more ":" if strings.Count(c.RemoteAddr().String(), ":") > 1 { @@ -76,57 +76,45 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net. }) } } - return newTimeoutConn(c, d.timeout) + + t := &timeoutConn{ + Conn: c, + timeout: d.timeout, + } + return t, t.nudgeDeadline() } -// A net.Conn that sets a deadline for every Read or Write operation +// A net.Conn that sets deadline for every Read/Write operation type timeoutConn struct { net.Conn timeout time.Duration } -// create a timeoutConn using the timeout -func newTimeoutConn(conn net.Conn, timeout time.Duration) (c *timeoutConn, err error) { - c = &timeoutConn{ - Conn: conn, - timeout: timeout, - } - err = c.nudgeDeadline() - return -} - // Nudge the deadline for an idle timeout on by c.timeout if non-zero -func (c *timeoutConn) nudgeDeadline() (err error) { - if c.timeout == 0 { - return nil +func (c *timeoutConn) nudgeDeadline() error { + if c.timeout > 0 { + return c.SetDeadline(time.Now().Add(c.timeout)) } - when := time.Now().Add(c.timeout) - return c.Conn.SetDeadline(when) + return nil } -// Read bytes doing idle timeouts +// Read bytes with rate limiting and idle timeouts func (c *timeoutConn) Read(b []byte) (n int, err error) { // Ideally we would LimitBandwidth(len(b)) here and replace tokens we didn't use n, err = c.Conn.Read(b) accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportRx, n) - // Don't nudge if no bytes or an error - if n == 0 || err != nil { - return + if err == nil && n > 0 && c.timeout > 0 { + err = c.nudgeDeadline() } - // Nudge the deadline on successful Read or Write - err = c.nudgeDeadline() return n, err } -// Write bytes doing idle timeouts +// Write bytes with rate limiting and idle timeouts func (c *timeoutConn) Write(b []byte) (n int, err error) { accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportTx, len(b)) n, err = c.Conn.Write(b) - // Don't nudge if no bytes or an error - if n == 0 || err != nil { - return + if err == nil && n > 0 && c.timeout > 0 { + err = c.nudgeDeadline() } - // Nudge the deadline on successful Read or Write - err = c.nudgeDeadline() return n, err }