From efb3000717736c014d07b2c06b9ed79489ea39e9 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Wed, 11 Jan 2017 01:06:02 +1000 Subject: [PATCH] client_lookup_set: Use RWMutex --- irc/client_lookup_set.go | 26 +++++++++++++------------- irc/server.go | 12 ++++++------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/irc/client_lookup_set.go b/irc/client_lookup_set.go index 57f01b58..1437624f 100644 --- a/irc/client_lookup_set.go +++ b/irc/client_lookup_set.go @@ -36,7 +36,7 @@ func ExpandUserHost(userhost string) (expanded string) { } type ClientLookupSet struct { - ByNickMutex sync.Mutex + ByNickMutex sync.RWMutex ByNick map[string]*Client } @@ -47,8 +47,8 @@ func NewClientLookupSet() *ClientLookupSet { } func (clients *ClientLookupSet) Count() int { - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() count := len(clients.ByNick) return count } @@ -59,8 +59,8 @@ func (clients *ClientLookupSet) Has(nick string) bool { if err == nil { return false } - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() _, exists := clients.ByNick[casefoldedName] return exists } @@ -78,8 +78,8 @@ func (clients *ClientLookupSet) getNoMutex(nick string) *Client { func (clients *ClientLookupSet) Get(nick string) *Client { casefoldedName, err := CasefoldName(nick) if err == nil { - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() cli := clients.ByNick[casefoldedName] return cli } @@ -149,8 +149,8 @@ func (clients *ClientLookupSet) Replace(oldNick, newNick string, client *Client) func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet) { set = make(ClientSet) - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() var client *Client for _, client = range clients.ByNick { // make sure they have all the required caps @@ -175,8 +175,8 @@ func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) { } matcher := ircmatch.MakeMatch(userhost) - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() for _, client := range clients.ByNick { if matcher.Match(client.nickMaskCasefolded) { set.Add(client) @@ -194,8 +194,8 @@ func (clients *ClientLookupSet) Find(userhost string) *Client { matcher := ircmatch.MakeMatch(userhost) var matchedClient *Client - clients.ByNickMutex.Lock() - defer clients.ByNickMutex.Unlock() + clients.ByNickMutex.RLock() + defer clients.ByNickMutex.RUnlock() for _, client := range clients.ByNick { if matcher.Match(client.nickMaskCasefolded) { matchedClient = client diff --git a/irc/server.go b/irc/server.go index d9a04f09..d472a454 100644 --- a/irc/server.go +++ b/irc/server.go @@ -343,11 +343,11 @@ func loadChannelList(channel *Channel, list string, maskMode ChannelMode) { func (server *Server) Shutdown() { //TODO(dan): Make sure we disallow new nicks - server.clients.ByNickMutex.Lock() + server.clients.ByNickMutex.RLock() for _, client := range server.clients.ByNick { client.Notice("Server is shutting down") } - server.clients.ByNickMutex.Unlock() + server.clients.ByNickMutex.RUnlock() if err := server.store.Close(); err != nil { Log.error.Println("Server.Shutdown store.Close: error:", err) @@ -1069,14 +1069,14 @@ func (server *Server) rehash() error { server.connectionLimitsMutex.Lock() server.connectionLimits = connectionLimits - server.clients.ByNickMutex.Lock() + server.clients.ByNickMutex.RLock() for _, client := range server.clients.ByNick { ipaddr := net.ParseIP(IPString(client.socket.conn.RemoteAddr())) if ipaddr != nil { server.connectionLimits.AddClient(ipaddr, true) } } - server.clients.ByNickMutex.Unlock() + server.clients.ByNickMutex.RUnlock() server.connectionLimitsMutex.Unlock() // setup new and removed caps @@ -1143,14 +1143,14 @@ func (server *Server) rehash() error { newISupportReplies := oldISupportList.GetDifference(server.isupport) // push new info to all of our clients - server.clients.ByNickMutex.Lock() + server.clients.ByNickMutex.RLock() for _, sClient := range server.clients.ByNick { for _, tokenline := range newISupportReplies { // ugly trickery ahead sClient.Send(nil, server.name, RPL_ISUPPORT, append([]string{sClient.nick}, tokenline...)...) } } - server.clients.ByNickMutex.Unlock() + server.clients.ByNickMutex.RUnlock() // destroy old listeners tlsListeners := config.TLSListeners()