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:
Roman Khimov 2020-04-29 17:19:31 +03:00
parent 24b46c6041
commit 00d6439eb2

View file

@ -29,9 +29,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
@ -50,7 +47,7 @@ type Options struct {
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.
@ -64,32 +61,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,
@ -139,23 +138,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 suppors that. // JSON-RPC call internally, so make sure your RPC server suppors that.
@ -196,7 +178,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
} }