Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: use atomic types #27214

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type Client struct {
isHTTP bool // connection type: http, ws or ipc
services *serviceRegistry

idCounter uint32
idCounter atomic.Uint32

// This function, if non-nil, is called when the connection is lost.
reconnectFunc reconnectFunc
Expand Down Expand Up @@ -263,7 +263,7 @@ func (c *Client) RegisterName(name string, receiver interface{}) error {
}

func (c *Client) nextID() json.RawMessage {
id := atomic.AddUint32(&c.idCounter, 1)
id := c.idCounter.Add(1)
return strconv.AppendUint(nil, uint64(id), 10)
}

Expand Down
10 changes: 5 additions & 5 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ type Server struct {

mutex sync.Mutex
codecs map[ServerCodec]struct{}
run int32
run atomic.Bool
}

// NewServer creates a new server instance with no registered handlers.
func NewServer() *Server {
server := &Server{
idgen: randomIDGenerator(),
codecs: make(map[ServerCodec]struct{}),
run: 1,
}
server.run.Store(true)
// Register the default service providing meta information about the RPC service such
// as the services and methods it offers.
rpcService := &RPCService{server}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Server) trackCodec(codec ServerCodec) bool {
s.mutex.Lock()
defer s.mutex.Unlock()

if atomic.LoadInt32(&s.run) == 0 {
if !s.run.Load() {
return false // Don't serve if server is stopped.
}
s.codecs[codec] = struct{}{}
Expand All @@ -114,7 +114,7 @@ func (s *Server) untrackCodec(codec ServerCodec) {
// this mode.
func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
// Don't serve if server is stopped.
if atomic.LoadInt32(&s.run) == 0 {
if !s.run.Load() {
return
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *Server) Stop() {
s.mutex.Lock()
defer s.mutex.Unlock()

if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
if s.run.CompareAndSwap(true, false) {
log.Debug("RPC server shutting down")
for codec := range s.codecs {
codec.close()
Expand Down
4 changes: 2 additions & 2 deletions signer/core/stdioui.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
)

type StdIOUI struct {
client rpc.Client
client *rpc.Client
}

func NewStdIOUI() *StdIOUI {
client, err := rpc.DialContext(context.Background(), "stdio://")
if err != nil {
log.Crit("Could not create stdio client", "err", err)
}
ui := &StdIOUI{client: *client}
ui := &StdIOUI{client: client}
return ui
}

Expand Down