Fix several races

This commit is contained in:
Sergey Matveev 2014-08-09 15:37:10 +04:00
parent 87ba38e7f7
commit def74ddc09
4 changed files with 26 additions and 16 deletions

View File

@ -34,13 +34,16 @@ type Client struct {
hostname string hostname string
conn net.Conn conn net.Conn
registered bool registered bool
ping_sent bool
timestamp time.Time
nickname string nickname string
username string username string
realname string realname string
} }
type ClientAlivenessState struct {
ping_sent bool
timestamp time.Time
}
func (client Client) String() string { func (client Client) String() string {
return client.nickname + "!" + client.username + "@" + client.conn.RemoteAddr().String() return client.nickname + "!" + client.username + "@" + client.conn.RemoteAddr().String()
} }
@ -65,8 +68,6 @@ func (client *Client) Processor(sink chan<- ClientEvent) {
sink <- ClientEvent{client, EVENT_DEL, ""} sink <- ClientEvent{client, EVENT_DEL, ""}
break break
} }
client.timestamp = time.Now()
client.ping_sent = false
buf_net = bytes.TrimRight(buf_net, "\x00") buf_net = bytes.TrimRight(buf_net, "\x00")
buf = append(buf, buf_net...) buf = append(buf, buf_net...)
if !bytes.HasSuffix(buf, []byte(CRLF)) { if !bytes.HasSuffix(buf, []byte(CRLF)) {

View File

@ -105,7 +105,6 @@ func TestNewClient(t *testing.T) {
} }
conn.inbound <- "foo" conn.inbound <- "foo"
event = <-sink event = <-sink
ts1 := client.timestamp
if (event.event_type != EVENT_MSG) || (event.text != "foo") { if (event.event_type != EVENT_MSG) || (event.text != "foo") {
t.Fatal("no first MSG", event) t.Fatal("no first MSG", event)
} }
@ -115,9 +114,6 @@ func TestNewClient(t *testing.T) {
t.Fatal("no second MSG", event) t.Fatal("no second MSG", event)
} }
conn.inbound <- "" conn.inbound <- ""
if client.timestamp.Before(ts1) || client.timestamp.Equal(ts1) {
t.Fatal("timestamp updating")
}
event = <-sink event = <-sink
if event.event_type != EVENT_DEL { if event.event_type != EVENT_DEL {
t.Fatal("no client termination", event) t.Fatal("no client termination", event)

View File

@ -43,6 +43,7 @@ type Daemon struct {
hostname string hostname string
motd string motd string
clients map[*Client]bool clients map[*Client]bool
client_aliveness map[*Client]*ClientAlivenessState
rooms map[string]*Room rooms map[string]*Room
room_sinks map[*Room]chan ClientEvent room_sinks map[*Room]chan ClientEvent
last_aliveness_check time.Time last_aliveness_check time.Time
@ -53,6 +54,7 @@ type Daemon struct {
func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon { func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon {
daemon := Daemon{hostname: hostname, motd: motd} daemon := Daemon{hostname: hostname, motd: motd}
daemon.clients = make(map[*Client]bool) daemon.clients = make(map[*Client]bool)
daemon.client_aliveness = make(map[*Client]*ClientAlivenessState)
daemon.rooms = make(map[string]*Room) daemon.rooms = make(map[string]*Room)
daemon.room_sinks = make(map[*Room]chan ClientEvent) daemon.room_sinks = make(map[*Room]chan ClientEvent)
daemon.log_sink = log_sink daemon.log_sink = log_sink
@ -255,20 +257,25 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
func (daemon *Daemon) Processor(events <-chan ClientEvent) { func (daemon *Daemon) Processor(events <-chan ClientEvent) {
for event := range events { for event := range events {
now := time.Now()
client := event.client
// Check for clients aliveness // Check for clients aliveness
now := time.Now()
if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) { if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) {
for c := range daemon.clients { for c := range daemon.clients {
if c.timestamp.Add(PING_TIMEOUT).Before(now) { aliveness, alive := daemon.client_aliveness[c]
if !alive {
continue
}
if aliveness.timestamp.Add(PING_TIMEOUT).Before(now) {
log.Println(c, "ping timeout") log.Println(c, "ping timeout")
c.conn.Close() c.conn.Close()
continue continue
} }
if !c.ping_sent && c.timestamp.Add(PING_THRESHOLD).Before(now) { if !aliveness.ping_sent && aliveness.timestamp.Add(PING_THRESHOLD).Before(now) {
if c.registered { if c.registered {
c.Msg("PING :" + daemon.hostname) c.Msg("PING :" + daemon.hostname)
c.ping_sent = true aliveness.ping_sent = true
} else { } else {
log.Println(c, "ping timeout") log.Println(c, "ping timeout")
c.conn.Close() c.conn.Close()
@ -278,12 +285,13 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
daemon.last_aliveness_check = now daemon.last_aliveness_check = now
} }
client := event.client
switch event.event_type { switch event.event_type {
case EVENT_NEW: case EVENT_NEW:
daemon.clients[client] = true daemon.clients[client] = true
daemon.client_aliveness[client] = &ClientAlivenessState{ping_sent: false, timestamp: now}
case EVENT_DEL: case EVENT_DEL:
delete(daemon.clients, client) delete(daemon.clients, client)
delete(daemon.client_aliveness, client)
for _, room_sink := range daemon.room_sinks { for _, room_sink := range daemon.room_sinks {
room_sink <- event room_sink <- event
} }
@ -295,11 +303,12 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
} }
if command == "QUIT" { if command == "QUIT" {
delete(daemon.clients, client) delete(daemon.clients, client)
delete(daemon.client_aliveness, client)
client.conn.Close() client.conn.Close()
continue continue
} }
if !client.registered { if !client.registered {
go daemon.ClientRegister(client, command, cols) daemon.ClientRegister(client, command, cols)
continue continue
} }
switch command { switch command {
@ -310,7 +319,7 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
client.ReplyNotEnoughParameters("JOIN") client.ReplyNotEnoughParameters("JOIN")
continue continue
} }
go daemon.HandlerJoin(client, cols[1]) daemon.HandlerJoin(client, cols[1])
case "LIST": case "LIST":
daemon.SendList(client, cols) daemon.SendList(client, cols)
case "LUSERS": case "LUSERS":
@ -432,5 +441,9 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
client.ReplyNicknamed("421", command, "Unknown command") client.ReplyNicknamed("421", command, "Unknown command")
} }
} }
if aliveness, alive := daemon.client_aliveness[client]; alive {
aliveness.timestamp = now
aliveness.ping_sent = false
}
} }
} }

View File

@ -106,7 +106,7 @@ func (room *Room) Processor(events <-chan ClientEvent) {
} }
delete(room.members, client) delete(room.members, client)
msg := fmt.Sprintf(":%s PART %s :%s", client, room.name, client.nickname) msg := fmt.Sprintf(":%s PART %s :%s", client, room.name, client.nickname)
go room.Broadcast(msg) room.Broadcast(msg)
room.log_sink <- LogEvent{room.name, client.nickname, "left", true} room.log_sink <- LogEvent{room.name, client.nickname, "left", true}
case EVENT_TOPIC: case EVENT_TOPIC:
if _, subscribed := room.members[client]; !subscribed { if _, subscribed := room.members[client]; !subscribed {