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

socket: Attempt to close sockets better

This commit is contained in:
Daniel Oaks 2017-04-18 16:43:24 +10:00
parent 1c917a19a7
commit 4a66771c39

View File

@ -49,10 +49,13 @@ 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() {
if socket.Closed {
return
}
socket.Closed = true socket.Closed = true
// force close loop to happen // force close loop to happen
go socket.fillLineToSendExists() go socket.fillLineToSendExists(true)
} }
// CertFP returns the fingerprint of the certificate provided by the client. // CertFP returns the fingerprint of the certificate provided by the client.
@ -116,14 +119,16 @@ func (socket *Socket) Write(data string) error {
socket.linesToSendMutex.Lock() socket.linesToSendMutex.Lock()
socket.linesToSend = append(socket.linesToSend, data) socket.linesToSend = append(socket.linesToSend, data)
socket.linesToSendMutex.Unlock() socket.linesToSendMutex.Unlock()
go socket.fillLineToSendExists() go socket.fillLineToSendExists(false)
return nil return nil
} }
// fillLineToSendExists only exists because you can't goroutine single statements. // fillLineToSendExists only exists because you can't goroutine single statements.
func (socket *Socket) fillLineToSendExists() { func (socket *Socket) fillLineToSendExists(force bool) {
socket.lineToSendExists <- true if force || !socket.Closed {
socket.lineToSendExists <- true
}
} }
// RunSocketWriter starts writing messages to the outgoing socket. // RunSocketWriter starts writing messages to the outgoing socket.
@ -185,15 +190,16 @@ func (socket *Socket) RunSocketWriter() {
break break
} }
} }
// empty the lineToSendExists channel
for 0 < len(socket.lineToSendExists) {
<-socket.lineToSendExists
}
//TODO(dan): empty socket.lineToSendExists queue //TODO(dan): empty socket.lineToSendExists queue
socket.conn.Close() socket.conn.Close()
if !socket.Closed { if !socket.Closed {
socket.Closed = true socket.Closed = true
} }
// empty the lineToSendExists channel
for 0 < len(socket.lineToSendExists) {
<-socket.lineToSendExists
}
} }
// WriteLine writes the given line out of Socket. // WriteLine writes the given line out of Socket.