Skip to content

Commit

Permalink
rpc: Support specifying HTTP client in RPC dialing (ethereum#15836)
Browse files Browse the repository at this point in the history
* 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: fix minor doc typos
  • Loading branch information
stevenroose authored and karalabe committed Jan 24, 2018
1 parent 5c83a4e commit 952482d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ 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) {
// DialHTTPWithClient creates a new RPC client that connects 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
Expand All @@ -76,10 +77,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 client that connects 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)
Expand Down

0 comments on commit 952482d

Please sign in to comment.