blib/lib/Langertha/Skeid/Registry.pm

filter: Statementstmt 100% 80 / 80 Branchbran 83.3% 35 / 42 Conditioncond 66.6% 22 / 33 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 85.1% 166 / 195 CC 35 SCAR 27.5
CC
35
Cov
88%
reading_from_snapshot
27.2
verify
5.2
_num
4.1
CRAP 15.7
« blib/lib/Langertha/Skeid/Proxy.pm ↑ Summary blib/lib/Langertha/Skeid/Protocol/Ollama/Stream.pm »
1 package Langertha::Skeid::Registry;
2 47 our $VERSION = '0.003';
3 # ABSTRACT: Signed Skeid-to-Skeid capacity snapshots: encoding, signing, verification, mapping
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Langertha::Skeid::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 44 ▶sub SIGNATURE_HEADER { 'X-Skeid-Registry-Signature' }
Subroutine SIGNATURE_HEADER: called CC=1 SCAR=0.0
Pod documented
14  
15  
16 22 ▶sub SCHEMA_VERSION { 1 }
Subroutine SCHEMA_VERSION: called CC=1 SCAR=0.0
Pod documented
17  
18  
19 37 ▶sub MIN_SECRET_BYTES { 32 }
Subroutine MIN_SECRET_BYTES: called CC=1 SCAR=0.0
Pod documented
20  
21 47 my $JSON = JSON::MaybeXS->new(canonical => 1, utf8 => 1);
22  
23  
24 31 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
25 31 my ($class, $snapshot) = @_;
26 31 return $JSON->encode($snapshot);
27 }
28  
29  
30 59 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
31 59 my ($class, $body, $secret) = @_;
32 59 return 'sha256=' . hmac_sha256_hex($body, $secret);
33 }
34  
35  
36 19 ▶sub signed_snapshot {
Subroutine signed_snapshot: called CC=3 SCAR=11.3
Pod documented
37 19 my ($class, $skeid) = @_;
38 19 ▶ my $secret = $skeid->registry_secret // '';
Condition50%1/2
$skeid->registry_secret // ""
l!l
190
MC/DC0%0/1
$skeid->registry_secret // ""
$skeid->registry_secret
Truth table50%1/2
A
A= $skeid->registry_secret
Aresult
11
00
39 19 ▶ die "registry secret is not set; a snapshot is never published unsigned\n" unless length $secret;
Branch: 1 branch
BranchTrueFalseTotal
unless length $secret 0 19 19
40 19 my $body = $class->encode($skeid->registry_snapshot);
41 18 return ($body, $class->sign($body, $secret));
42 }
43  
44  
45 29 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.4
Pod documented
46 29 my ($class, $body, $header, $secret) = @_;
47 29 ▶ return 0 unless defined($body) && defined($header) && defined($secret) && length($secret);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and defined $header and defined $secret and length $secret 1 28 29
Condition66%6/9
defined $body and defined $header and defined $secret and length $secret
!ll&&!rl&&r
1028
defined $body and defined $header and defined $secret
!ll&&!rl&&r
1028
defined $body and defined $header
!ll&&!rl&&r
0128
MC/DC25%1/4
defined $body and defined $header and defined $secret and length $secret
defined $body defined $header defined $secret length $secret
Truth table40%2/5
A and B and C and D
A= defined $bodyB= defined $headerC= defined $secretD= length $secret
ABCDresult
0XXX0
10XX0
110X0
11100
11111
48 28 my $want = $class->sign($body, $secret);
49 28 $header =~ s/\A\s+|\s+\z//g;
50 28 return Langertha::Skeid::Secret->equal($header, $want);
51 }
52  
53  
54 27 ▶sub reading_from_snapshot {
Subroutine reading_from_snapshot: called CC=26 SCAR=33.0
Pod documented
55 27 my ($class, $snapshot, %args) = @_;
56 27 ▶ my @want = map { lc } grep { defined && length } @{ $args{tags} || [] };
Condition60%3/5
defined $_ && length $_
!ll&&!rl&&r
001
$args{'tags'} || []
l!l
207
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC100%1/1
$args{'tags'} || []
$args{'tags'}
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table100%2/2
A
A= $args{'tags'}
Aresult
11
00
57 27 my $now = Time::HiRes::time();
58  
59 27 my ($limit, $free, $selected) = (0, 0, 0);
60 27 ▶ for my $node (@{ $snapshot->{nodes} || [] }) {
Condition50%1/2
$snapshot->{'nodes'} || []
l!l
270
MC/DC0%0/1
$snapshot->{'nodes'} || []
$snapshot->{'nodes'}
Truth table50%1/2
A
A= $snapshot->{'nodes'}
Aresult
11
00
61 44 ▶ next unless ref($node) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 44 44
62 44 ▶ next unless $node->{healthy};
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"healthy"} 2 42 44
63 42 ▶ if (@want) {
Branch: 1 branch
BranchTrueFalseTotal
if (@want) 2 40 42
64 2 ▶ my %have = map { lc($_) => 1 } grep { defined } @{ ref($node->{tags}) eq 'ARRAY' ? $node->{tags} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'tags'} eq 'ARRAY' ? : 2 0 2
65 2 ▶ next if grep { !$have{$_} } @want;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $have{$_};} @want 1 1 2
66 }
67 41 $selected++;
68  
69 41 my $max = _num($node->{max_conns});
70 41 my $inflight = _num($node->{inflight});
71 41 ▶ my $cap = ref($node->{capacity}) eq 'HASH' ? $node->{capacity} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'capacity'} eq 'HASH' ? : 2 39 41
72 41 ▶ my $cap_limit = $cap ? _num($cap->{limit}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cap ? : 2 39 41
73 41 ▶ my $backoff = ($cap && $cap->{retry_after} && _num($cap->{retry_after}) > $now) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cap && $cap->{'retry_after'} && _num($cap->{'retry_after'}) > $now ? : 1 40 41
Condition83%5/6
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
!ll&&!rl&&r
4001
$cap and $cap->{"retry_after"}
!ll&&!rl&&r
3911
MC/DC66%2/3
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
$cap $cap->{"retry_after"} _num($cap->{"retry_after"}) > $now
Truth table75%3/4
A and B and C
A= $capB= $cap->{"retry_after"}C= _num($cap->{"retry_after"}) > $now
ABCresult
0XX0
10X0
1100
1111
74  
75 41 ▶ my $node_limit = $max > 0 ? $max : $cap_limit;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 40 1 41
76 41 ▶ if ($node_limit <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_limit <= 0) 1 40 41
77 # Nothing bounds this node, so nothing bounds the downstream -- unless it is backing off,
78 # in which case it simply has no slots to offer.
79 1 ▶ next if $backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $backoff 0 1 1
80 1 return { used => undef, limit => undef };
81 }
82  
83 40 ▶ my $node_free = $max > 0 ? $max - $inflight : $node_limit;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 40 0 40
84 40 ▶ if ($cap_limit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cap_limit > 0) 1 39 40
85 1 my $cap_free = $cap_limit - _num($cap->{used});
86 1 ▶ $node_free = $cap_free if $cap_free < $node_free;
Branch: 1 branch
BranchTrueFalseTotal
if $cap_free < $node_free 1 0 1
87 }
88 40 ▶ $node_free = 0 if $backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $backoff 1 39 40
89 40 ▶ $node_free = 0 if $node_free < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $node_free < 0 1 39 40
90 40 ▶ $node_free = $node_limit if $node_free > $node_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $node_free > $node_limit 0 40 40
91  
92 40 $limit += $node_limit;
93 40 $free += $node_free;
94 }
95  
96 26 ▶ return { used => 1, limit => 1 } if !$selected || $limit <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $selected or $limit <= 0 1 25 26
Condition66%2/3
not $selected or $limit <= 0
l!l&&r!l&&!r
1025
MC/DC50%1/2
not $selected or $limit <= 0
not $selected $limit <= 0
Truth table66%2/3
not(A) or B
A= not $selectedB= $limit <= 0
ABresult
1X1
011
000
97 25 return { used => $limit - $free, limit => $limit };
98 }
99  
100 86 ▶sub _num {
Subroutine _num: called CC=4 SCAR=14.1
101 86 my ($value) = @_;
102 86 ▶ return 0 unless defined $value && !ref $value && $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/ 1 85 86
Condition66%4/6
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
!ll&&!rl&&r
1085
defined $value and not ref $value
!ll&&!rl&&r
1085
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
defined $value not ref $value $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
ABCresult
0XX0
10X0
1100
1111
103 85 return 0 + $value;
104 }
105  
106  
107 47 1;
108  
109 __END__
110  
111 =pod
112  
113 =encoding UTF-8
114  
115 =head1 NAME
116  
117 Langertha::Skeid::Registry - Signed Skeid-to-Skeid capacity snapshots: encoding, signing, verification, mapping
118  
119 =head1 VERSION
120  
121 version 0.003
122  
123 =head1 SYNOPSIS
124  
125 # downstream: what GET /skeid/registry/snapshot sends
126 my ($body, $signature) = Langertha::Skeid::Registry->signed_snapshot($skeid);
127  
128 # fronting tier: what CapacityProbe::Registry does with it
129 Langertha::Skeid::Registry->verify($body, $signature, $secret) or die 'bad signature';
130 my $reading = Langertha::Skeid::Registry->reading_from_snapshot($snapshot, tags => ['local']);
131  
132 =head1 DESCRIPTION
133  
134 The two halves of the registry (ADR 0017) share one module, so the bytes one side signs are
135 the bytes the other side checks and the mapping to a capacity reading is written down once.
136  
137 The signature is C<sha256=> plus the hex HMAC-SHA256 of the exact response body, in the
138 L</SIGNATURE_HEADER>. C<generated_at> and C<ttl> sit inside the signed body, so a snapshot's age
139 cannot be changed without breaking it.
140  
141 =head2 SIGNATURE_HEADER
142  
143 C<X-Skeid-Registry-Signature>.
144  
145 =head2 SCHEMA_VERSION
146  
147 The snapshot schema this code writes and accepts: 1.
148  
149 =head2 MIN_SECRET_BYTES
150  
151 The shortest signing secret either side accepts: 32 bytes, the HMAC-SHA256 output size. A
152 downstream with a shorter one fails its config load; a fronting probe with one reports
153 C<missing_secret>.
154  
155 =head2 encode
156  
157 my $body = Langertha::Skeid::Registry->encode($snapshot);
158  
159 Canonical JSON bytes (sorted keys).
160  
161 =head2 sign
162  
163 my $signature = Langertha::Skeid::Registry->sign($body, $secret); # 'sha256=...'
164  
165 C<sha256=> plus the hex HMAC-SHA256 of C<$body> under C<$secret>.
166  
167 =head2 signed_snapshot
168  
169 my ($body, $signature) = Langertha::Skeid::Registry->signed_snapshot($skeid);
170  
171 L<Langertha::Skeid/registry_snapshot>, encoded and signed with the Skeid's C<registry_secret>.
172 Dies without a secret: a snapshot is never published unsigned.
173  
174 =head2 verify
175  
176 my $ok = Langertha::Skeid::Registry->verify($body, $signature_header, $secret);
177  
178 True when the header is the signature of these exact bytes under this secret. The comparison
179 takes the same time wherever the first difference is, so a caller cannot find the signature
180 one character at a time.
181  
182 =head2 reading_from_snapshot
183  
184 my $reading = Langertha::Skeid::Registry->reading_from_snapshot($snapshot, tags => \@tags);
185 # { used => 5, limit => 16 } or { used => undef, limit => undef } (no ceiling)
186  
187 Turns a verified snapshot into the one capacity reading admission consumes for the node that
188 stands for this downstream. Over the downstream nodes that are healthy and carry every tag in
189 C<tags>:
190  
191 =over 4
192  
193 =item * C<free> per node is C<max_conns - inflight>, at most C<limit - used> of the node's own
194 capacity reading when it has one with a limit, and 0 while its backoff is pending. Never below
195 0, never above the node's limit.
196  
197 =item * C<limit> is the sum of the nodes' limits (C<max_conns>, or the reading's limit for a
198 node without one), C<used> is C<limit> minus the summed C<free>.
199  
200 =item * A node with no ceiling at all (C<max_conns> 0 and no limited reading) makes the whole
201 downstream unbounded: the reading has no limit and does not narrow admission.
202  
203 =item * No selected healthy node: reported as full (C<used> 1 of C<limit> 1). That downstream
204 would answer C<503>, so routing should go elsewhere.
205  
206 =back
207  
208 C<used> is the downstream's own number. It is never added to what this process counts in
209 C<inflight>: the requests this process sent are already in it (ADR 0017).
210  
211 =head1 SEE ALSO
212  
213 L<Langertha::Skeid/registry_snapshot> and L<Langertha::Skeid/registry_enabled> (publishing),
214 L<Langertha::Skeid::CapacityProbe::Registry> (reading), L<Langertha::Skeid::Secret>
215  
216 =head1 SUPPORT
217  
218 =head2 Issues
219  
220 Please report bugs and feature requests on GitHub at
221 L<https://github.com/Getty/langertha-skeid/issues>.
222  
223 =head2 IRC
224  
225 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
226  
227 =head1 CONTRIBUTING
228  
229 Contributions are welcome! Please fork the repository and submit a pull request.
230  
231 =head1 AUTHOR
232  
233 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
234  
235 =head1 COPYRIGHT AND LICENSE
236  
237 This software is copyright (c) 2026 by Torsten Raudssus.
238  
239 This is free software; you can redistribute it and/or modify it under
240 the same terms as the Perl 5 programming language system itself.
241  
242 =cut
« blib/lib/Langertha/Skeid/Proxy.pm ↑ Summary blib/lib/Langertha/Skeid/Protocol/Ollama/Stream.pm »