aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorSteven Roose <stevenroose@gmail.com>2018-01-24 16:59:15 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-01-24 16:59:15 +0800
commit952482d5e47c390afbd04230c43b1df8c53d84ef (patch)
treee2bbc61d4eea3bfad94b80392c16af61ceee6961 /rpc
parent5c83a4e5dd600fcf19502f7fb083dc0b6393f969 (diff)
downloaddexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar.gz
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar.bz2
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar.lz
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar.xz
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.tar.zst
dexon-952482d5e47c390afbd04230c43b1df8c53d84ef.zip
rpc: Support specifying HTTP client in RPC dialing (#15836)
* 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
Diffstat (limited to 'rpc')
-rw-r--r--rpc/http.go12
1 files changed, 9 insertions, 3 deletions
diff --git a/rpc/http.go b/rpc/http.go
index d61b0e470..6717899b5 100644
--- a/rpc/http.go
+++ b/rpc/http.go
@@ -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
@@ -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)