| 1 | package IPC::Manager::Client::AtomicPipe; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '0.000036'; | ||
| 6 | |||
| 7 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Atomic::Pipe; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Carp qw/croak/; | ||
| 10 | use POSIX qw/mkfifo/; | ||
| 11 | |||
| 12 | use parent 'IPC::Manager::Base::FS::Handle'; | ||
| 13 | use Object::HashBase qw{ | ||
| 14 | permissions | ||
| 15 | +pipe | ||
| 16 | +pipe_cache | ||
| 17 | }; | ||
| 18 | |||
| 19 | use Role::Tiny::With; | ||
| 20 | with 'IPC::Manager::Role::Outbox'; | ||
| 21 | |||
| 22 | sub _viable { require Atomic::Pipe; Atomic::Pipe->VERSION('0.026'); 1 } | ||
| 23 | sub suspend_supported { 0 } | ||
| 24 | |||
| 25 | sub check_path { -p $_[1] } | ||
| 26 | sub path_type { 'FIFO' } | ||
| 27 | |||
| 28 | sub have_handles_for_select { 1 } | ||
| 29 | sub handles_for_select { $_[0]->{+PIPE} ? $_[0]->{+PIPE}->rh : () } | ||
| 30 | |||
| 31 | sub make_path { | ||
| 32 | my $self = shift; | ||
| 33 | my $path = $self->path; | ||
| 34 | my $perms = $self->{+PERMISSIONS} //= 0700; | ||
| 35 | mkfifo($path, $perms) or die "Failed to make fifo '$path': $!"; | ||
| 36 | my $p = Atomic::Pipe->read_fifo($path); | ||
| 37 | $p->blocking(0); | ||
| 38 | $p->resize_or_max($p->max_size) if $p->max_size; | ||
| 39 | |||
| 40 | $self->{+PIPE} = $p; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub pre_disconnect_hook { | ||
| 44 | my $self = shift; | ||
| 45 | unlink($self->{+PATH}) or warn "Could not unlink fifo: $!"; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub init { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | $self->SUPER::init(); | ||
| 52 | |||
| 53 | $self->{+BUFFER} //= []; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub pre_suspend_hook { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | $self->pid_check; | ||
| 60 | |||
| 61 | # Get all messages and re-queue them | ||
| 62 | my $p = $self->{+PIPE}; | ||
| 63 | |||
| 64 | my @msgs; | ||
| 65 | while ($p->{$p->IN_BUFFER_SIZE}) { | ||
| 66 | push @msgs => $p->read_message; | ||
| 67 | } | ||
| 68 | |||
| 69 | if (@msgs) { | ||
| 70 | $self->requeue_message(@msgs); | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->SUPER::pre_suspend_hook(@_); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub fill_buffer { | ||
| 77 | my $self = shift; | ||
| 78 | my $msg = $self->{+PIPE}->read_message // return @{$self->{+BUFFER}} ? 1 : 0; | ||
| 79 | push @{$self->{+BUFFER}} => $msg; | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _process_msg { | ||
| 84 | my $self = shift; | ||
| 85 | my ($in) = @_; | ||
| 86 | |||
| 87 | my $msg = IPC::Manager::Message->new($self->{+SERIALIZER}->deserialize($in)); | ||
| 88 | $self->{+STATS}->{read}->{$msg->{from}}++; | ||
| 89 | return $msg; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get_messages { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | my $p = $self->{+PIPE}; | ||
| 96 | |||
| 97 | my @out; | ||
| 98 | |||
| 99 | push @out => $self->read_resume_file; | ||
| 100 | |||
| 101 | push @out => map { $self->_process_msg($_) } @{$self->{+BUFFER}}; | ||
| 102 | |||
| 103 | while (my $msg = $p->read_message) { | ||
| 104 | push @out => $self->_process_msg($msg); | ||
| 105 | } | ||
| 106 | |||
| 107 | @{$self->{+BUFFER}} = (); | ||
| 108 | |||
| 109 | return $self->sort_messages(@out); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub peer_left { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | my $p = $self->{+PIPE} or return 0; | ||
| 116 | my $state = $p->{Atomic::Pipe::STATE()} or return 0; | ||
| 117 | |||
| 118 | my %tags; | ||
| 119 | $tags{$_} = 1 for keys %{$state->{parts} // {}}; | ||
| 120 | $tags{$_} = 1 for keys %{$state->{buffers} // {}}; | ||
| 121 | |||
| 122 | my $removed = 0; | ||
| 123 | for my $tag (keys %tags) { | ||
| 124 | my ($pid) = split /:/, $tag, 2; | ||
| 125 | next unless $pid && $pid =~ m/^-?\d+$/; | ||
| 126 | |||
| 127 | # pid_is_running returns 1 (ours), -1 (running but not ours), or 0 | ||
| 128 | # (gone). Only clear the tag when the pid is genuinely gone. | ||
| 129 | next if $self->pid_is_running($pid); | ||
| 130 | |||
| 131 | delete $state->{parts}->{$tag}; | ||
| 132 | delete $state->{buffers}->{$tag}; | ||
| 133 | $removed++; | ||
| 134 | } | ||
| 135 | |||
| 136 | return $removed; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _peer_pipe { | ||
| 140 | my ($self, $peer) = @_; | ||
| 141 | |||
| 142 | my $fifo = $self->peer_exists($peer) or return undef; | ||
| 143 | return $self->{+PIPE_CACHE}->{$fifo} //= do { | ||
| 144 | my $w = Atomic::Pipe->write_fifo($fifo); | ||
| 145 | $w->write_blocking($self->send_blocking ? 1 : 0); | ||
| 146 | $w; | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _outbox_set_blocking { | ||
| 151 | my ($self, $bool) = @_; | ||
| 152 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 153 | $p->write_blocking($bool ? 1 : 0); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _outbox_can_send { | ||
| 158 | my ($self, $peer) = @_; | ||
| 159 | |||
| 160 | my $p = $self->_peer_pipe($peer) or return 0; | ||
| 161 | |||
| 162 | require IO::Select; | ||
| 163 | my $s = IO::Select->new($p->wh); | ||
| 164 | return $s->can_write(0) ? 1 : 0; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _outbox_try_write { | ||
| 168 | my ($self, $peer, $payload) = @_; | ||
| 169 | |||
| 170 | $self->pid_check; | ||
| 171 | my $p = $self->_peer_pipe($peer) | ||
| 172 | or die "'$peer' is not a valid message recipient"; | ||
| 173 | |||
| 174 | # Non-blocking write. write_message pushes into OUT_BUFFER and | ||
| 175 | # calls flush(). With write_blocking(0) on the underlying pipe, | ||
| 176 | # flush() partial-writes on EAGAIN and leaves the unwritten tail | ||
| 177 | # in OUT_BUFFER for a later drain_pending to resume. | ||
| 178 | $p->write_message($payload); | ||
| 179 | |||
| 180 | return 0 if $p->pending_output; | ||
| 181 | |||
| 182 | $self->{+STATS}->{sent}->{$peer}++; | ||
| 183 | return 1; | ||
| 184 | } | ||
| 185 | |||
| 186 | # Atomic::Pipe owns its own OUT_BUFFER, which IS the per-peer | ||
| 187 | # queue. Role::Outbox's _OUTBOX would double-buffer the payload | ||
| 188 | # (write_message pushes into OUT_BUFFER, then drain_pending replays | ||
| 189 | # the same payload, which write_message then enqueues a SECOND | ||
| 190 | # time). The next four methods bypass _OUTBOX and use OUT_BUFFER | ||
| 191 | # directly so each payload reaches the kernel exactly once. | ||
| 192 | |||
| 193 | sub try_send_message { | ||
| 194 | my $self = shift; | ||
| 195 | |||
| 196 | # Mirror send_message's call signature so callers can pass | ||
| 197 | # ($peer, \%content) and friends without having to serialize. | ||
| 198 | # The pre-serialized fast path is ($peer, $string). | ||
| 199 | my ($peer, $payload); | ||
| 200 | if (@_ >= 2 && ref($_[1]) eq '' && !ref($_[0])) { | ||
| 201 | ($peer, $payload) = @_; | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | my $msg = $self->build_message(@_); | ||
| 205 | $peer = $msg->to or croak "No peer specified"; | ||
| 206 | $payload = $self->{+SERIALIZER}->serialize($msg); | ||
| 207 | } | ||
| 208 | |||
| 209 | $self->pid_check; | ||
| 210 | my $p = $self->_peer_pipe($peer) | ||
| 211 | or die "'$peer' is not a valid message recipient"; | ||
| 212 | |||
| 213 | $p->write_message($payload); | ||
| 214 | |||
| 215 | return 0 if $p->pending_output; | ||
| 216 | |||
| 217 | $self->{+STATS}->{sent}->{$peer}++; | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub drain_pending { | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | my $delivered = 0; | ||
| 225 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 226 | next unless $p->pending_output; | ||
| 227 | $p->flush; | ||
| 228 | $delivered++ unless $p->pending_output; | ||
| 229 | } | ||
| 230 | return $delivered; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub pending_sends { | ||
| 234 | my $self = shift; | ||
| 235 | my $n = 0; | ||
| 236 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 237 | $n++ if $p->pending_output; | ||
| 238 | } | ||
| 239 | return $n; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub have_pending_sends { | ||
| 243 | my $self = shift; | ||
| 244 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 245 | return 1 if $p->pending_output; | ||
| 246 | } | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub pending_sends_to { | ||
| 251 | my ($self, $peer) = @_; | ||
| 252 | my $fifo = $self->peer_exists($peer) or return 0; | ||
| 253 | my $p = $self->{+PIPE_CACHE}->{$fifo} or return 0; | ||
| 254 | return $p->pending_output ? 1 : 0; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub have_writable_handles { | ||
| 258 | my $self = shift; | ||
| 259 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 260 | return 1 if $p->pending_output; | ||
| 261 | } | ||
| 262 | return 0; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub writable_handles { | ||
| 266 | my $self = shift; | ||
| 267 | my @h; | ||
| 268 | for my $p (values %{$self->{+PIPE_CACHE} // {}}) { | ||
| 269 | next unless $p->pending_output; | ||
| 270 | push @h => $p->wh; | ||
| 271 | } | ||
| 272 | return @h; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub _outbox_writable_handle { | ||
| 276 | my ($self, $peer) = @_; | ||
| 277 | my $p = $self->_peer_pipe($peer) or return undef; | ||
| 278 | return $p->wh; | ||
| 279 | } | ||
| 280 | |||
| 281 | # send_message respects send_blocking. In blocking mode, drain any | ||
| 282 | # backlog blocking, then write blocking. In non-blocking mode, | ||
| 283 | # delegate to try_send_message. | ||
| 284 | sub send_message { | ||
| 285 | my $self = shift; | ||
| 286 | my $msg = $self->build_message(@_); | ||
| 287 | |||
| 288 | my $peer_id = $msg->to or croak "No peer specified"; | ||
| 289 | my $payload = $self->{+SERIALIZER}->serialize($msg); | ||
| 290 | |||
| 291 | $self->pid_check; | ||
| 292 | |||
| 293 | if ($self->send_blocking) { | ||
| 294 | $self->_drain_blocking($peer_id) if $self->pending_sends_to($peer_id); | ||
| 295 | |||
| 296 | my $p = $self->_peer_pipe($peer_id) | ||
| 297 | or die "'$peer_id' is not a valid message recipient"; | ||
| 298 | |||
| 299 | $p->write_message($payload); | ||
| 300 | $p->flush(blocking => 1); | ||
| 301 | $self->{+STATS}->{sent}->{$peer_id}++; | ||
| 302 | return 1; | ||
| 303 | } | ||
| 304 | |||
| 305 | return $self->try_send_message($peer_id, $payload); | ||
| 306 | } | ||
| 307 | |||
| 308 | sub _drain_blocking { | ||
| 309 | my ($self, $peer) = @_; | ||
| 310 | |||
| 311 | my $p = $self->_peer_pipe($peer) or return; | ||
| 312 | |||
| 313 | # AtomicPipe never populates _OUTBOX (the pipe's OUT_BUFFER is | ||
| 314 | # the queue). Flushing pending_output blocking drains it. | ||
| 315 | $p->flush(blocking => 1) if $p->pending_output; | ||
| 316 | } | ||
| 317 | |||
| 318 | 1; | ||
| 319 | |||
| 320 | __END__ | ||