fs/http: declutter code #5596

This commit is contained in:
Ivan Andreev 2021-08-24 17:40:24 +03:00
parent 1d50336615
commit a32fde09ca
1 changed files with 20 additions and 32 deletions

View File

@ -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
}