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