Data-ReqRep-Shared
view release on metacpan or search on metacpan
$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)
Client API
Synchronous:
my $resp = $cli->req($data); # infinite wait
my $resp = $cli->req_wait($data, $secs); # single deadline
Both return undef if no reply arrives: "req_wait" on its timeout, and
either at once when "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 "CRASH SAFETY").
Perl signal handlers, "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: "req" and "req_wait" cancel their
request, while "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
("POSIX::sigaction" without "safe", "Sys::SigAction",
"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
"SIGKILL" and "SIGSTOP" waits, about 0.2 seconds per GiB on tmpfs and the
whole write-out on a filesystem without "fallocate"; one that then ends
the process leaves a file-backed channel's file as an interrupted create
(see "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 "recv_wait" rather than a dying
handler, then answer what is queued with one "drain", as
eg/graceful_shutdown.pl does, or on Int, which has no "drain", with at
most "size" "recv" calls: a "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: "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 "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.
"req_wait" reads an undef timeout as 0; the other waits read it as no
timeout.
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: "send" and
"send_notify" when the queue is full or no response slot is free,
"send_wait" and "send_wait_notify" when the timeout passes first.
"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 "req_wait" or was destroyed, does
its work, and gets false from "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 "cancel": the responder frees the slot when its copy
finishes. A "get_wait" that times out leaves the request in flight: wait
again or "cancel" it, or its slot stays held until the client handle is
destroyed. An undef sooner than the timeout, or from a "get_wait" without
one, means no reply will come (its server died, "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 "get" and "get_wait" croak and
"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 "get" with its parent's id receives the reply and
frees the slot, and the parent then gets undef.
Convenience (Str only):
my $id = $cli->send_notify($data); # send + eventfd signal
my $id = $cli->send_wait_notify($data, $secs);
Status:
$cli->pending; $cli->size; $cli->capacity;
$cli->is_empty; $cli->resp_slots; $cli->resp_size;
$cli->stats; $cli->path; $cli->memfd;
"pending" counts the requests this process has in flight, from all its
handles on the channel.
eventfd (see "Event Loop Integration"):
$cli->eventfd; $cli->eventfd_set($fd);
$cli->eventfd_consume; $cli->fileno;
$cli->notify; # signal request eventfd
( run in 0.719 second using v1.01-cache-2.11-cpan-60884a8df76 )