3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

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

View File

@ -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