From f497e248f1cc4a1ea91b9f40979d4fe6febf90fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Sat, 16 May 2015 03:56:58 +0200 Subject: [PATCH] Stop message handler goroutine when connection closes --- server/irc.go | 12 +++++++----- server/message_handler.go | 7 ++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/irc.go b/server/irc.go index c87ee406..95c5ec21 100644 --- a/server/irc.go +++ b/server/irc.go @@ -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 { diff --git a/server/message_handler.go b/server/message_handler.go index 01672176..ce8abca6 100644 --- a/server/message_handler.go +++ b/server/message_handler.go @@ -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{