Data-ReqRep-Shared
view release on metacpan or search on metacpan
lib/Data/ReqRep/Shared.pm view on Meta::CPAN
$srv->eventfd; $srv->eventfd_set($fd);
$srv->eventfd_consume; $srv->notify;
$srv->fileno; # current request eventfd (-1 if none)
$srv->reply_eventfd; $srv->reply_eventfd_set($fd);
$srv->reply_eventfd_consume; $srv->reply_notify;
$srv->reply_fileno; # current reply eventfd (-1 if none)
=head2 Client API
B<Synchronous>:
my $resp = $cli->req($data); # infinite wait
my $resp = $cli->req_wait($data, $secs); # single deadline
Both return undef if no reply arrives: C<req_wait> on its timeout, and either
at once when C<clear> discards the request, or within two seconds of the server
that received the request dying before its reply is complete (for the
exceptions see L</CRASH SAFETY>).
Perl signal handlers, C<alarm> included, run while a call waits for a request,
a reply, a free slot, queue room or the Str queue mutex. A handler that dies
ends the call: C<req> and C<req_wait> cancel their request, while C<get_wait>
leaves it in flight as its timeout would. One that returns lets the call carry
on waiting. A call can return just as a handler is due, and if that handler
dies what the call took is lost: a server's request goes unreplied and its
client waits until its timeout, or for ever without one while that server
lives; a client's reply is gone and waiting for it again returns undef. A
constructor waiting for the file lock runs handlers only once that wait ends,
and if 120 signals arrive meanwhile Perl dies of them, leaving that signal
blocked. All this holds for Perl's default, deferred handlers: one installed
unsafely (C<POSIX::sigaction> without C<safe>, C<Sys::SigAction>,
C<PERL_SIGNALS=unsafe>) runs inside the module's C code, and if it dies there
it can leave a slot, arena room or the queue mutex held until the process
exits. While a constructor reserves the segment, every signal but C<SIGKILL>
and C<SIGSTOP> waits, about 0.2 seconds per GiB on tmpfs and the whole
write-out on a filesystem without C<fallocate>; one that then ends the process
leaves a file-backed channel's file as an interrupted create (see
L</CRASH SAFETY>) holding the reserved space, while a memfd or anonymous
channel's goes with the process.
Stop a server with a flag and a timed C<recv_wait> rather than a dying
handler, then answer what is queued with one C<drain>, as
F<eg/graceful_shutdown.pl> does, or on Int, which has no C<drain>, with at most
C<size> C<recv> calls: a C<recv> loop until the queue is empty keeps taking
new requests for as long as each arrives before the queue empties, which a few
clients sending in a loop keep up even when each waits for its reply. A
request sent after that last take waits for the next server to open the same
file (one that removes it and creates it again never sees it), and without a
timeout its client waits for ever: stop the senders first, and in a chain of
channels let each stage exit before stopping the one it sends to.
A timeout of 0, like the non-blocking calls, never waits for a request, a
reply, a slot or room: C<req_wait> with one still sends the request, then
gives it up at once. Such a call on a Str channel still waits for the queue
mutex, for two seconds at most when the process holding it is stopped, and
gives up as soon as a signal arrives; its handler runs once the call returns.
A timed call keeps its deadline behind a stopped holder, except that
C<recv_wait_multi> taking the rest of its batch, and a send giving back arena
room, its own as it times out or a dead process's, may each run up to two
seconds over. A negative timeout, or NaN, waits for ever like no timeout at
all, so clamp a computed remaining time at 0. C<req_wait> reads an undef
timeout as 0; the other waits read it as no timeout.
B<Asynchronous>:
my $id = $cli->send($data); # non-blocking
my $id = $cli->send_wait($data, $secs); # blocking
my $resp = $cli->get($id); # non-blocking
my $resp = $cli->get_wait($id, $secs); # blocking
$cli->cancel($id); # abandon request
A send returns undef when its request was not queued: C<send> and
C<send_notify> when the queue is full or no response slot is free,
C<send_wait> and C<send_wait_notify> when the timeout passes first.
C<cancel> releases the slot, dropping the reply if it has already arrived. It
does not take the request back: a server still receives a request whose client
cancelled it, gave up on it in C<req_wait> or was destroyed, does its work, and
gets false from C<reply>. A client that retries on timeout therefore adds work
to a server that is behind; carry a deadline in the request if servers should
skip stale ones. A reply still being copied in does not delay C<cancel>: the
responder frees the slot when its copy finishes. A C<get_wait> that times out
leaves the request in flight: wait again or C<cancel> it, or its slot stays
held until the client handle is destroyed. An undef sooner than the timeout, or
from a C<get_wait> without one, means no reply will come (its server died,
C<clear> ran, or another process took the reply), and waiting again returns
undef at once. An id that names no slot of the channel makes C<get> and
C<get_wait> croak and C<cancel> do nothing.
Destroying a client cancels its requests still in flight, freeing their slots;
a forked child destroying its copy of the parent's client leaves them alone.
Destroying a server handle gives back nothing it has received: the requests
stay with the process, which may still reply to them through another handle.
A reply is taken by whichever process reads it with the request's id: a forked
child that calls C<get> with its parent's id receives the reply and frees the
slot, and the parent then gets undef.
B<Convenience> (Str only):
my $id = $cli->send_notify($data); # send + eventfd signal
my $id = $cli->send_wait_notify($data, $secs);
B<Status>:
$cli->pending; $cli->size; $cli->capacity;
$cli->is_empty; $cli->resp_slots; $cli->resp_size;
$cli->stats; $cli->path; $cli->memfd;
C<pending> counts the requests this process has in flight, from all its
handles on the channel.
B<eventfd> (see L</Event Loop Integration>):
$cli->eventfd; $cli->eventfd_set($fd);
$cli->eventfd_consume; $cli->fileno;
$cli->notify; # signal request eventfd
$cli->req_eventfd_set($fd); $cli->req_fileno;
$cli->ready_fd; $cli->ready; # this client's own replies
( run in 1.587 second using v1.01-cache-2.11-cpan-60884a8df76 )