chore(gopher): extract dial timeout constant
This commit is contained in:
@@ -34,6 +34,12 @@ const (
|
|||||||
typeSearch = '7' // index search
|
typeSearch = '7' // index search
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// dialTimeout caps the time spent establishing a TCP connection and
|
||||||
|
// waiting for the first response byte. Generous enough for slow Gopher
|
||||||
|
// servers, short enough that a stalled connection does not block the
|
||||||
|
// caller indefinitely.
|
||||||
|
const dialTimeout = 30 * time.Second
|
||||||
|
|
||||||
// Protocol implements gopher:// downloads (RFC 1436).
|
// Protocol implements gopher:// downloads (RFC 1436).
|
||||||
type Protocol struct {
|
type Protocol struct {
|
||||||
*protocol.BaseProtocol
|
*protocol.BaseProtocol
|
||||||
@@ -68,12 +74,12 @@ func (p *Protocol) Download(ctx context.Context, req *core.DownloadRequest) (*co
|
|||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(host, port)
|
addr := net.JoinHostPort(host, port)
|
||||||
conn, err := net.DialTimeout("tcp", addr, 30*time.Second)
|
conn, err := net.DialTimeout("tcp", addr, dialTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, core.NewNetworkError("failed to connect to gopher server", err, core.SafeURL(req.URL))
|
return nil, core.NewNetworkError("failed to connect to gopher server", err, core.SafeURL(req.URL))
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
conn.SetDeadline(time.Now().Add(30 * time.Second))
|
conn.SetDeadline(time.Now().Add(dialTimeout))
|
||||||
|
|
||||||
// Close the connection when the context is cancelled so any
|
// Close the connection when the context is cancelled so any
|
||||||
// blocking Read returns immediately instead of waiting for the
|
// blocking Read returns immediately instead of waiting for the
|
||||||
|
|||||||
Reference in New Issue
Block a user