client: make http.Client internal to the Client
Exposing it the outside users is strange, so incapsulate it completely. Fix DialTimeout setting along the way, handle negative timeouts as invalid.
This commit is contained in:
parent
ec62edac68
commit
315aabde56
1 changed files with 22 additions and 40 deletions
|
@ -31,9 +31,6 @@ const (
|
||||||
// Client represents the middleman for executing JSON RPC calls
|
// Client represents the middleman for executing JSON RPC calls
|
||||||
// to remote NEO RPC nodes.
|
// to remote NEO RPC nodes.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// The underlying http client. It's never a good practice to use
|
|
||||||
// the http.DefaultClient, therefore we will role our own.
|
|
||||||
cliMu *sync.Mutex
|
|
||||||
cli *http.Client
|
cli *http.Client
|
||||||
endpoint *url.URL
|
endpoint *url.URL
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
@ -49,11 +46,11 @@ type Client struct {
|
||||||
// All Values are optional. If any duration is not specified
|
// All Values are optional. If any duration is not specified
|
||||||
// a default of 3 seconds will be used.
|
// a default of 3 seconds will be used.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Cert string
|
Cert string
|
||||||
Key string
|
Key string
|
||||||
CACert string
|
CACert string
|
||||||
DialTimeout time.Duration
|
DialTimeout time.Duration
|
||||||
Client *http.Client
|
RequestTimeout time.Duration
|
||||||
// Version is the version of the client that will be send
|
// Version is the version of the client that will be send
|
||||||
// along with the request body. If no version is specified
|
// along with the request body. If no version is specified
|
||||||
// the default version (currently 2.0) will be used.
|
// the default version (currently 2.0) will be used.
|
||||||
|
@ -79,32 +76,34 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.DialTimeout <= 0 {
|
||||||
|
opts.DialTimeout = defaultDialTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.RequestTimeout <= 0 {
|
||||||
|
opts.RequestTimeout = defaultRequestTimeout
|
||||||
|
}
|
||||||
|
|
||||||
if opts.Version == "" {
|
if opts.Version == "" {
|
||||||
opts.Version = defaultClientVersion
|
opts.Version = defaultClientVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Client == nil {
|
httpClient := &http.Client{
|
||||||
opts.Client = &http.Client{
|
Transport: &http.Transport{
|
||||||
Transport: &http.Transport{
|
DialContext: (&net.Dialer{
|
||||||
DialContext: (&net.Dialer{
|
Timeout: opts.DialTimeout,
|
||||||
Timeout: opts.DialTimeout,
|
}).DialContext,
|
||||||
}).DialContext,
|
},
|
||||||
},
|
Timeout: opts.RequestTimeout,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(@antdm): Enable SSL.
|
// TODO(@antdm): Enable SSL.
|
||||||
if opts.Cert != "" && opts.Key != "" {
|
if opts.Cert != "" && opts.Key != "" {
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Client.Timeout == 0 {
|
|
||||||
opts.Client.Timeout = defaultRequestTimeout
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cli: opts.Client,
|
cli: httpClient,
|
||||||
cliMu: new(sync.Mutex),
|
|
||||||
balancerMu: new(sync.Mutex),
|
balancerMu: new(sync.Mutex),
|
||||||
wifMu: new(sync.Mutex),
|
wifMu: new(sync.Mutex),
|
||||||
endpoint: url,
|
endpoint: url,
|
||||||
|
@ -154,23 +153,6 @@ func (c *Client) SetBalancer(b request.BalanceGetter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client is a getter for client field.
|
|
||||||
func (c *Client) Client() *http.Client {
|
|
||||||
c.cliMu.Lock()
|
|
||||||
defer c.cliMu.Unlock()
|
|
||||||
return c.cli
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetClient is a setter for client field.
|
|
||||||
func (c *Client) SetClient(cli *http.Client) {
|
|
||||||
c.cliMu.Lock()
|
|
||||||
defer c.cliMu.Unlock()
|
|
||||||
|
|
||||||
if cli != nil {
|
|
||||||
c.cli = cli
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CalculateInputs creates input transactions for the specified amount of given
|
// CalculateInputs creates input transactions for the specified amount of given
|
||||||
// asset belonging to specified address. This implementation uses GetUnspents
|
// asset belonging to specified address. This implementation uses GetUnspents
|
||||||
// JSON-RPC call internally, so make sure your RPC server supports that.
|
// JSON-RPC call internally, so make sure your RPC server supports that.
|
||||||
|
@ -211,7 +193,7 @@ func (c *Client) performRequest(method string, p request.RawParams, v interface{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
resp, err := c.Client().Do(req)
|
resp, err := c.cli.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue