| 1 | package PAGI::Server::TransportState; | ||||||||||||
| 2 | |||||||||||||
| 3 | 157 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 157 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 157 | our $VERSION = '0.002013'; | |||||||||||
| 7 | |||||||||||||
| 8 | 157 | ▶ | use Scalar::Util qw(weaken); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | PAGI::Server::TransportState - Outbound flow-control introspection for a connection | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | # Built by the server from an outbound-buffer source (not by the app): | ||||||||||||
| 17 | my $transport = PAGI::Server::TransportState->new( | ||||||||||||
| 18 | measure => sub { $conn->_get_write_buffer_size }, | ||||||||||||
| 19 | high => sub { $conn->{write_high_watermark} }, | ||||||||||||
| 20 | low => sub { $conn->{write_low_watermark} }, | ||||||||||||
| 21 | arm_drain => sub { my $fire = shift; $conn->_wait_for_drain->on_ready($fire) }, | ||||||||||||
| 22 | ); | ||||||||||||
| 23 | |||||||||||||
| 24 | # Read by the application via the scope: | ||||||||||||
| 25 | my $transport = $scope->{'pagi.transport'}; | ||||||||||||
| 26 | |||||||||||||
| 27 | # Bytes queued for the client but not yet written to the network | ||||||||||||
| 28 | my $pending = $transport->buffered_amount; | ||||||||||||
| 29 | |||||||||||||
| 30 | # The backpressure band (sends block at high, resume at low) | ||||||||||||
| 31 | my $ceiling = $transport->high_water_mark; | ||||||||||||
| 32 | my $floor = $transport->low_water_mark; | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||
| 35 | |||||||||||||
| 36 | PAGI::Server::TransportState is the object placed in the C<pagi.transport> scope | ||||||||||||
| 37 | key. It gives an application a synchronous, read-only view of B<outbound flow | ||||||||||||
| 38 | control> -- how much data the server has queued for the client but not yet | ||||||||||||
| 39 | written to the network -- so it can conflate, coalesce, shed load, or disconnect | ||||||||||||
| 40 | a slow client instead of only blocking until the buffer drains. It is the | ||||||||||||
| 41 | server-side analogue of the browser WebSocket API's C<bufferedAmount>. | ||||||||||||
| 42 | |||||||||||||
| 43 | The handle is source-agnostic: it measures the outbound buffer through coderefs | ||||||||||||
| 44 | supplied by the server, never by reaching into a connection itself. That lets | ||||||||||||
| 45 | the same hysteresis logic serve different transports -- under HTTP/1.1 the | ||||||||||||
| 46 | source reads the shared TCP write buffer, while under HTTP/2 it reads a | ||||||||||||
| 47 | per-stream send queue. All reads are live: each call invokes the source and | ||||||||||||
| 48 | reports its current state. See the "Transport Flow Control" section in | ||||||||||||
| 49 | L<PAGI::Spec::Www> for the full specification. | ||||||||||||
| 50 | |||||||||||||
| 51 | =head1 METHODS | ||||||||||||
| 52 | |||||||||||||
| 53 | =head2 new | ||||||||||||
| 54 | |||||||||||||
| 55 | my $transport = PAGI::Server::TransportState->new( | ||||||||||||
| 56 | measure => sub { ... }, # current buffered bytes | ||||||||||||
| 57 | high => $bytes, # high-water mark (value or coderef) | ||||||||||||
| 58 | low => $bytes, # low-water mark (value or coderef) | ||||||||||||
| 59 | arm_drain => sub { my $fire = shift; ... }, | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | |||||||||||||
| 62 | Creates a transport-state handle. B<This is built by the server, not the | ||||||||||||
| 63 | application> -- apps receive the finished handle via the C<pagi.transport> scope | ||||||||||||
| 64 | key. The arguments describe the outbound buffer source: | ||||||||||||
| 65 | |||||||||||||
| 66 | =over 4 | ||||||||||||
| 67 | |||||||||||||
| 68 | =item * C<measure> -- coderef returning the current buffered byte count. | ||||||||||||
| 69 | C<undef>/missing is treated as C<0>. | ||||||||||||
| 70 | |||||||||||||
| 71 | =item * C<high> / C<low> -- the backpressure band. Each may be a plain value or | ||||||||||||
| 72 | a coderef returning the current mark; C<undef> means unavailable. | ||||||||||||
| 73 | |||||||||||||
| 74 | =item * C<arm_drain> -- coderef invoked when the buffer crosses the high mark. It | ||||||||||||
| 75 | receives a single C<$fire> callback and must invoke it exactly once when the | ||||||||||||
| 76 | buffer next falls below the low mark, so C<on_drain> fires and the cycle re-arms. | ||||||||||||
| 77 | |||||||||||||
| 78 | =back | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | |||||||||||||
| 82 | 547 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 83 | 547 | my ($class, %args) = @_; | |||||||||||
| 84 | |||||||||||||
| 85 | my $self = bless { | ||||||||||||
| 86 | _measure => $args{measure}, # coderef -> current buffered bytes | ||||||||||||
| 87 | _high => $args{high}, # value or coderef -> high mark (undef ok) | ||||||||||||
| 88 | _low => $args{low}, # value or coderef -> low mark (undef ok) | ||||||||||||
| 89 | _arm_drain => $args{arm_drain}, # coderef: (fire) -> call fire once when below low | ||||||||||||
| 90 | |||||||||||||
| 91 | # Backpressure callbacks + hysteresis state. _above_high is true once | ||||||||||||
| 92 | # the buffer has crossed the high mark and not yet drained below the low | ||||||||||||
| 93 | # mark, so on_high_water is edge-triggered (fires once per cycle). | ||||||||||||
| 94 | 547 | _high_water_callbacks => [], | |||||||||||
| 95 | _drain_callbacks => [], | ||||||||||||
| 96 | _above_high => 0, | ||||||||||||
| 97 | }, $class; | ||||||||||||
| 98 | |||||||||||||
| 99 | 547 | return $self; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | =head2 buffered_amount | ||||||||||||
| 103 | |||||||||||||
| 104 | my $pending = $transport->buffered_amount; | ||||||||||||
| 105 | |||||||||||||
| 106 | Returns the number of bytes queued for the client but not yet written to the | ||||||||||||
| 107 | network, as an integer; C<0> when the send buffer is fully drained (or once the | ||||||||||||
| 108 | underlying connection has gone away). A synchronous, non-blocking, | ||||||||||||
| 109 | non-destructive read. | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | 493 | ▶ | sub buffered_amount { | ||||||||||
|
Subroutine
buffered_amount: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 114 | 493 | my $self = shift; | |||||||||||
| 115 | 493 | my $measure = $self->{_measure}; | |||||||||||
| 116 | 493 | ▶ | return 0 unless $measure; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 492 | ▶ | return $measure->() // 0; | ||||||||||
|
Condition100%2/2
&$measure() // 0
MC/DC100%1/1
&$measure() // 0
&$measure()
Truth table100%2/2
A
A= &$measure()
| |||||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | =head2 high_water_mark | ||||||||||||
| 121 | |||||||||||||
| 122 | my $ceiling = $transport->high_water_mark; | ||||||||||||
| 123 | |||||||||||||
| 124 | Returns the buffered-byte threshold at or above which the server applies | ||||||||||||
| 125 | backpressure (a C<$send> that would exceed it blocks until the buffer drains), | ||||||||||||
| 126 | or C<undef> if unavailable. Applications use it to threshold relative to the | ||||||||||||
| 127 | ceiling rather than hard-coding a byte count. | ||||||||||||
| 128 | |||||||||||||
| 129 | =cut | ||||||||||||
| 130 | |||||||||||||
| 131 | 479 | ▶ | sub high_water_mark { | ||||||||||
|
Subroutine
high_water_mark: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 132 | 479 | my $self = shift; | |||||||||||
| 133 | 479 | my $high = $self->{_high}; | |||||||||||
| 134 | 479 | ▶ | return ref $high eq 'CODE' ? $high->() : $high; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | =head2 low_water_mark | ||||||||||||
| 138 | |||||||||||||
| 139 | my $floor = $transport->low_water_mark; | ||||||||||||
| 140 | |||||||||||||
| 141 | Returns the buffered-byte threshold the buffer must fall back to before the | ||||||||||||
| 142 | server releases backpressure (the drain point), or C<undef> if unavailable. | ||||||||||||
| 143 | |||||||||||||
| 144 | =cut | ||||||||||||
| 145 | |||||||||||||
| 146 | 11 | ▶ | sub low_water_mark { | ||||||||||
|
Subroutine
low_water_mark: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 147 | 11 | my $self = shift; | |||||||||||
| 148 | 11 | my $low = $self->{_low}; | |||||||||||
| 149 | 11 | ▶ | return ref $low eq 'CODE' ? $low->() : $low; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | =head2 on_high_water | ||||||||||||
| 153 | |||||||||||||
| 154 | $transport->on_high_water(sub { $source->pause }); | ||||||||||||
| 155 | |||||||||||||
| 156 | Registers a callback invoked when the outbound buffer reaches or exceeds | ||||||||||||
| 157 | L</high_water_mark> (backpressure engaged). Edge-triggered: it fires once when | ||||||||||||
| 158 | the buffer crosses up, and not again until the buffer has drained below the low | ||||||||||||
| 159 | mark and crossed up again. If the buffer is already at or above the mark when | ||||||||||||
| 160 | the callback is registered, it is invoked immediately. Multiple callbacks may be | ||||||||||||
| 161 | registered; they are invoked in registration order with no arguments. Returns | ||||||||||||
| 162 | the handle for chaining. | ||||||||||||
| 163 | |||||||||||||
| 164 | =cut | ||||||||||||
| 165 | |||||||||||||
| 166 | 14 | ▶ | sub on_high_water { | ||||||||||
|
Subroutine
on_high_water: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 167 | 14 | my ($self, $cb) = @_; | |||||||||||
| 168 | 14 | push @{$self->{_high_water_callbacks}}, $cb; | |||||||||||
| 169 | |||||||||||||
| 170 | 14 | ▶ | if ($self->{_above_high}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | # Already in the high state: this late registrant fires now. | ||||||||||||
| 172 | 0 | $self->_fire([$cb]); | |||||||||||
| 173 | } | ||||||||||||
| 174 | else { | ||||||||||||
| 175 | # May already be above the mark but not yet detected (no send since). | ||||||||||||
| 176 | 14 | $self->_check_watermarks; | |||||||||||
| 177 | } | ||||||||||||
| 178 | |||||||||||||
| 179 | 14 | return $self; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | =head2 on_drain | ||||||||||||
| 183 | |||||||||||||
| 184 | $transport->on_drain(sub { $source->resume }); | ||||||||||||
| 185 | |||||||||||||
| 186 | Registers a callback invoked when the outbound buffer falls back below | ||||||||||||
| 187 | L</low_water_mark> after having reached the high mark (backpressure released). | ||||||||||||
| 188 | It is not invoked merely because the buffer is below the low mark when | ||||||||||||
| 189 | registered -- only on an actual high-then-low transition. Multiple callbacks may | ||||||||||||
| 190 | be registered; they are invoked in registration order with no arguments. Returns | ||||||||||||
| 191 | the handle for chaining. | ||||||||||||
| 192 | |||||||||||||
| 193 | =cut | ||||||||||||
| 194 | |||||||||||||
| 195 | 12 | ▶ | sub on_drain { | ||||||||||
|
Subroutine
on_drain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 196 | 12 | my ($self, $cb) = @_; | |||||||||||
| 197 | 12 | push @{$self->{_drain_callbacks}}, $cb; | |||||||||||
| 198 | 12 | return $self; | |||||||||||
| 199 | } | ||||||||||||
| 200 | |||||||||||||
| 201 | =head2 _check_watermarks | ||||||||||||
| 202 | |||||||||||||
| 203 | $transport->_check_watermarks; | ||||||||||||
| 204 | |||||||||||||
| 205 | B<Internal method> - Called by the server after an application send. Detects a | ||||||||||||
| 206 | high-water crossing and fires C<on_high_water>, then arms drain detection (via | ||||||||||||
| 207 | the source's C<arm_drain> coderef) so C<on_drain> fires once the buffer falls | ||||||||||||
| 208 | below the low mark. Edge-triggered and idempotent while above. | ||||||||||||
| 209 | |||||||||||||
| 210 | =cut | ||||||||||||
| 211 | |||||||||||||
| 212 | 492 | ▶ | sub _check_watermarks { | ||||||||||
|
Subroutine
_check_watermarks: called CC=5 SCAR=16.2
| |||||||||||||
| 213 | 492 | my ($self) = @_; | |||||||||||
| 214 | |||||||||||||
| 215 | 492 | ▶ | return if $self->{_above_high}; # already armed; waiting for drain | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 216 | |||||||||||||
| 217 | 468 | my $high = $self->high_water_mark; | |||||||||||
| 218 | 468 | ▶ | return unless defined $high; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 219 | 468 | ▶ | return unless $self->buffered_amount >= $high; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 220 | |||||||||||||
| 221 | 39 | $self->{_above_high} = 1; | |||||||||||
| 222 | 39 | $self->_fire($self->{_high_water_callbacks}); | |||||||||||
| 223 | |||||||||||||
| 224 | # Arm drain detection through the source: when the buffer falls below the | ||||||||||||
| 225 | # low mark, fire on_drain and re-arm the cycle. | ||||||||||||
| 226 | 39 | ▶ | my $arm = $self->{_arm_drain} or return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 227 | 39 | weaken(my $weak = $self); | |||||||||||
| 228 | 29 | ▶ | $arm->(sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 229 | 29 | ▶ | return unless $weak; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 230 | 29 | $weak->{_above_high} = 0; | |||||||||||
| 231 | 29 | $weak->_fire($weak->{_drain_callbacks}); | |||||||||||
| 232 | 39 | }); | |||||||||||
| 233 | |||||||||||||
| 234 | 39 | return; | |||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | # Invoke a list of callbacks in order, isolating exceptions. | ||||||||||||
| 238 | 68 | ▶ | sub _fire { | ||||||||||
|
Subroutine
_fire: called CC=3 SCAR=11.0
| |||||||||||||
| 239 | 68 | my ($self, $cbs) = @_; | |||||||||||
| 240 | 68 | for my $cb (@$cbs) { | |||||||||||
| 241 | 25 | ▶ | eval { $cb->(); 1 } or warn "transport callback error: $@"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 242 | } | ||||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | 157 | 1; | |||||||||||
| 246 | |||||||||||||
| 247 | __END__ | ||||||||||||