mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 02:48:59 +00:00
chore(protocol): prioritize closing a connection (#9711)
The read/write loops may keep going for a while on a closing connection with lots of read/write activity, as it's random which select case is chosen. And if the connection is slow or even broken, a single read/write can take a long time/until timeout. Add initial non-blocking selects with only the cases relevant to closing, to prioritize those.
This commit is contained in:
parent
2238a288d9
commit
1f4fde9525
@ -465,6 +465,11 @@ func (c *rawConnection) dispatcherLoop() (err error) {
|
||||
var msg message
|
||||
state := stateInitial
|
||||
for {
|
||||
select {
|
||||
case <-c.closed:
|
||||
return ErrClosed
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case msg = <-c.inbox:
|
||||
case <-c.closed:
|
||||
@ -758,6 +763,17 @@ func (c *rawConnection) writerLoop() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
// When the connection is closing or closed, that should happen
|
||||
// immediately, not compete with the (potentially very busy) outbox.
|
||||
select {
|
||||
case hm := <-c.closeBox:
|
||||
_ = c.writeMessage(hm.msg)
|
||||
close(hm.done)
|
||||
return
|
||||
case <-c.closed:
|
||||
return
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case cc := <-c.clusterConfigBox:
|
||||
err := c.writeMessage(cc)
|
||||
|
Loading…
Reference in New Issue
Block a user