| 1 | use v5.36; | ||
| 2 | package Remote::Perl::FlowControl; | ||
| 3 | our $VERSION = '0.005'; | ||
| 4 | |||
| 5 | # Credit-based flow control. Each stream has a budget of bytes the sender | ||
| 6 | # is allowed to transmit before the receiver must grant more. | ||
| 7 | # | ||
| 8 | # "send credits" -- bytes we are allowed to send on a given stream | ||
| 9 | # (incremented when remote sends CREDIT messages to us) | ||
| 10 | # "recv pending" -- bytes we have received but not yet acknowledged back; | ||
| 11 | # once this crosses half the window we issue a CREDIT grant | ||
| 12 | |||
| 13 | sub new($class, %args) { | ||
| 14 | return bless { | ||
| 15 | window_size => $args{window_size} // 65_536, | ||
| 16 | send_credits => {}, # stream_id => bytes remaining | ||
| 17 | recv_pending => {}, # stream_id => bytes consumed since last grant | ||
| 18 | }, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | # Called at handshake time: remote has granted us $credits bytes on $stream. | ||
| 22 | sub init_stream($self, $stream, $credits = undef) { | ||
| 23 | $credits //= $self->{window_size}; | ||
| 24 | $self->{send_credits}{$stream} = $credits; | ||
| 25 | $self->{recv_pending}{$stream} = 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | # How many bytes we may currently send on $stream. | ||
| 29 | sub send_credit($self, $stream) { | ||
| 30 | return $self->{send_credits}{$stream} // 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | # Deduct $n bytes from our send budget. Dies if budget is insufficient. | ||
| 34 | sub consume_send_credit($self, $stream, $n) { | ||
| 35 | my $have = $self->{send_credits}{$stream} // 0; | ||
| 36 | die "Flow control: send credit exhausted on stream $stream " | ||
| 37 | . "(have $have, want $n)\n" | ||
| 38 | if $n > $have; | ||
| 39 | $self->{send_credits}{$stream} = $have - $n; | ||
| 40 | } | ||
| 41 | |||
| 42 | # Remote sent us a CREDIT message granting $n more bytes on $stream. | ||
| 43 | sub add_send_credit($self, $stream, $n) { | ||
| 44 | $self->{send_credits}{$stream} = ($self->{send_credits}{$stream} // 0) + $n; | ||
| 45 | } | ||
| 46 | |||
| 47 | # We just received $n bytes of data on $stream. | ||
| 48 | # Returns the number of bytes to grant back (0 if not yet time). | ||
| 49 | # Caller must send a CREDIT message with this value when non-zero. | ||
| 50 | sub receive_data($self, $stream, $n) { | ||
| 51 | $self->{recv_pending}{$stream} = ($self->{recv_pending}{$stream} // 0) + $n; | ||
| 52 | if ($self->{recv_pending}{$stream} >= $self->{window_size} / 2) { | ||
| 53 | my $grant = $self->{recv_pending}{$stream}; | ||
| 54 | $self->{recv_pending}{$stream} = 0; | ||
| 55 | return $grant; | ||
| 56 | } | ||
| 57 | return 0; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Force-grant all outstanding pending bytes back (e.g. at stream close). | ||
| 61 | sub flush_recv_pending($self, $stream) { | ||
| 62 | my $grant = $self->{recv_pending}{$stream} // 0; | ||
| 63 | $self->{recv_pending}{$stream} = 0; | ||
| 64 | return $grant; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ |