do an actual nonblocking send instead of the len() trick

This commit is contained in:
Shivaram Lingamneni 2018-03-16 12:39:11 -04:00
parent fa5d4be718
commit 0a432c9d99
1 changed files with 6 additions and 7 deletions

View File

@ -121,8 +121,6 @@ func (socket *Socket) Read() (string, error) {
// Write sends the given string out of Socket. // Write sends the given string out of Socket.
func (socket *Socket) Write(data string) (err error) { func (socket *Socket) Write(data string) (err error) {
socket.Lock() socket.Lock()
defer socket.Unlock()
if socket.closed { if socket.closed {
err = io.EOF err = io.EOF
} else if uint64(len(data)+len(socket.buffer)) > socket.MaxSendQBytes { } else if uint64(len(data)+len(socket.buffer)) > socket.MaxSendQBytes {
@ -131,12 +129,13 @@ func (socket *Socket) Write(data string) (err error) {
} else { } else {
socket.buffer = append(socket.buffer, data...) socket.buffer = append(socket.buffer, data...)
} }
socket.Unlock()
// this can generate a spurious wakeup, since we are racing against the channel read, // notify the consumer that data is available
// but since we are holding the mutex, we are not racing against the other writes select {
// and therefore we cannot miss a wakeup or block case socket.lineToSendExists <- true:
if len(socket.lineToSendExists) == 0 { default:
socket.lineToSendExists <- true // a notification is already in the queue, this is fine
} }
return return