fs/http: declutter code #5596

This commit is contained in:
Ivan Andreev 2021-08-24 17:40:24 +03:00
parent 1d50336615
commit a32fde09ca

View file

@ -43,20 +43,20 @@ func NewDialer(ctx context.Context) *Dialer {
return 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) { func (d *Dialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address) return d.DialContext(context.Background(), network, address)
} }
var warnDSCPFail, warnDSCPWindows sync.Once var warnDSCPFail, warnDSCPWindows sync.Once
// DialContext connects to the address on the named network using // DialContext connects to the network address using the provided context.
// the provided context.
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
c, err := d.Dialer.DialContext(ctx, network, address) c, err := d.Dialer.DialContext(ctx, network, address)
if err != nil { if err != nil {
return c, err return c, err
} }
if d.tclass != 0 { if d.tclass != 0 {
// IPv6 addresses must have two or more ":" // IPv6 addresses must have two or more ":"
if strings.Count(c.RemoteAddr().String(), ":") > 1 { 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 { type timeoutConn struct {
net.Conn net.Conn
timeout time.Duration 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 // Nudge the deadline for an idle timeout on by c.timeout if non-zero
func (c *timeoutConn) nudgeDeadline() (err error) { func (c *timeoutConn) nudgeDeadline() error {
if c.timeout == 0 { if c.timeout > 0 {
return nil return c.SetDeadline(time.Now().Add(c.timeout))
} }
when := time.Now().Add(c.timeout) return nil
return c.Conn.SetDeadline(when)
} }
// Read bytes doing idle timeouts // Read bytes with rate limiting and idle timeouts
func (c *timeoutConn) Read(b []byte) (n int, err error) { func (c *timeoutConn) Read(b []byte) (n int, err error) {
// Ideally we would LimitBandwidth(len(b)) here and replace tokens we didn't use // Ideally we would LimitBandwidth(len(b)) here and replace tokens we didn't use
n, err = c.Conn.Read(b) n, err = c.Conn.Read(b)
accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportRx, n) accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportRx, n)
// Don't nudge if no bytes or an error if err == nil && n > 0 && c.timeout > 0 {
if n == 0 || err != nil { err = c.nudgeDeadline()
return
} }
// Nudge the deadline on successful Read or Write
err = c.nudgeDeadline()
return n, err 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) { func (c *timeoutConn) Write(b []byte) (n int, err error) {
accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportTx, len(b)) accounting.TokenBucket.LimitBandwidth(accounting.TokenBucketSlotTransportTx, len(b))
n, err = c.Conn.Write(b) n, err = c.Conn.Write(b)
// Don't nudge if no bytes or an error if err == nil && n > 0 && c.timeout > 0 {
if n == 0 || err != nil { err = c.nudgeDeadline()
return
} }
// Nudge the deadline on successful Read or Write
err = c.nudgeDeadline()
return n, err return n, err
} }