mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 19:03:17 +03:00
Fixing race conditions.
This commit is contained in:
parent
3025e59935
commit
99c2cf1756
@ -73,7 +73,7 @@ type Client struct {
|
|||||||
beepMe bool
|
beepMe bool
|
||||||
colorMe bool
|
colorMe bool
|
||||||
closed bool
|
closed bool
|
||||||
sync.Mutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient constructs a new client
|
// NewClient constructs a new client
|
||||||
|
12
server.go
12
server.go
@ -44,7 +44,7 @@ type Server struct {
|
|||||||
admins map[string]struct{} // fingerprint lookup
|
admins map[string]struct{} // fingerprint lookup
|
||||||
bannedPK map[string]*time.Time // fingerprint lookup
|
bannedPK map[string]*time.Time // fingerprint lookup
|
||||||
started time.Time
|
started time.Time
|
||||||
sync.Mutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer constructs a new server
|
// NewServer constructs a new server
|
||||||
@ -112,6 +112,9 @@ func (s *Server) Broadcast(msg string, except *Client) {
|
|||||||
logger.Debugf("Broadcast to %d: %s", s.Len(), msg)
|
logger.Debugf("Broadcast to %d: %s", s.Len(), msg)
|
||||||
s.history.Add(msg)
|
s.history.Add(msg)
|
||||||
|
|
||||||
|
s.RLock()
|
||||||
|
defer s.RUnlock()
|
||||||
|
|
||||||
for _, client := range s.clients {
|
for _, client := range s.clients {
|
||||||
if except != nil && client == except {
|
if except != nil && client == except {
|
||||||
continue
|
continue
|
||||||
@ -145,9 +148,7 @@ func (s *Server) Privmsg(nick, message string, sender *Client) error {
|
|||||||
|
|
||||||
// SetMotd sets the Message of the Day (MOTD)
|
// SetMotd sets the Message of the Day (MOTD)
|
||||||
func (s *Server) SetMotd(motd string) {
|
func (s *Server) SetMotd(motd string) {
|
||||||
s.Lock()
|
|
||||||
s.motd = motd
|
s.motd = motd
|
||||||
s.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MotdUnicast sends the MOTD as a SysMsg
|
// MotdUnicast sends the MOTD as a SysMsg
|
||||||
@ -248,6 +249,9 @@ func (s *Server) Rename(client *Client, newName string) {
|
|||||||
func (s *Server) List(prefix *string) []string {
|
func (s *Server) List(prefix *string) []string {
|
||||||
r := []string{}
|
r := []string{}
|
||||||
|
|
||||||
|
s.RLock()
|
||||||
|
defer s.RUnlock()
|
||||||
|
|
||||||
for name := range s.clients {
|
for name := range s.clients {
|
||||||
if prefix != nil && !strings.HasPrefix(name, *prefix) {
|
if prefix != nil && !strings.HasPrefix(name, *prefix) {
|
||||||
continue
|
continue
|
||||||
@ -494,9 +498,11 @@ func (s *Server) AutoCompleteFunction(line string, pos int, key rune) (newLine s
|
|||||||
|
|
||||||
// Stop stops the server
|
// Stop stops the server
|
||||||
func (s *Server) Stop() {
|
func (s *Server) Stop() {
|
||||||
|
s.Lock()
|
||||||
for _, client := range s.clients {
|
for _, client := range s.clients {
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
}
|
}
|
||||||
|
s.Unlock()
|
||||||
|
|
||||||
close(s.done)
|
close(s.done)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user