Stop message handler goroutine when connection closes

This commit is contained in:
Ken-Håvard Lieng 2015-05-16 03:56:58 +02:00
parent e55760a1a5
commit f497e248f1
2 changed files with 13 additions and 6 deletions

View File

@ -62,6 +62,7 @@ type IRC struct {
reader *bufio.Reader
out chan string
ready sync.WaitGroup
once sync.Once
nick string
nickLock sync.Mutex
@ -247,22 +248,23 @@ func (i *IRC) recv() {
msg := parseMessage(line)
msg.Prefix = parseUser(msg.Prefix)
i.Messages <- msg
switch msg.Command {
case PING:
i.write("PONG :" + msg.Trailing)
go i.write("PONG :" + msg.Trailing)
case RPL_WELCOME:
i.ready.Done()
i.once.Do(i.ready.Done)
}
i.Messages <- msg
}
}
func (i *IRC) close() {
i.conn.Close()
i.ready.Done()
i.once.Do(i.ready.Done)
close(i.out)
close(i.Messages)
}
func parseMessage(line string) *Message {

View File

@ -12,7 +12,12 @@ func handleMessages(irc *IRC, session *Session) {
userBuffers := make(map[string][]string)
var motd MOTD
for msg := range irc.Messages {
for {
msg, ok := <-irc.Messages
if !ok {
return
}
switch msg.Command {
case NICK:
session.sendJSON("nick", Nick{