From 1eb5e2d002cba8367ffc1d0d5b3f330d75119f87 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Tue, 9 Jan 2018 12:35:20 +0100 Subject: [PATCH] rpc: Support specifying HTTP client in RPC dialing Adds a minimal interface that captures http.Client and adds a new method rpc.DialHTTPClient that takes a client using that interface. The existing rpc.DialHTTP method is then alternatively implemented by using the new rpc.DialHTTPClient method provided with a standard *http.Client. --- rpc/http.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rpc/http.go b/rpc/http.go index 4143e2a8d..ceb986d46 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -62,9 +62,10 @@ func (hc *httpConn) Close() error { return nil } -// DialHTTP creates a new RPC clients that connection to an RPC server over HTTP. -func DialHTTP(endpoint string) (*Client, error) { - req, err := http.NewRequest("POST", endpoint, nil) +// DialHTTPWithClient creates a new RPC clients that connection to an RPC server over HTTP +// using the provided HTTP Client. +func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) { + req, err := http.NewRequest(http.MethodPost, endpoint, nil) if err != nil { return nil, err } @@ -73,10 +74,15 @@ func DialHTTP(endpoint string) (*Client, error) { initctx := context.Background() return newClient(initctx, func(context.Context) (net.Conn, error) { - return &httpConn{client: new(http.Client), req: req, closed: make(chan struct{})}, nil + return &httpConn{client: client, req: req, closed: make(chan struct{})}, nil }) } +// DialHTTP creates a new RPC clients that connection to an RPC server over HTTP. +func DialHTTP(endpoint string) (*Client, error) { + return DialHTTPWithClient(endpoint, new(http.Client)) +} + func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error { hc := c.writeConn.(*httpConn) respBody, err := hc.doRequest(ctx, msg)