rpc: refactor WSClient initialisation
Fix the following linter warning: ``` pkg/rpc/client/wsclient.go:99:18 govet copylocks: literal copies lock value from *cl: github.com/nspcc-dev/neo-go/pkg/rpc/client.Client contains sync.RWMutex ```
This commit is contained in:
parent
8991ee91cd
commit
0d8723527c
2 changed files with 25 additions and 18 deletions
|
@ -83,10 +83,19 @@ type calculateValidUntilBlockCache struct {
|
||||||
// New returns a new Client ready to use. You should call Init method to
|
// New returns a new Client ready to use. You should call Init method to
|
||||||
// initialize network magic the client is operating on.
|
// initialize network magic the client is operating on.
|
||||||
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
||||||
url, err := url.Parse(endpoint)
|
cl := new(Client)
|
||||||
|
err := initClient(ctx, cl, endpoint, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return cl, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initClient(ctx context.Context, cl *Client, endpoint string, opts Options) error {
|
||||||
|
url, err := url.Parse(endpoint)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if opts.DialTimeout <= 0 {
|
if opts.DialTimeout <= 0 {
|
||||||
opts.DialTimeout = defaultDialTimeout
|
opts.DialTimeout = defaultDialTimeout
|
||||||
|
@ -110,19 +119,17 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
||||||
// if opts.Cert != "" && opts.Key != "" {
|
// if opts.Cert != "" && opts.Key != "" {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
cl := &Client{
|
cl.ctx = ctx
|
||||||
ctx: ctx,
|
cl.cli = httpClient
|
||||||
cli: httpClient,
|
cl.endpoint = url
|
||||||
endpoint: url,
|
cl.cache = cache{
|
||||||
cache: cache{
|
|
||||||
nativeHashes: make(map[string]util.Uint160),
|
nativeHashes: make(map[string]util.Uint160),
|
||||||
},
|
|
||||||
latestReqID: atomic.NewUint64(0),
|
|
||||||
}
|
}
|
||||||
|
cl.latestReqID = atomic.NewUint64(0)
|
||||||
cl.getNextRequestID = (cl).getRequestID
|
cl.getNextRequestID = (cl).getRequestID
|
||||||
cl.opts = opts
|
cl.opts = opts
|
||||||
cl.requestF = cl.makeHTTPRequest
|
cl.requestF = cl.makeHTTPRequest
|
||||||
return cl, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getRequestID() uint64 {
|
func (c *Client) getRequestID() uint64 {
|
||||||
|
|
|
@ -79,20 +79,13 @@ const (
|
||||||
// You should call Init method to initialize network magic the client is
|
// You should call Init method to initialize network magic the client is
|
||||||
// operating on.
|
// operating on.
|
||||||
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
|
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
|
||||||
cl, err := New(ctx, endpoint, opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cl.cli = nil
|
|
||||||
|
|
||||||
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
||||||
ws, _, err := dialer.Dial(endpoint, nil)
|
ws, _, err := dialer.Dial(endpoint, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
wsc := &WSClient{
|
wsc := &WSClient{
|
||||||
Client: *cl,
|
Client: Client{},
|
||||||
Notifications: make(chan Notification),
|
Notifications: make(chan Notification),
|
||||||
|
|
||||||
ws: ws,
|
ws: ws,
|
||||||
|
@ -102,6 +95,13 @@ func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error
|
||||||
requests: make(chan *request.Raw),
|
requests: make(chan *request.Raw),
|
||||||
subscriptions: make(map[string]bool),
|
subscriptions: make(map[string]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = initClient(ctx, &wsc.Client, endpoint, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
wsc.Client.cli = nil
|
||||||
|
|
||||||
go wsc.wsReader()
|
go wsc.wsReader()
|
||||||
go wsc.wsWriter()
|
go wsc.wsWriter()
|
||||||
wsc.requestF = wsc.makeWsRequest
|
wsc.requestF = wsc.makeWsRequest
|
||||||
|
|
Loading…
Reference in a new issue