blib/lib/AnyEvent/Memcached/Peer.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 161 SCAR 58.3
CC
18
Cov
0%
conntrack
110.0
connect
30.0
command
6.0
CRAP 342.0
1 package #hide
2 AnyEvent::Memcached::Peer;
3  
4 use common::sense 2;m{
5 use strict;
6 use warnings;
7 }x;
8 use base 'AnyEvent::Connection';
9 use Carp;
10 use AnyEvent::Connection::Util;
11 use Scalar::Util qw(weaken);
12 #use Devel::Leak::Cb;
13 sub DEBUG () { 0 }
14  
15 use AnyEvent::Memcached::Conn;
16  
17 sub new {
18 my $self = shift->SUPER::new(
19 rawcon => 'AnyEvent::Memcached::Conn',
20 reconnect => 1,
21 @_,
22 );
23 $self->{waitingcb} = {};
24 $self;
25 }
26  
27 sub connect {
28 my $self = shift;
29 $self->{connecting} and return;
30 $self->{grd}{con} = $self->reg_cb( connected => sub { $self->{failed} = 0; } );
31 $self->{grd}{cfl} = $self->reg_cb( connfail => sub { $self->{failed} = 1; } );
32 $self->{grd}{dis} = $self->reg_cb( disconnect => sub {
33 shift;shift;
34 %$self or return;
35 warn "Peer $self->{host}:$self->{port} disconnected".(@_ ? ": @_" : '')."\n" if $self->{debug};
36 my $e = @_ ? "@_" : "disconnected";
37 for ( keys %{$self->{waitingcb}} ) {
38 if ($self->{waitingcb}{$_}) {
39 #warn "Cleanup: ",::sub_fullname( $self->{waitingcb}{$_} );
40 $self->{waitingcb}{$_}(undef,$e);
41 }
42 delete $self->{waitingcb}{$_};
43 }
44 } );
45 $self->SUPER::connect(@_);
46 return;
47 }
48  
49 sub conntrack {
50 my $self = shift;
51 my ($method,$args,$cb) = @_;
52 if($self->{connecting} and $self->{failed}) {
53 warn "Is connecting, have fails => not connected" if DEBUG;
54 $cb and $cb->(undef, "Not connected");
55 return;
56 }
57 elsif (!$self->{connected}) {
58 my @args = @$args; # copy to avoid rewriting
59 warn time()." Not connected, do connect for ".\@args.", ".dumper($args[0]) if DEBUG;
60 my ($c,$t);
61 weaken( $self->{waitingcb}{int $cb} = $cb ) if $cb;
62 weaken( $self );
63 # This rely on correct event invocation order of Object::Event.
64 # If this could change, I'll add own queue
65 $c = $self->reg_cb(
66 connected => sub {
67 shift->unreg_me;
68 #$c or return;
69 warn "connected cb for ".\@args.", ".dumper($args[0]) if DEBUG;
70 undef $c;undef $t;
71 $self or return;
72 delete $self->{waitingcb}{int $cb} if $cb;
73 return $self->{con}->$method(@args);
74 },
75 );
76 $t = AnyEvent->timer(
77 after => $self->{timeout},# + 0.05, # Since there are timers inside connect, we need to delay a bit longer
78 cb => sub {
79 #$t or return;
80 warn time()." timeout $self->{timeout} cb for $args->[0]" if DEBUG;
81 undef $c;undef $t;
82 $self or return;
83 if ($cb){
84 $self->{waitingcb}{int $cb};
85 $cb->(undef, "Connect timeout");
86 }
87 },
88 );
89 $self->connect();
90 }
91 else {
92 Carp::cluck "How do I get here?";
93 return $self->{con}->$method(@$args);
94 }
95 }
96  
97 sub command {
98 my $self = shift;
99 if ($self->{connected}) {
100 return $self->{con}->command( @_ );
101 }
102 else {
103 my ($cmd,%args) = @_;
104 $self->conntrack( command => \@_, $args{cb} );
105 }
106 }
107  
108 sub request {
109 my $self = shift;
110 if ($self->{connected}) {
111 return $self->{con}->say(@_);
112 }
113 else {
114 # no cb
115 $self->conntrack( say => \@_ );
116 }
117 }
118  
119 sub reader {
120 my $self = shift;
121 if ($self->{connected}) {
122 return $self->{con}->reader(@_);
123 }
124 else {
125 my %args = @_;
126 $self->conntrack( reader => \@_, $args{cb} );
127 }
128
129 }
130  
131 sub want_command {
132 my $self = shift;
133 warn "wanting command";
134 if ($self->{connected}) {
135 return $self->{con}->want_command(@_);
136 }
137 else {
138 my %args = @_;
139 $self->conntrack( want_command => \@_ );
140 }
141 }
142  
143 1;