3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

client_lookup_set: Use RWMutex

This commit is contained in:
Daniel Oaks 2017-01-11 01:06:02 +10:00
parent 658d1656fa
commit efb3000717
2 changed files with 19 additions and 19 deletions

View File

@ -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

View File

@ -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()