Add context to requests

This commit is contained in:
Adnan Maolood 2020-11-26 00:42:25 -05:00
parent 3b9cc7f168
commit 82688746dd
2 changed files with 10 additions and 3 deletions

View File

@ -96,10 +96,11 @@ func (c *Client) do(req *Request, via []*Request) (*Response, error) {
return c.verifyConnection(req, cs) return c.verifyConnection(req, cs)
}, },
} }
conn, err := tls.Dial("tcp", req.Host, config) netConn, err := (&net.Dialer{}).DialContext(req.Context, "tcp", req.Host)
if err != nil { if err != nil {
return nil, err return nil, err
} }
conn := tls.Client(netConn, config)
// Set connection deadline // Set connection deadline
if d := c.Timeout; d != 0 { if d := c.Timeout; d != 0 {
conn.SetDeadline(time.Now().Add(d)) conn.SetDeadline(time.Now().Add(d))

View File

@ -2,6 +2,7 @@ package gemini
import ( import (
"bufio" "bufio"
"context"
"crypto/tls" "crypto/tls"
"net" "net"
"net/url" "net/url"
@ -33,6 +34,10 @@ type Request struct {
// connection on which the request was received. // connection on which the request was received.
// This field is ignored by the client. // This field is ignored by the client.
TLS tls.ConnectionState TLS tls.ConnectionState
// Context specifies the context to use for client requests.
// Context must not be nil.
Context context.Context
} }
// NewRequest returns a new request. The host is inferred from the URL. // NewRequest returns a new request. The host is inferred from the URL.
@ -52,8 +57,9 @@ func NewRequestFromURL(url *url.URL) *Request {
host += ":1965" host += ":1965"
} }
return &Request{ return &Request{
URL: url, URL: url,
Host: host, Host: host,
Context: context.Background(),
} }
} }