rpc: make RPC Client thread-safe

This commit is contained in:
AnnaShaleva 2022-02-21 16:06:43 +03:00 committed by Anna Shaleva
parent 0092330fe1
commit 408f6b050e
2 changed files with 21 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/url"
"sync"
"time"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
@ -34,6 +35,7 @@ type Client struct {
opts Options
requestF func(*request.Raw) (*response.Raw, error)
cacheLock sync.RWMutex
// cache stores RPC node related information client is bound to.
// cache is mostly filled in during Init(), but can also be updated
// during regular Client lifecycle.
@ -123,6 +125,10 @@ func (c *Client) Init() error {
if err != nil {
return fmt.Errorf("failed to get network magic: %w", err)
}
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
c.cache.network = version.Protocol.Network
c.cache.stateRootInHeader = version.Protocol.StateRootInHeader
if version.Protocol.MillisecondsPerBlock == 0 {