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:
AnnaShaleva 2022-02-21 18:30:07 +03:00
parent 8991ee91cd
commit 0d8723527c
2 changed files with 25 additions and 18 deletions

View file

@ -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 {

View file

@ -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