rpc: support dial http with context

This commit is contained in:
Trung Nguyen 2020-01-08 10:16:20 -05:00
parent 4d663d57d6
commit c38bdd3593
No known key found for this signature in database
GPG key ID: 4636434ED9505EB7
2 changed files with 5 additions and 6 deletions

View file

@ -173,7 +173,7 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) {
} }
switch u.Scheme { switch u.Scheme {
case "http", "https": case "http", "https":
return DialHTTP(rawurl) return DialHTTP(ctx, rawurl)
case "ws", "wss": case "ws", "wss":
return DialWebsocket(ctx, rawurl, "") return DialWebsocket(ctx, rawurl, "")
case "stdio": case "stdio":

View file

@ -106,7 +106,7 @@ var DefaultHTTPTimeouts = HTTPTimeouts{
// DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP // DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP
// using the provided HTTP Client. // using the provided HTTP Client.
func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) { func DialHTTPWithClient(ctx context.Context, endpoint string, client *http.Client) (*Client, error) {
req, err := http.NewRequest(http.MethodPost, endpoint, nil) req, err := http.NewRequest(http.MethodPost, endpoint, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -114,15 +114,14 @@ func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) {
req.Header.Set("Content-Type", contentType) req.Header.Set("Content-Type", contentType)
req.Header.Set("Accept", contentType) req.Header.Set("Accept", contentType)
initctx := context.Background() return newClient(ctx, func(context.Context) (ServerCodec, error) {
return newClient(initctx, func(context.Context) (ServerCodec, error) {
return &httpConn{client: client, req: req, closeCh: make(chan interface{})}, nil return &httpConn{client: client, req: req, closeCh: make(chan interface{})}, nil
}) })
} }
// DialHTTP creates a new RPC client that connects to an RPC server over HTTP. // DialHTTP creates a new RPC client that connects to an RPC server over HTTP.
func DialHTTP(endpoint string) (*Client, error) { func DialHTTP(ctx context.Context, endpoint string) (*Client, error) {
return DialHTTPWithClient(endpoint, new(http.Client)) return DialHTTPWithClient(ctx, endpoint, new(http.Client))
} }
func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error { func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {