From 0a432c9d99a4cdcb82654af2b3bb7c6ab9e1d5eb Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Fri, 16 Mar 2018 12:39:11 -0400 Subject: [PATCH] do an actual nonblocking send instead of the len() trick --- irc/socket.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/irc/socket.go b/irc/socket.go index dceff9a6..9d3282ab 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -121,8 +121,6 @@ func (socket *Socket) Read() (string, error) { // Write sends the given string out of Socket. func (socket *Socket) Write(data string) (err error) { socket.Lock() - defer socket.Unlock() - if socket.closed { err = io.EOF } else if uint64(len(data)+len(socket.buffer)) > socket.MaxSendQBytes { @@ -131,12 +129,13 @@ func (socket *Socket) Write(data string) (err error) { } else { socket.buffer = append(socket.buffer, data...) } + socket.Unlock() - // this can generate a spurious wakeup, since we are racing against the channel read, - // but since we are holding the mutex, we are not racing against the other writes - // and therefore we cannot miss a wakeup or block - if len(socket.lineToSendExists) == 0 { - socket.lineToSendExists <- true + // notify the consumer that data is available + select { + case socket.lineToSendExists <- true: + default: + // a notification is already in the queue, this is fine } return