3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

fix more data races

This commit is contained in:
Jeremy Latt 2014-02-14 08:42:56 -08:00
parent 6ea3c8f4d1
commit 9600be82a3
2 changed files with 10 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package irc
import ( import (
"log" "log"
"sync"
) )
type Channel struct { type Channel struct {
@ -10,6 +11,7 @@ type Channel struct {
destroyed bool destroyed bool
key string key string
members ClientSet members ClientSet
mutex *sync.Mutex
name string name string
noOutside bool noOutside bool
password string password string
@ -38,6 +40,7 @@ func NewChannel(s *Server, name string) *Channel {
banList: make([]UserMask, 0), banList: make([]UserMask, 0),
commands: commands, commands: commands,
members: make(ClientSet), members: make(ClientSet),
mutex: &sync.Mutex{},
name: name, name: name,
replies: replies, replies: replies,
server: s, server: s,
@ -92,11 +95,13 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
if DEBUG_CHANNEL { if DEBUG_CHANNEL {
log.Printf("%s ← %s %s", channel, reply.Source(), reply) log.Printf("%s ← %s %s", channel, reply.Source(), reply)
} }
channel.mutex.Lock()
for client := range channel.members { for client := range channel.members {
if reply.Source() != Identifier(client) { if reply.Source() != Identifier(client) {
client.Reply(reply) client.Reply(reply)
} }
} }
channel.mutex.Unlock()
} }
} }

View File

@ -9,6 +9,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"sync"
"time" "time"
) )
@ -17,6 +18,7 @@ type Server struct {
commands chan Command commands chan Command
ctime time.Time ctime time.Time
motdFile string motdFile string
mutex *sync.Mutex
name string name string
operators map[string]string operators map[string]string
password string password string
@ -30,6 +32,7 @@ func NewServer(config *Config) *Server {
commands: make(chan Command), commands: make(chan Command),
ctime: time.Now(), ctime: time.Now(),
motdFile: config.MOTD, motdFile: config.MOTD,
mutex: &sync.Mutex{},
name: config.Name, name: config.Name,
operators: make(map[string]string), operators: make(map[string]string),
password: config.Password, password: config.Password,
@ -296,7 +299,9 @@ func (m *QuitCommand) HandleServer(server *Server) {
iclients.Remove(client) iclients.Remove(client)
for channel := range client.channels { for channel := range client.channels {
channel.mutex.Lock()
channel.members.Remove(client) channel.members.Remove(client)
channel.mutex.Unlock()
} }
client.Reply(RplError(server, client)) client.Reply(RplError(server, client))