Skip to content

Commit

Permalink
Use lighter atomic counters instead of mutexes
Browse files Browse the repository at this point in the history
Where appropriate, it makes sense to use lighter weight atomic counters
instead of mutexes.
  • Loading branch information
tsenart authored and davecgh committed Jul 7, 2014
1 parent 07bdbd9 commit 2afc5a0
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,18 @@ func newWorkState() *workState {
// rpcServer holds the items the rpc server may need to access (config,
// shutdown, main server, etc.)
type rpcServer struct {
started int32
shutdown int32
server *server
authsha [fastsha256.Size]byte
ntfnMgr *wsNotificationManager
numClients int
numClientsMutex sync.Mutex
statusLines map[int]string
statusLock sync.RWMutex
wg sync.WaitGroup
listeners []net.Listener
workState *workState
quit chan int
started int32
shutdown int32
server *server
authsha [fastsha256.Size]byte
ntfnMgr *wsNotificationManager
numClients int32
statusLines map[int]string
statusLock sync.RWMutex
wg sync.WaitGroup
listeners []net.Listener
workState *workState
quit chan int
}

// Start is used by server.go to start the rpc listener.
Expand Down Expand Up @@ -353,10 +352,7 @@ func (s *rpcServer) writeHTTPResponseHeaders(req *http.Request, headers http.Hea
//
// This function is safe for concurrent access.
func (s *rpcServer) limitConnections(w http.ResponseWriter, remoteAddr string) bool {
s.numClientsMutex.Lock()
defer s.numClientsMutex.Unlock()

if s.numClients+1 > cfg.RPCMaxClients {
if int(atomic.LoadInt32(&s.numClients)+1) > cfg.RPCMaxClients {
rpcsLog.Infof("Max RPC clients exceeded [%d] - "+
"disconnecting client %s", cfg.RPCMaxClients,
remoteAddr)
Expand All @@ -373,10 +369,7 @@ func (s *rpcServer) limitConnections(w http.ResponseWriter, remoteAddr string) b
//
// This function is safe for concurrent access.
func (s *rpcServer) incrementClients() {
s.numClientsMutex.Lock()
defer s.numClientsMutex.Unlock()

s.numClients++
atomic.AddInt32(&s.numClients, 1)
}

// decrementClients subtracts one from the number of connected RPC clients.
Expand All @@ -385,10 +378,7 @@ func (s *rpcServer) incrementClients() {
//
// This function is safe for concurrent access.
func (s *rpcServer) decrementClients() {
s.numClientsMutex.Lock()
defer s.numClientsMutex.Unlock()

s.numClients--
atomic.AddInt32(&s.numClients, -1)
}

// checkAuth checks the HTTP Basic authentication supplied by a wallet
Expand Down

0 comments on commit 2afc5a0

Please sign in to comment.