3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 05:49:25 +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 { type ClientLookupSet struct {
ByNickMutex sync.Mutex ByNickMutex sync.RWMutex
ByNick map[string]*Client ByNick map[string]*Client
} }
@ -47,8 +47,8 @@ func NewClientLookupSet() *ClientLookupSet {
} }
func (clients *ClientLookupSet) Count() int { func (clients *ClientLookupSet) Count() int {
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
count := len(clients.ByNick) count := len(clients.ByNick)
return count return count
} }
@ -59,8 +59,8 @@ func (clients *ClientLookupSet) Has(nick string) bool {
if err == nil { if err == nil {
return false return false
} }
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
_, exists := clients.ByNick[casefoldedName] _, exists := clients.ByNick[casefoldedName]
return exists return exists
} }
@ -78,8 +78,8 @@ func (clients *ClientLookupSet) getNoMutex(nick string) *Client {
func (clients *ClientLookupSet) Get(nick string) *Client { func (clients *ClientLookupSet) Get(nick string) *Client {
casefoldedName, err := CasefoldName(nick) casefoldedName, err := CasefoldName(nick)
if err == nil { if err == nil {
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
cli := clients.ByNick[casefoldedName] cli := clients.ByNick[casefoldedName]
return cli return cli
} }
@ -149,8 +149,8 @@ func (clients *ClientLookupSet) Replace(oldNick, newNick string, client *Client)
func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet) { func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet) {
set = make(ClientSet) set = make(ClientSet)
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
var client *Client var client *Client
for _, client = range clients.ByNick { for _, client = range clients.ByNick {
// make sure they have all the required caps // make sure they have all the required caps
@ -175,8 +175,8 @@ func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
} }
matcher := ircmatch.MakeMatch(userhost) matcher := ircmatch.MakeMatch(userhost)
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
for _, client := range clients.ByNick { for _, client := range clients.ByNick {
if matcher.Match(client.nickMaskCasefolded) { if matcher.Match(client.nickMaskCasefolded) {
set.Add(client) set.Add(client)
@ -194,8 +194,8 @@ func (clients *ClientLookupSet) Find(userhost string) *Client {
matcher := ircmatch.MakeMatch(userhost) matcher := ircmatch.MakeMatch(userhost)
var matchedClient *Client var matchedClient *Client
clients.ByNickMutex.Lock() clients.ByNickMutex.RLock()
defer clients.ByNickMutex.Unlock() defer clients.ByNickMutex.RUnlock()
for _, client := range clients.ByNick { for _, client := range clients.ByNick {
if matcher.Match(client.nickMaskCasefolded) { if matcher.Match(client.nickMaskCasefolded) {
matchedClient = client matchedClient = client

View File

@ -343,11 +343,11 @@ func loadChannelList(channel *Channel, list string, maskMode ChannelMode) {
func (server *Server) Shutdown() { func (server *Server) Shutdown() {
//TODO(dan): Make sure we disallow new nicks //TODO(dan): Make sure we disallow new nicks
server.clients.ByNickMutex.Lock() server.clients.ByNickMutex.RLock()
for _, client := range server.clients.ByNick { for _, client := range server.clients.ByNick {
client.Notice("Server is shutting down") client.Notice("Server is shutting down")
} }
server.clients.ByNickMutex.Unlock() server.clients.ByNickMutex.RUnlock()
if err := server.store.Close(); err != nil { if err := server.store.Close(); err != nil {
Log.error.Println("Server.Shutdown store.Close: error:", err) Log.error.Println("Server.Shutdown store.Close: error:", err)
@ -1069,14 +1069,14 @@ func (server *Server) rehash() error {
server.connectionLimitsMutex.Lock() server.connectionLimitsMutex.Lock()
server.connectionLimits = connectionLimits server.connectionLimits = connectionLimits
server.clients.ByNickMutex.Lock() server.clients.ByNickMutex.RLock()
for _, client := range server.clients.ByNick { for _, client := range server.clients.ByNick {
ipaddr := net.ParseIP(IPString(client.socket.conn.RemoteAddr())) ipaddr := net.ParseIP(IPString(client.socket.conn.RemoteAddr()))
if ipaddr != nil { if ipaddr != nil {
server.connectionLimits.AddClient(ipaddr, true) server.connectionLimits.AddClient(ipaddr, true)
} }
} }
server.clients.ByNickMutex.Unlock() server.clients.ByNickMutex.RUnlock()
server.connectionLimitsMutex.Unlock() server.connectionLimitsMutex.Unlock()
// setup new and removed caps // setup new and removed caps
@ -1143,14 +1143,14 @@ func (server *Server) rehash() error {
newISupportReplies := oldISupportList.GetDifference(server.isupport) newISupportReplies := oldISupportList.GetDifference(server.isupport)
// push new info to all of our clients // push new info to all of our clients
server.clients.ByNickMutex.Lock() server.clients.ByNickMutex.RLock()
for _, sClient := range server.clients.ByNick { for _, sClient := range server.clients.ByNick {
for _, tokenline := range newISupportReplies { for _, tokenline := range newISupportReplies {
// ugly trickery ahead // ugly trickery ahead
sClient.Send(nil, server.name, RPL_ISUPPORT, append([]string{sClient.nick}, tokenline...)...) sClient.Send(nil, server.name, RPL_ISUPPORT, append([]string{sClient.nick}, tokenline...)...)
} }
} }
server.clients.ByNickMutex.Unlock() server.clients.ByNickMutex.RUnlock()
// destroy old listeners // destroy old listeners
tlsListeners := config.TLSListeners() tlsListeners := config.TLSListeners()