From 0d8723527c9f0b97f70125875f3951c4ff123028 Mon Sep 17 00:00:00 2001 From: AnnaShaleva Date: Mon, 21 Feb 2022 18:30:07 +0300 Subject: [PATCH] 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 ``` --- pkg/rpc/client/client.go | 27 +++++++++++++++++---------- pkg/rpc/client/wsclient.go | 16 ++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pkg/rpc/client/client.go b/pkg/rpc/client/client.go index 20baae7ba..a43f81649 100644 --- a/pkg/rpc/client/client.go +++ b/pkg/rpc/client/client.go @@ -83,10 +83,19 @@ type calculateValidUntilBlockCache struct { // New returns a new Client ready to use. You should call Init method to // initialize network magic the client is operating on. 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 { 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 { opts.DialTimeout = defaultDialTimeout @@ -110,19 +119,17 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) { // if opts.Cert != "" && opts.Key != "" { // } - cl := &Client{ - ctx: ctx, - cli: httpClient, - endpoint: url, - cache: cache{ - nativeHashes: make(map[string]util.Uint160), - }, - latestReqID: atomic.NewUint64(0), + cl.ctx = ctx + cl.cli = httpClient + cl.endpoint = url + cl.cache = cache{ + nativeHashes: make(map[string]util.Uint160), } + cl.latestReqID = atomic.NewUint64(0) cl.getNextRequestID = (cl).getRequestID cl.opts = opts cl.requestF = cl.makeHTTPRequest - return cl, nil + return nil } func (c *Client) getRequestID() uint64 { diff --git a/pkg/rpc/client/wsclient.go b/pkg/rpc/client/wsclient.go index c3f13a522..3927af9c3 100644 --- a/pkg/rpc/client/wsclient.go +++ b/pkg/rpc/client/wsclient.go @@ -79,20 +79,13 @@ const ( // You should call Init method to initialize network magic the client is // operating on. 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} ws, _, err := dialer.Dial(endpoint, nil) if err != nil { return nil, err } wsc := &WSClient{ - Client: *cl, + Client: Client{}, Notifications: make(chan Notification), ws: ws, @@ -102,6 +95,13 @@ func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error requests: make(chan *request.Raw), 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.wsWriter() wsc.requestF = wsc.makeWsRequest