From d696f2313ef523a1ee5438a0bccea17222b42bb8 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Thu, 27 Mar 2014 16:49:22 -0700 Subject: [PATCH] stop leaking client goroutines; closes #34 --- irc/client.go | 21 ++++++++++++++++----- irc/socket.go | 8 +------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/irc/client.go b/irc/client.go index 4e076b54..e6f95787 100644 --- a/irc/client.go +++ b/irc/client.go @@ -60,10 +60,17 @@ func NewClient(server *Server, conn net.Conn) *Client { // command goroutine // -func (client *Client) run() { - for command := range client.commands { - command.SetClient(client) +func (client *Client) send(command Command) { + command.SetClient(client) + client.server.commands <- command +} +func (client *Client) run() { + client.send(&ProxyCommand{ + hostname: AddrLookupHostname(client.socket.conn.RemoteAddr()), + }) + + for command := range client.commands { checkPass, ok := command.(checkPasswordCommand) if ok { checkPass.LoadPassword(client.server) @@ -75,8 +82,12 @@ func (client *Client) run() { checkPass.CheckPassword() } - client.server.commands <- command + client.send(command) } + + client.send(&QuitCommand{ + message: "connection closed", + }) } func (client *Client) connectionTimeout() { @@ -259,8 +270,8 @@ func (client *Client) Quit(message Text) { return } - client.Reply(RplError("connection closed")) client.hasQuit = true + client.Reply(RplError("quit")) client.server.whoWas.Append(client) friends := client.Friends() friends.Remove(client) diff --git a/irc/socket.go b/irc/socket.go index 84e90313..2b075ea0 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -41,10 +41,6 @@ func (socket *Socket) Close() { } func (socket *Socket) readLines(commands chan<- Command) { - commands <- &ProxyCommand{ - hostname: AddrLookupHostname(socket.conn.RemoteAddr()), - } - for { line, err := socket.reader.ReadString('\n') if socket.isError(err, R) { @@ -64,9 +60,7 @@ func (socket *Socket) readLines(commands chan<- Command) { commands <- msg } - commands <- &QuitCommand{ - message: "connection closed", - } + close(commands) } func (socket *Socket) Write(line string) (err error) {