Data-ReqRep-Shared
view release on metacpan or search on metacpan
lib/Data/ReqRep/Shared.pm view on Meta::CPAN
$cli->req_eventfd_set($req_fd);
$cli->eventfd_set($rep_fd);
$cli->send_notify($data); # wakes server
# EV::io $rep_fd for reply ...
exit;
}
# parent = server
my $w = EV::io $req_fd, EV::READ, sub {
$srv->eventfd_consume;
while (my ($req, $id) = $srv->recv) {
$srv->reply($id, process($req));
}
$srv->reply_notify;
};
=head2 Crash Safety
=over
=item * B<Stale mutex> -- if a process dies holding the request queue
mutex, other processes detect it via PID tracking and recover within
2 seconds.
=item * B<Stale response slots> -- if a client dies while holding a
slot (ACQUIRED or READY state), the slot is reclaimed automatically
during the next slot acquisition scan.
=item * B<ABA protection> -- response slot IDs carry a generation
counter. A cancelled-and-reacquired slot has a different generation,
so stale C<reply>/C<get>/C<cancel> calls are safely rejected.
=back
=head2 Tuning
=over
=item C<req_cap> -- request queue capacity (power of 2). Higher for
bursty workloads (1024-4096), lower for steady-state (64-256).
Memory: 24 bytes/slot + arena (Str) or 24 bytes/slot (Int).
=item C<resp_slots> -- max concurrent in-flight requests across all
clients. One slot per outstanding async request. For synchronous
C<req()>, one per client suffices. Memory: 64 bytes/slot (Int) or
(32 + C<resp_size> rounded up to 64) bytes/slot (Str).
=item C<resp_size> -- max response payload bytes (Str only). Fixed
per slot. Responses exceeding this croak. Pick the 99th percentile.
=item C<arena> -- request data arena bytes (Str only, default
C<req_cap * 256>). Increase for large requests. Monitor
C<arena_used> in C<stats()>.
=back
=head2 Benchmarks
Linux x86_64. Run C<perl -Mblib bench/vs.pl 50000> to reproduce.
SINGLE-PROCESS ECHO (200K iterations)
ReqRep::Int (lock-free) 1.8M req/s
ReqRep::Str (12B, mutex) 1.2M req/s
ReqRep::Str batch (100x) 1.4M req/s
CROSS-PROCESS ECHO (50K iterations, 12B payload)
Pipe pair (1:1) 240K req/s
Unix socketpair (1:1) 222K req/s
ReqRep::Int 202K req/s *
ReqRep::Str 177K req/s *
IPC::Msg (SysV) 165K req/s
TCP loopback 115K req/s
MCE::Channel 96K req/s
Socketpair via broker 82K req/s
Forks::Queue (Shmem) 5K req/s
C<*> = MPMC with per-request reply routing. Pipes and sockets are
faster for simple 1:1 echo but require dedicated fd pairs per
client-worker connection and cannot do MPMC without a broker (which
halves throughput).
=head1 SEE ALSO
L<Data::Buffer::Shared> - typed shared array
L<Data::HashMap::Shared> - concurrent hash table
L<Data::Queue::Shared> - FIFO queue
L<Data::PubSub::Shared> - publish-subscribe ring
L<Data::Sync::Shared> - synchronization primitives
L<Data::Pool::Shared> - fixed-size object pool
L<Data::Stack::Shared> - LIFO stack
L<Data::Deque::Shared> - double-ended queue
L<Data::Log::Shared> - append-only log (WAL)
L<Data::Heap::Shared> - priority queue
L<Data::Graph::Shared> - directed weighted graph
L<Data::BitSet::Shared> - shared bitset (lock-free per-bit ops)
L<Data::RingBuffer::Shared> - fixed-size overwriting ring buffer
=head1 AUTHOR
vividsnow
=head1 LICENSE
This is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
( run in 0.343 second using v1.01-cache-2.11-cpan-71847e10f99 )