Use lighter atomic counters instead of mutexes

Where appropriate, it makes sense to use lighter weight atomic counters
instead of mutexes.
This commit is contained in:
Tomás Senart 2014-07-03 03:55:22 +02:00 committed by Dave Collins
parent 07bdbd9e3d
commit 2afc5a0af2

View file

@ -210,8 +210,7 @@ type rpcServer struct {
server *server
authsha [fastsha256.Size]byte
ntfnMgr *wsNotificationManager
numClients int
numClientsMutex sync.Mutex
numClients int32
statusLines map[int]string
statusLock sync.RWMutex
wg sync.WaitGroup
@ -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)
@ -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.
@ -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