rpc: add SetHeader method to Client (#21392)

Resolves #20163

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
rene
2020-08-03 14:08:42 +02:00
committed by GitHub
parent 9c2ac6fbd5
commit 290d6bd903
3 changed files with 79 additions and 10 deletions

View File

@ -85,7 +85,7 @@ type Client struct {
// writeConn is used for writing to the connection on the caller's goroutine. It should
// only be accessed outside of dispatch, with the write lock held. The write lock is
// taken by sending on requestOp and released by sending on sendDone.
// taken by sending on reqInit and released by sending on reqSent.
writeConn jsonWriter
// for dispatch
@ -260,6 +260,19 @@ func (c *Client) Close() {
}
}
// SetHeader adds a custom HTTP header to the client's requests.
// This method only works for clients using HTTP, it doesn't have
// any effect for clients using another transport.
func (c *Client) SetHeader(key, value string) {
if !c.isHTTP {
return
}
conn := c.writeConn.(*httpConn)
conn.mu.Lock()
conn.headers.Set(key, value)
conn.mu.Unlock()
}
// Call performs a JSON-RPC call with the given arguments and unmarshals into
// result if no error occurred.
//