diff --git a/irc/server.go b/irc/server.go index 8193e708..4ec61afd 100644 --- a/irc/server.go +++ b/irc/server.go @@ -433,12 +433,12 @@ func (server *Server) Lusers(client *Client, rb *ResponseBuffer) { } rb.Add(nil, server.name, RPL_LUSERCLIENT, nick, fmt.Sprintf(client.t("There are %[1]d users and %[2]d invisible on %[3]d server(s)"), stats.Total-stats.Invisible, stats.Invisible, 1)) - rb.Add(nil, server.name, RPL_LUSEROP, nick, strconv.Itoa(stats.Operators), client.t("IRC Operators online")) - rb.Add(nil, server.name, RPL_LUSERUNKNOWN, nick, strconv.Itoa(stats.Unknown), client.t("unregistered connections")) + rb.Add(nil, server.name, RPL_LUSEROP, nick, strconv.Itoa(int(stats.Operators)), client.t("IRC Operators online")) + rb.Add(nil, server.name, RPL_LUSERUNKNOWN, nick, strconv.Itoa(int(stats.Unknown)), client.t("unregistered connections")) rb.Add(nil, server.name, RPL_LUSERCHANNELS, nick, strconv.Itoa(numChannels), client.t("channels formed")) rb.Add(nil, server.name, RPL_LUSERME, nick, fmt.Sprintf(client.t("I have %[1]d clients and %[2]d servers"), stats.Total, 0)) - total := strconv.Itoa(stats.Total) - max := strconv.Itoa(stats.Max) + total := strconv.Itoa(int(stats.Total)) + max := strconv.Itoa(int(stats.Max)) rb.Add(nil, server.name, RPL_LOCALUSERS, nick, total, max, fmt.Sprintf(client.t("Current local users %[1]s, max %[2]s"), total, max)) rb.Add(nil, server.name, RPL_GLOBALUSERS, nick, total, max, fmt.Sprintf(client.t("Current global users %[1]s, max %[2]s"), total, max)) } diff --git a/irc/stats.go b/irc/stats.go index d97f208a..37b85bb0 100644 --- a/irc/stats.go +++ b/irc/stats.go @@ -1,55 +1,47 @@ package irc import ( - "sync" + "sync/atomic" ) type StatsValues struct { - Unknown int // unregistered clients - Total int // registered clients, including invisible - Max int // high-water mark of registered clients - Invisible int - Operators int + Unknown int32 // unregistered clients + Total int32 // registered clients, including invisible + Max int32 // high-water mark of registered clients + Invisible int32 + Operators int32 } // Stats tracks statistics for a running server type Stats struct { StatsValues - - mutex sync.Mutex } // Add Adds an unregistered client func (s *Stats) Add() { - s.mutex.Lock() - s.Unknown += 1 - s.mutex.Unlock() + atomic.AddInt32(&s.Unknown, 1) } // AddRegistered Activates a registered client, e.g., for the initial attach to a persistent client func (s *Stats) AddRegistered(invisible, operator bool) { - s.mutex.Lock() if invisible { - s.Invisible += 1 + atomic.AddInt32(&s.Invisible, 1) } if operator { - s.Operators += 1 + atomic.AddInt32(&s.Operators, 1) } - s.Total += 1 + atomic.AddInt32(&s.Total, 1) s.setMax() - s.mutex.Unlock() } // Register Transition a client from unregistered to registered func (s *Stats) Register(invisible bool) { - s.mutex.Lock() - s.Unknown -= 1 + atomic.AddInt32(&s.Unknown, -1) if invisible { - s.Invisible += 1 + atomic.AddInt32(&s.Invisible, 1) } - s.Total += 1 + atomic.AddInt32(&s.Total, 1) s.setMax() - s.mutex.Unlock() } func (s *Stats) setMax() { @@ -60,39 +52,32 @@ func (s *Stats) setMax() { // ChangeInvisible Modify the Invisible count func (s *Stats) ChangeInvisible(increment int) { - s.mutex.Lock() - s.Invisible += increment - s.mutex.Unlock() + atomic.AddInt32(&s.Invisible, int32(increment)) } // ChangeOperators Modify the Operator count func (s *Stats) ChangeOperators(increment int) { - s.mutex.Lock() - s.Operators += increment - s.mutex.Unlock() + atomic.AddInt32(&s.Operators, int32(increment)) } // Remove a user from the server func (s *Stats) Remove(registered, invisible, operator bool) { - s.mutex.Lock() - if registered { - s.Total -= 1 - } else { - s.Unknown -= 1 + switch registered { + case true: + atomic.AddInt32(&s.Total, -1) + default: + atomic.AddInt32(&s.Unknown, -1) } if invisible { - s.Invisible -= 1 + atomic.AddInt32(&s.Invisible, -1) } if operator { - s.Operators -= 1 + atomic.AddInt32(&s.Operators, -1) } - s.mutex.Unlock() } // GetValues GetStats retrives total, invisible and oper count func (s *Stats) GetValues() (result StatsValues) { - s.mutex.Lock() result = s.StatsValues - s.mutex.Unlock() return }