Event loops: sockets registered once, readiness kept until not found

A fiber waiting on a socket registered it with its loop and took it
off again on every wait: two epoll_ctl() calls a wait, some 39 for an
exchange of the mix benchmark, about an eighth of the proxy's time. A
socket is now registered once, edge-triggered for reading and writing,
until it is closed, and the loop keeps what it was reported ready for
until an operation finds it not to be: read, write, accept, connect,
splice() from a pipe and TLS tell it so through fiberblockfunc, the
close through fiberclosefunc. A wait for what a socket is known to be
ready for is answered at once, with no system call and no switch; only
with none of them ready does the fiber wait in its loop. Readiness is
trusted once: answered again with nothing reported or found since, what
was there may have been taken without its end being found, and the
system is asked with poll(). epoll and kqueue only; with poll() each
wait still registers its sockets.

What takes readability for an end rather than reading, the control
connection of a UDP association, the client waiting for a SOCKS BIND,
the server between requests of a kept-alive HTTP connection, asks
first with sockreadable(), a peek below any TLS layer.

kqueue: the end of either side, the peer's or our own shutdown, is
readable or writable, not a hang up, which poll() reports only with
both: kept, it made a wait for the answer after our shutdown return
at once, and sockmap give up with 93.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-25 12:41:47 +03:00
1 parent 43f40569d4
commit 7b873b80b1
10 files changed
+263 -15

No files matched your search

+8 -3
View File
@@ -148,13 +148,14 @@ static void wakedrain(struct loop *l){
static int kqarm(struct loop *l, struct loopio *io, short events){
struct kevent ch[2];
int n = 0;
unsigned short add = EV_ADD | (io->edge? EV_CLEAR : 0);
if((events & POLLIN) && !(io->armed & POLLIN))
EV_SET(ch + n++, io->fd, EVFILT_READ, EV_ADD, 0, 0, io);
EV_SET(ch + n++, io->fd, EVFILT_READ, add, 0, 0, io);
else if(!(events & POLLIN) && (io->armed & POLLIN))
EV_SET(ch + n++, io->fd, EVFILT_READ, EV_DELETE, 0, 0, io);
if((events & POLLOUT) && !(io->armed & POLLOUT))
EV_SET(ch + n++, io->fd, EVFILT_WRITE, EV_ADD, 0, 0, io);
EV_SET(ch + n++, io->fd, EVFILT_WRITE, add, 0, 0, io);
else if(!(events & POLLOUT) && (io->armed & POLLOUT))
EV_SET(ch + n++, io->fd, EVFILT_WRITE, EV_DELETE, 0, 0, io);
if(n && kevent(l->fd, ch, n, NULL, 0, NULL) && (events & ~io->armed)) return 1;
@@ -172,6 +173,7 @@ static int eparm(struct loop *l, struct loopio *io, short events){
ev.data.ptr = io;
if(events & POLLIN) ev.events |= EPOLLIN;
if(events & POLLOUT) ev.events |= EPOLLOUT;
if(io->edge) ev.events |= EPOLLET;
/* not registered while nothing is waited for: an error or a hang up
would be reported all the same */
if(!events) op = EPOLL_CTL_DEL;
@@ -665,7 +667,10 @@ int looponce(struct loop *l, int maxwait){
if(ev[i].flags & EV_ERROR) revents |= POLLERR;
else if(ev[i].filter == EVFILT_READ) revents |= POLLIN;
else if(ev[i].filter == EVFILT_WRITE) revents |= POLLOUT;
if(ev[i].flags & EV_EOF) revents |= POLLHUP;
/* The end of either side, the peer's or our own shutdown,
is no hang up, which poll() reports only with both:
readable or writable, the read or the write telling
what it is */
readyadd(l, io, revents);
}
}