mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-22 11:59:40 +01:00
tweak: clean up Socket.Close()
This commit is contained in:
parent
0a432c9d99
commit
8fd1446627
@ -52,18 +52,11 @@ func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
|
|||||||
|
|
||||||
// Close stops a Socket from being able to send/receive any more data.
|
// Close stops a Socket from being able to send/receive any more data.
|
||||||
func (socket *Socket) Close() {
|
func (socket *Socket) Close() {
|
||||||
alreadyClosed := func() bool {
|
socket.Lock()
|
||||||
socket.Lock()
|
socket.closed = true
|
||||||
defer socket.Unlock()
|
socket.Unlock()
|
||||||
result := socket.closed
|
|
||||||
socket.closed = true
|
|
||||||
return result
|
|
||||||
}()
|
|
||||||
|
|
||||||
if !alreadyClosed {
|
socket.wakeWriter()
|
||||||
// force close loop to happen if it hasn't already
|
|
||||||
socket.Write("")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CertFP returns the fingerprint of the certificate provided by the client.
|
// CertFP returns the fingerprint of the certificate provided by the client.
|
||||||
@ -131,14 +124,17 @@ func (socket *Socket) Write(data string) (err error) {
|
|||||||
}
|
}
|
||||||
socket.Unlock()
|
socket.Unlock()
|
||||||
|
|
||||||
// notify the consumer that data is available
|
socket.wakeWriter()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// wakeWriter wakes up the goroutine that actually performs the write, without blocking
|
||||||
|
func (socket *Socket) wakeWriter() {
|
||||||
|
// nonblocking send to the channel, no-op if it's full
|
||||||
select {
|
select {
|
||||||
case socket.lineToSendExists <- true:
|
case socket.lineToSendExists <- true:
|
||||||
default:
|
default:
|
||||||
// a notification is already in the queue, this is fine
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFinalData sets the final data to send when the SocketWriter closes.
|
// SetFinalData sets the final data to send when the SocketWriter closes.
|
||||||
|
Loading…
Reference in New Issue
Block a user