| 1 | package Langertha::Skeid::KeyBroker; | ||||||||||||||||||||||||||||
| 2 | 6 | our $VERSION = '0.004'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Pluggable API key resolution for Skeid nodes | ||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | has cache_ttl => ( | ||||||||||||||||||||||||||||
| 11 | is => 'rw', | ||||||||||||||||||||||||||||
| 12 | 95 | ▶ | default => sub { 300 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has negative_cache_ttl => ( | ||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | 95 | ▶ | default => sub { 5 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 6 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # ref => { key => $key_or_undef, error => $msg_or_undef, expires => $epoch } | ||||||||||||||||||||||||||||
| 22 | has _cache => ( | ||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | 96 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 6 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # ref => [ @callbacks ] for a resolution currently in flight | ||||||||||||||||||||||||||||
| 28 | has _pending => ( | ||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | 96 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 6 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub resolve_key { | ||||||||||||||||||||||||||
|
Subroutine
resolve_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 1 | my ($self, $ref) = @_; | |||||||||||||||||||||||||||
| 36 | 1 | croak ref($self) . " must implement resolve_key()"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 94 | ▶ | sub key_async { | ||||||||||||||||||||||||||
|
Subroutine
key_async: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 94 | my ($self, $ref, $cb) = @_; | |||||||||||||||||||||||||||
| 42 | 94 | ▶ | $cb ||= sub { }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub {
}
MC/DC0%0/2
$cb ||= sub {
}
$cb
sub {
}
Truth table33%1/3
A ||= B
A= $cbB= sub {
}
| |||||||||||||||||||||||||||||
| 43 | 94 | ▶ | return $cb->(undef, 'no key reference') unless defined($ref) && length($ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ref and length $ref
MC/DC0%0/2
defined $ref and length $ref
defined $ref
length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 94 | ▶ | if (my $hit = $self->cached_key($ref)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 10 | $cb->($hit->{key}, $hit->{error}); | |||||||||||||||||||||||||||
| 47 | 10 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # One round-trip per reference, however many requests are waiting on it. Without this, a cold | ||||||||||||||||||||||||||||
| 51 | # cache at concurrency 64 is 64 identical vault calls -- and the moment a key expires, every | ||||||||||||||||||||||||||||
| 52 | # in-flight request misses at once. | ||||||||||||||||||||||||||||
| 53 | 84 | my $pending = $self->_pending; | |||||||||||||||||||||||||||
| 54 | 84 | ▶ | if ($pending->{$ref}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 4 | push @{$pending->{$ref}}, $cb; | |||||||||||||||||||||||||||
| 56 | 4 | return; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 80 | $pending->{$ref} = [$cb]; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 79 | ▶ | $self->resolve_key_async($ref, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 61 | 79 | my ($key, $error) = @_; | |||||||||||||||||||||||||||
| 62 | 79 | ▶ | $key = undef unless defined($key) && length($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and length $key
MC/DC50%1/2
defined $key and length $key
defined $key
length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
| |||||||||||||||||||||||||||||
| 63 | 79 | $self->_cache_result($ref, $key, $error); | |||||||||||||||||||||||||||
| 64 | 79 | ▶ | my $waiting = delete($pending->{$ref}) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $pending->{$ref} || []
MC/DC0%0/1
delete $pending->{$ref} || []
delete $pending->{$ref}
Truth table50%1/2
A
A= delete $pending->{$ref}
| |||||||||||||||||||||||||||||
| 65 | 79 | $_->($key, $error) for @$waiting; | |||||||||||||||||||||||||||
| 66 | 80 | }); | |||||||||||||||||||||||||||
| 67 | 80 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 71 | ▶ | sub resolve_key_async { | ||||||||||||||||||||||||||
|
Subroutine
resolve_key_async: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 71 | my ($self, $ref, $cb) = @_; | |||||||||||||||||||||||||||
| 73 | 71 | my $key = eval { $self->resolve_key($ref) }; | |||||||||||||||||||||||||||
| 74 | 71 | ▶ | my $error = $@ ? do { my $e = "$@"; $e =~ s/\s+\z//; $e } : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 71 | ▶ | $key = undef unless defined($key) && length($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and length $key
MC/DC50%1/2
defined $key and length $key
defined $key
length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
| |||||||||||||||||||||||||||||
| 76 | 71 | ▶ | $error //= 'no key resolved' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$error //= "no key resolved"
MC/DC0%0/1
$error //= "no key resolved"
$error
Truth table50%1/2
A
A= $error
| |||||||||||||||||||||||||||||
| 77 | 71 | $cb->($key, $error); | |||||||||||||||||||||||||||
| 78 | 71 | return; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 96 | ▶ | sub cached_key { | ||||||||||||||||||||||||||
|
Subroutine
cached_key: called CC=5 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 96 | my ($self, $ref) = @_; | |||||||||||||||||||||||||||
| 84 | 96 | ▶ | return unless defined($ref) && length($ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ref and length $ref
MC/DC0%0/2
defined $ref and length $ref
defined $ref
length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
| |||||||||||||||||||||||||||||
| 85 | 96 | ▶ | my $hit = $self->_cache->{$ref} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 11 | ▶ | if ($hit->{expires} <= time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | delete $self->_cache->{$ref}; | |||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 11 | return $hit; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub forget_key { | ||||||||||||||||||||||||||
|
Subroutine
forget_key: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 1 | my ($self, $ref) = @_; | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | if (defined($ref) && length($ref)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ref and length $ref
MC/DC0%0/2
defined $ref and length $ref
defined $ref
length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
| |||||||||||||||||||||||||||||
| 97 | 1 | delete $self->_cache->{$ref}; | |||||||||||||||||||||||||||
| 98 | 1 | return 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | %{$self->_cache} = (); | |||||||||||||||||||||||||||
| 101 | 0 | return 1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 79 | ▶ | sub _cache_result { | ||||||||||||||||||||||||||
|
Subroutine
_cache_result: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 105 | 79 | my ($self, $ref, $key, $error) = @_; | |||||||||||||||||||||||||||
| 106 | 79 | ▶ | my $ttl = defined($key) ? $self->cache_ttl : $self->negative_cache_ttl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 79 | ▶ | return unless $ttl > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 78 | $self->_cache->{$ref} = { key => $key, error => $error, expires => time + $ttl }; | |||||||||||||||||||||||||||
| 109 | 78 | return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub needs_refresh { 0 } | ||||||||||||||||||||||||||
|
Subroutine
needs_refresh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub refresh { } | ||||||||||||||||||||||||||
|
Subroutine
refresh: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||