File Coverage

blib/lib/Perlbal/Plugin/Queues.pm
Criterion Covered Total %
statement 9 33 27.2
branch 0 6 0.0
condition 0 3 0.0
subroutine 3 8 37.5
pod 0 4 0.0
total 12 54 22.2


line stmt bran cond sub pod time code
1             ###########################################################################
2             # simple queue length header inclusion plugin
3             ###########################################################################
4              
5             package Perlbal::Plugin::Queues;
6              
7 1     1   1705 use strict;
  1         4  
  1         40  
8 1     1   5 use warnings;
  1         3  
  1         37  
9 1     1   6 no warnings qw(deprecated);
  1         3  
  1         438  
10              
11             # called when we're being added to a service
12             sub register {
13 0     0 0   my ($class, $svc) = @_;
14              
15             # more complicated statistics
16             $svc->register_hook('Queues', 'backend_client_assigned', sub {
17 0     0     my Perlbal::BackendHTTP $obj = shift;
18 0           my Perlbal::HTTPHeaders $hds = $obj->{req_headers};
19 0           my Perlbal::Service $svc = $obj->{service};
20 0 0 0       return 0 unless defined $hds && defined $svc;
21              
22             # determine age of oldest (first in line)
23 0           my $now = time;
24 0           my Perlbal::ClientProxy $cp = $svc->{waiting_clients}->[0];
25 0 0         my $age = defined $cp ? ($now - $cp->{last_request_time}) : 0;
26              
27             # now do the age of the high priority queue
28 0           $cp = $svc->{waiting_clients_highpri}->[0];
29 0 0         my $hpage = defined $cp ? ($now - $cp->{last_request_time}) : 0;
30              
31             # setup the queue length headers
32 0           $hds->header('X-Queue-Count', scalar(@{$svc->{waiting_clients}}));
  0            
33 0           $hds->header('X-Queue-Age', $age);
34 0           $hds->header('X-HP-Queue-Count', scalar(@{$svc->{waiting_clients_highpri}}));
  0            
35 0           $hds->header('X-HP-Queue-Age', $hpage);
36 0           return 0;
37 0           });
38              
39 0           return 1;
40             }
41              
42             # called when we're no longer active on a service
43             sub unregister {
44 0     0 0   my ($class, $svc) = @_;
45              
46             # clean up time
47 0           $svc->unregister_hooks('Queues');
48 0           return 1;
49             }
50              
51             # we don't do anything in here
52 0     0 0   sub load { return 1; }
53 0     0 0   sub unload { return 1; }
54              
55             1;