From ffd40d6260b01290b2dd060f69ed2c663eba4027 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 24 Sep 2015 21:28:51 +0000 Subject: nntpd: hoist out daemon management code We'll probably be supporting read-only IMAP, or maybe we'll just implement a custom HTTP server so users can manage/upgrade the same way as the nntpd while being immune to slow clients. --- lib/PublicInbox/Listener.pm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/PublicInbox/Listener.pm (limited to 'lib/PublicInbox/Listener.pm') diff --git a/lib/PublicInbox/Listener.pm b/lib/PublicInbox/Listener.pm new file mode 100644 index 00000000..7f9658b8 --- /dev/null +++ b/lib/PublicInbox/Listener.pm @@ -0,0 +1,34 @@ +# Copyright (C) 2015 all contributors +# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) +package PublicInbox::Listener; +use strict; +use warnings; +use base 'Danga::Socket'; +use Socket qw(SOL_SOCKET SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY); +use fields qw(post_accept); +require IO::Handle; + +sub new ($$$) { + my ($class, $s, $cb) = @_; + setsockopt($s, SOL_SOCKET, SO_KEEPALIVE, 1); + setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1); + listen($s, 1024); + IO::Handle::blocking($s, 0); + my $self = fields::new($class); + $self->SUPER::new($s); # calls epoll_create for the first socket + $self->watch_read(1); + $self->{post_accept} = $cb; + $self +} + +sub event_read { + my ($self) = @_; + # no loop here, we want to fairly distribute clients + # between multiple processes sharing the same socket + if (accept(my $c, $self->{sock})) { + IO::Handle::blocking($c, 0); # no accept4 :< + $self->{post_accept}->($c); + } +} + +1; -- cgit v1.2.3-24-ge0c7