Skip to content

Commit 5995a5b

Browse files
committed
fix: trigger write event only when flush with read throttled
1 parent f00f2b8 commit 5995a5b

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

connection_impl.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (c *connection) waitRead(n int) (err error) {
415415
goto CLEANUP
416416
}
417417
// wait full n
418-
for c.inputBuffer.Len() < n {
418+
for c.inputBuffer.Len() < n && err == nil {
419419
switch c.status(closing) {
420420
case poller:
421421
err = Exception(ErrEOF, "wait read")
@@ -424,9 +424,6 @@ func (c *connection) waitRead(n int) (err error) {
424424
default:
425425
err = <-c.readTrigger
426426
}
427-
if err != nil {
428-
goto CLEANUP
429-
}
430427
}
431428
CLEANUP:
432429
atomic.StoreInt64(&c.waitReadSize, 0)
@@ -506,7 +503,13 @@ func (c *connection) flush() error {
506503
if c.outputBuffer.IsEmpty() {
507504
return nil
508505
}
509-
err = c.operator.Control(PollR2RW)
506+
if c.operator.getMode() == ophup {
507+
// triggered read throttled, so here shouldn't trigger read event again
508+
err = c.operator.Control(PollHup2W)
509+
} else {
510+
err = c.operator.Control(PollR2RW)
511+
}
512+
c.operator.done()
510513
if err != nil {
511514
return Exception(err, "when flush")
512515
}

poll.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,7 @@ const (
7272
// PollR2Hup is used to remove the readable monitor of FDOperator.
7373
PollR2Hup PollEvent = 0x9
7474
// PollHup2R is used to add the readable monitor of FDOperator, generally used with PollR2Hup.
75-
PollHup2R PollEvent = 0x10
75+
PollHup2R PollEvent = 0xA
76+
// PollHup2W is used to add the writeable monitor of FDOperator.
77+
PollHup2W PollEvent = 0xB
7678
)

poll_default_bsd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ func (p *defaultPoll) Control(operator *FDOperator, event PollEvent) error {
218218
case PollHup2R:
219219
operator.setMode(opread)
220220
evs[0].Filter, evs[0].Flags = syscall.EVFILT_READ, syscall.EV_ADD|syscall.EV_ENABLE
221+
case PollHup2W:
222+
operator.setMode(opwrite)
223+
evs[0].Filter, evs[0].Flags = syscall.EVFILT_WRITE, syscall.EV_ADD|syscall.EV_ENABLE
221224
}
222225
_, err := syscall.Kevent(p.fd, evs, nil, nil)
223226
return err

poll_default_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ func (p *defaultPoll) Control(operator *FDOperator, event PollEvent) error {
277277
case PollHup2R:
278278
operator.setMode(opread)
279279
op, evt.events = syscall.EPOLL_CTL_MOD, syscall.EPOLLIN|syscall.EPOLLRDHUP|syscall.EPOLLERR
280+
case PollHup2W:
281+
operator.setMode(opwrite)
282+
op, evt.events = syscall.EPOLL_CTL_MOD, syscall.EPOLLOUT|syscall.EPOLLRDHUP|syscall.EPOLLERR
280283
}
281284
return EpollCtl(p.fd, op, operator.FD, &evt)
282285
}

0 commit comments

Comments
 (0)