| 1 | # Session cache for RPC::Switch::Client::Tiny | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | package RPC::Switch::Client::Tiny::SessionCache; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Time::Local; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 22 | our $VERSION = 1.22; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 12 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 14 | 12 | my $self = bless { | |||||||||||||||||||||||||||
| 15 | %args, | ||||||||||||||||||||||||||||
| 16 | active => {}, # active async sessions | ||||||||||||||||||||||||||||
| 17 | lru => {}, # lru list for sessions | ||||||||||||||||||||||||||||
| 18 | expiring => [], # sorted session expire list | ||||||||||||||||||||||||||||
| 19 | per_user => {}, # active sessions per user (optional) | ||||||||||||||||||||||||||||
| 20 | }, $class; | ||||||||||||||||||||||||||||
| 21 | 12 | $self->{lru}{prev} = $self->{lru}{next} = $self->{lru}; | |||||||||||||||||||||||||||
| 22 | 12 | ▶ | $self->{session_expire} = 60 unless $self->{session_expire}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 12 | ▶ | $self->{session_idle} = 1 unless $self->{session_idle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 12 | ▶ | $self->{session_persist_user} = '' unless $self->{session_persist_user}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 12 | ▶ | $self->{max_user_session} = 0 unless $self->{max_user_session}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 12 | return $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 24 | ▶ | sub bin_search { | ||||||||||||||||||||||||||
|
Subroutine
bin_search: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 24 | my ($array, $cmp, $key) = @_; | |||||||||||||||||||||||||||
| 31 | 24 | my ($lo, $hi) = (0, $#{$array}); | |||||||||||||||||||||||||||
| 32 | 24 | my $found; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # If more than one element matches, return index to last one. | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | 24 | ▶ | while ($lo <= $hi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 39 | my $mid = int(($lo + $hi) / 2); | |||||||||||||||||||||||||||
| 38 | 39 | my $ret = $cmp->($key, $array->[$mid]); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 39 | ▶ | if ($ret == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 7 | $found = $mid; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 39 | ▶ | if ($ret < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 16 | $hi = $mid - 1; | |||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||
| 46 | 23 | $lo = $mid + 1; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 24 | ▶ | if (defined $found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 7 | return (1, $found); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 17 | return (0, $lo); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | ▶ | sub _expire_find_session_idx { | ||||||||||||||||||||||||||
|
Subroutine
_expire_find_session_idx: called CC=4 SCAR=19.7
| |||||||||||||||||||||||||||||
| 56 | 7 | my ($self, $session, $idx) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | do { | ||||||||||||||||||||||||||||
| 59 | 7 | ▶ | if ($self->{expiring}[$idx]->{id} eq $session->{id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 7 | return $idx; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | last if (--$idx < 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | } while ($self->{expiring}[$idx]->{expiretime} eq $session->{expiretime}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 24 | ▶ | sub expire_insert { | ||||||||||||||||||||||||||
|
Subroutine
expire_insert: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 24 | my ($self, $session) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Sort expire list in ascending order. | ||||||||||||||||||||||||||||
| 71 | # (mostly appends if all sessions have the same validity) | ||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||
| 73 | 24 | ▶ | my ($found, $idx) = bin_search($self->{expiring}, sub { $_[0]->{expiretime} - $_[1]->{expiretime} }, $session); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 24 | ▶ | if ($found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | # Update expire entry if session exists. | ||||||||||||||||||||||||||||
| 76 | # (there should be just one session per id) | ||||||||||||||||||||||||||||
| 77 | # | ||||||||||||||||||||||||||||
| 78 | 7 | my $sessionidx = $self->_expire_find_session_idx($session, $idx); | |||||||||||||||||||||||||||
| 79 | 7 | ▶ | if (defined $sessionidx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 7 | $self->{expiring}[$sessionidx] = $session; | |||||||||||||||||||||||||||
| 81 | 7 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | splice(@{$self->{expiring}}, $idx+1, 0, $session); | |||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 17 | splice(@{$self->{expiring}}, $idx, 0, $session); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub expire_remove { | ||||||||||||||||||||||||||
|
Subroutine
expire_remove: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($self, $session) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Remove can take a lot of processing if it is called | ||||||||||||||||||||||||||||
| 93 | # for long lists on every session drop. | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my ($found, $idx) = bin_search($self->{expiring}, sub { $_[0]->{expiretime} - $_[1]->{expiretime} }, $session); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | my $sessionidx = $self->_expire_find_session_idx($session, $idx); | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (defined $sessionidx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | splice(@{$self->{expiring}}, $sessionidx, 1); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | sub expire_regenerate { | ||||||||||||||||||||||||||
|
Subroutine
expire_regenerate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 1 | my ($self, $sessionlist) = @_; | |||||||||||||||||||||||||||
| 106 | 1 | $self->{expiring} = [sort { $a->{expiretime} - $b->{expiretime} } @$sessionlist]; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub list_empty { | ||||||||||||||||||||||||||
|
Subroutine
list_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 2 | my ($head) = @_; | |||||||||||||||||||||||||||
| 111 | 2 | return $head->{next} == $head; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 18 | ▶ | sub list_add { | ||||||||||||||||||||||||||
|
Subroutine
list_add: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 18 | my ($prev, $elem) = @_; | |||||||||||||||||||||||||||
| 116 | 18 | $prev->{next}{prev} = $elem; | |||||||||||||||||||||||||||
| 117 | 18 | $elem->{next} = $prev->{next}; | |||||||||||||||||||||||||||
| 118 | 18 | $elem->{prev} = $prev; | |||||||||||||||||||||||||||
| 119 | 18 | $prev->{next} = $elem; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 13 | ▶ | sub list_del { | ||||||||||||||||||||||||||
|
Subroutine
list_del: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 13 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 124 | 13 | $elem->{next}{prev} = $elem->{prev}; | |||||||||||||||||||||||||||
| 125 | 13 | $elem->{prev}{next} = $elem->{next}; | |||||||||||||||||||||||||||
| 126 | 13 | delete $elem->{prev}; | |||||||||||||||||||||||||||
| 127 | 13 | delete $elem->{next}; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 25 | ▶ | sub session_put { | ||||||||||||||||||||||||||
|
Subroutine
session_put: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 25 | my ($self, $child) = @_; | |||||||||||||||||||||||||||
| 132 | 25 | ▶ | my %runtime = (exists $child->{runtime}) ? (runtime => $child->{runtime}) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 25 | ▶ | return unless exists $child->{session}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 25 | ▶ | if (exists $self->{active}{$child->{session}{id}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 3 | return; # don't allow double sessions | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 22 | my $diff = $child->{session}{expiretime} - time(); | |||||||||||||||||||||||||||
| 140 | 22 | ▶ | if ($diff < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 2 | return; # session expired | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 20 | ▶ | if ($self->{max_user_session} && exists $child->{session}{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
MC/DC50%1/2
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
$self->{"max_user_session"}
exists $child->{"session"}{"user"}
Truth table66%2/3
A and B
A= $self->{"max_user_session"}B= exists $child->{"session"}{"user"}
| |||||||||||||||||||||||||||||
| 144 | 4 | my $user = $child->{session}{user}; | |||||||||||||||||||||||||||
| 145 | 4 | ▶ | if (exists $self->{per_user}{$user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 3 | my $cnt = scalar keys %{$self->{per_user}{$user}}; | |||||||||||||||||||||||||||
| 147 | 3 | ▶ | if ($cnt >= $self->{max_user_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 2 | return; # too many user sessions | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 2 | $self->{per_user}{$user}{$child->{session}{id}} = 1; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 18 | ▶ | $self->{trace_cb}->('PUT', {pid => $child->{pid}, id => $child->{id}, session => $child->{session}{id}, %runtime}) if $self->{trace_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 18 | $self->{active}{$child->{session}{id}} = $child; | |||||||||||||||||||||||||||
| 155 | 18 | list_add($self->{lru}{prev}, $child); | |||||||||||||||||||||||||||
| 156 | 18 | delete $child->{runtime}; | |||||||||||||||||||||||||||
| 157 | 18 | return 1; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 29 | ▶ | sub session_get { | ||||||||||||||||||||||||||
|
Subroutine
session_get: called CC=9 SCAR=26.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | 29 | my ($self, $session_id, $msg_id, $msg_vci) = @_; | |||||||||||||||||||||||||||
| 162 | 29 | ▶ | my %id = (defined $msg_id) ? (id => $msg_id) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 29 | ▶ | my %vci = (defined $msg_vci) ? (vci => $msg_vci) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 29 | ▶ | if (exists $self->{active}{$session_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 13 | my $child = delete $self->{active}{$session_id}; | |||||||||||||||||||||||||||
| 167 | 13 | list_del($child); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 13 | ▶ | if ($self->{max_user_session} && exists $child->{session}{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
MC/DC0%0/2
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
$self->{"max_user_session"}
exists $child->{"session"}{"user"}
Truth table33%1/3
A and B
A= $self->{"max_user_session"}B= exists $child->{"session"}{"user"}
| |||||||||||||||||||||||||||||
| 170 | 0 | my $user = $child->{session}{user}; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (exists $self->{per_user}{$user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | delete $self->{per_user}{$user}{$child->{session}{id}}; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (scalar keys %{$self->{per_user}{$user}} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | delete $self->{per_user}{$user}; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 13 | my $stoptime = sprintf "%.02f", time() - $child->{start}; | |||||||||||||||||||||||||||
| 180 | 13 | ▶ | $self->{trace_cb}->('GET', {pid => $child->{pid}, %id, %vci, session => $session_id, stoptime => $stoptime}) if $self->{trace_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 13 | return $child; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 16 | return; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub session_get_per_user { | ||||||||||||||||||||||||||
|
Subroutine
session_get_per_user: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 0 | my ($self, $user, $msg_id, $msg_vci) = @_; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ($self->{max_user_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | foreach my $session_id (keys %{$self->{per_user}{$user}}) { | |||||||||||||||||||||||||||
| 191 | 0 | return $self->session_get($session_id, $msg_id, $msg_vci); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 0 | return; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 5 | ▶ | sub session_get_per_user_idle { | ||||||||||||||||||||||||||
|
Subroutine
session_get_per_user_idle: called CC=7 SCAR=31.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 198 | 5 | my ($self, $child) = @_; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 5 | ▶ | return unless exists $child->{session}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 5 | ▶ | if ($self->{max_user_session} && exists $child->{session}{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
MC/DC0%0/2
$self->{"max_user_session"} and exists $child->{"session"}{"user"}
$self->{"max_user_session"}
exists $child->{"session"}{"user"}
Truth table33%1/3
A and B
A= $self->{"max_user_session"}B= exists $child->{"session"}{"user"}
| |||||||||||||||||||||||||||||
| 203 | 0 | my $user = $child->{session}{user}; | |||||||||||||||||||||||||||
| 204 | 0 | foreach my $session_id (keys %{$self->{per_user}{$user}}) { | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if (exists $self->{active}{$session_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | my $other_child = $self->{active}{$session_id}; | |||||||||||||||||||||||||||
| 207 | 0 | my $idle = time() - $other_child->{start}; | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | if ($idle >= $self->{session_idle}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | return $self->session_get($session_id); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 5 | return; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 3 | ▶ | sub parse_isotime { | ||||||||||||||||||||||||||
|
Subroutine
parse_isotime: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 218 | 3 | my ($isotime) = @_; | |||||||||||||||||||||||||||
| 219 | 3 | my ($yy,$mm,$dd,$h,$m,$s,$msec) = $isotime =~ | |||||||||||||||||||||||||||
| 220 | /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?Z$/; | ||||||||||||||||||||||||||||
| 221 | 3 | ▶ | return unless defined $s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 3 | my $time = timegm($s,$m,$h,$dd,$mm-1,$yy-1900); | |||||||||||||||||||||||||||
| 224 | 3 | return $time; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 19 | ▶ | sub session_new { | ||||||||||||||||||||||||||
|
Subroutine
session_new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 228 | 19 | my ($self, $set_session) = @_; | |||||||||||||||||||||||||||
| 229 | 19 | my $expiretime; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 19 | ▶ | if (exists $set_session->{expires}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 3 | $expiretime = parse_isotime($set_session->{expires}); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | # set default expire in seconds | ||||||||||||||||||||||||||||
| 235 | 19 | ▶ | $expiretime = time() + $self->{session_expire} unless $expiretime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 19 | my $session = {id => $set_session->{id}, expiretime => $expiretime}; | |||||||||||||||||||||||||||
| 237 | 19 | ▶ | $session->{user} = $set_session->{user} if exists $set_session->{user}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 19 | return $session; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 2 | ▶ | sub lru_list { | ||||||||||||||||||||||||||
|
Subroutine
lru_list: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 242 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 243 | 2 | my @list = (); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 2 | ▶ | for (my $elem = $self->{lru}{next}; $elem != $self->{lru}; $elem = $elem->{next}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 4 | push(@list, $elem); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | 2 | return \@list; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 2 | ▶ | sub lru_dequeue { | ||||||||||||||||||||||||||
|
Subroutine
lru_dequeue: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 252 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2 | ▶ | unless (list_empty($self->{lru})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 2 | my $child = $self->{lru}{next}; | |||||||||||||||||||||||||||
| 256 | 2 | return $self->session_get($child->{session}{id}); | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 0 | return; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 61 | ▶ | sub expired_dequeue { | ||||||||||||||||||||||||||
|
Subroutine
expired_dequeue: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 262 | 61 | my ($self) = @_; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # Use sorted expire list to expire sessions. | ||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||
| 266 | 61 | ▶ | while (scalar @{$self->{expiring}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 19 | my $session = $self->{expiring}[0]; | |||||||||||||||||||||||||||
| 268 | 19 | my $diff = $session->{expiretime} - time(); | |||||||||||||||||||||||||||
| 269 | 19 | ▶ | return if ($diff >= 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 2 | $session = shift @{$self->{expiring}}; | |||||||||||||||||||||||||||
| 272 | 2 | my $child = $self->session_get($session->{id}); | |||||||||||||||||||||||||||
| 273 | 2 | ▶ | return $child if $child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | 44 | return; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 22 | 1; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | __END__ | ||||||||||||||||||||||||||||