From aa0102f777d0155fa098b1e79eb9f4e8d1769279 Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Sat, 16 Oct 2021 09:29:50 +0000
Subject: [PATCH] wqworker: favor level-triggered epoll for fairness

Sigfd->event_step needs priority over WQWorkers (and everything
else).  Do that by running once per event_loop iteration rather
than looping inside event_step.  This lowers throughput since it
requires more syscalls, but that's the price of fairness.
---
 lib/PublicInbox/WQWorker.pm | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/lib/PublicInbox/WQWorker.pm b/lib/PublicInbox/WQWorker.pm
index 48b901bb..950bd170 100644
--- a/lib/PublicInbox/WQWorker.pm
+++ b/lib/PublicInbox/WQWorker.pm
@@ -6,7 +6,7 @@ package PublicInbox::WQWorker;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::DS);
-use PublicInbox::Syscall qw(EPOLLIN EPOLLEXCLUSIVE EPOLLET);
+use PublicInbox::Syscall qw(EPOLLIN EPOLLEXCLUSIVE);
 use Errno qw(EAGAIN ECONNRESET);
 use IO::Handle (); # blocking
 
@@ -14,19 +14,19 @@ sub new {
 	my ($cls, $wq, $sock) = @_;
 	$sock->blocking(0);
 	my $self = bless { sock => $sock, wq => $wq }, $cls;
-	$self->SUPER::new($sock, EPOLLEXCLUSIVE|EPOLLIN|EPOLLET);
+	$self->SUPER::new($sock, EPOLLEXCLUSIVE|EPOLLIN);
 	$self;
 }
 
 sub event_step {
 	my ($self) = @_;
-	my $n;
-	do {
-		$n = $self->{wq}->recv_and_run($self->{sock});
-	} while ($n);
-	return if !defined($n) && $! == EAGAIN; # likely
-	warn "wq worker error: $!\n" if !defined($n) && $! != ECONNRESET;
-	$self->{wq}->wq_atexit_child if $self->{sock} == $self->{wq}->{-wq_s2};
+	my $n = $self->{wq}->recv_and_run($self->{sock}) and return;
+	unless (defined $n) {
+		return if $! == EAGAIN;
+		warn "recvmsg: $!" if $! != ECONNRESET;
+	}
+	$self->{sock} == $self->{wq}->{-wq_s2} and
+		$self->{wq}->wq_atexit_child;
 	$self->close; # PublicInbox::DS::close
 }
 
-- 
2.51.0