Skip to content

Commit

Permalink
opt: reduce duplicate code for I/O reactors
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Apr 23, 2024
1 parent 5cca785 commit a5bc95b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 201 deletions.
23 changes: 17 additions & 6 deletions reactor_epoll_default.go → reactor_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux && !poll_opt
// +build linux,!poll_opt
//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && !poll_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
// +build !poll_opt

package gnet

Expand Down Expand Up @@ -53,8 +54,13 @@ func (el *eventloop) orbit() error {
err := el.poller.Polling(func(fd int, ev netpoll.IOEvent, flags netpoll.IOFlags) error {
c := el.connections.getConn(fd)
if c == nil {
// Somehow epoll notified with an event for a stale fd that is not in our connection set.
// We need to delete it from the epoll set.
// For kqueue, this might happen when the connection has already been closed,
// the file descriptor will be deleted from kqueue automatically as documented
// in the manual pages.
// For epoll, it somehow notified with an event for a stale fd that is not in
// our connection set. We need to explicitly delete it from the epoll set.
// Also print a warning log for this kind of irregularity.
el.getLogger().Warnf("received event[fd=%d|ev=%d|flags=%d] of a stale connection from event-loop(%d)", fd, ev, flags, el.idx)

Check warning on line 63 in reactor_default.go

View check run for this annotation

Codecov / codecov/patch

reactor_default.go#L63

Added line #L63 was not covered by tests
return el.poller.Delete(fd)
}
return c.processIO(fd, ev, flags)
Expand Down Expand Up @@ -84,8 +90,13 @@ func (el *eventloop) run() error {
if _, ok := el.listeners[fd]; ok {
return el.accept(fd, ev, flags)
}
// Somehow epoll notified with an event for a stale fd that is not in our connection set.
// We need to delete it from the epoll set.
// For kqueue, this might happen when the connection has already been closed,
// the file descriptor will be deleted from kqueue automatically as documented
// in the manual pages.
// For epoll, it somehow notified with an event for a stale fd that is not in
// our connection set. We need to explicitly delete it from the epoll set.
// Also print a warning log for this kind of irregularity.
el.getLogger().Warnf("received event[fd=%d|ev=%d|flags=%d] of a stale connection from event-loop(%d)", fd, ev, flags, el.idx)
return el.poller.Delete(fd)
}
return c.processIO(fd, ev, flags)
Expand Down
84 changes: 0 additions & 84 deletions reactor_epoll_ultimate.go

This file was deleted.

109 changes: 0 additions & 109 deletions reactor_kqueue_default.go

This file was deleted.

4 changes: 2 additions & 2 deletions reactor_kqueue_ultimate.go → reactor_ultimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (freebsd || dragonfly || netbsd || openbsd || darwin) && poll_opt
// +build freebsd dragonfly netbsd openbsd darwin
//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && poll_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
// +build poll_opt

package gnet
Expand Down

0 comments on commit a5bc95b

Please sign in to comment.