From 7cccb89f2727305db1a0c2bbe1588606c8806a82 Mon Sep 17 00:00:00 2001 From: Jason Stubbs Date: Mon, 6 Feb 2012 11:16:12 +1100 Subject: [PATCH 1/3] Only use a closure in the uncommon case --- lib/Perlbal/ClientHTTPBase.pm | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/Perlbal/ClientHTTPBase.pm b/lib/Perlbal/ClientHTTPBase.pm index d5b38b4..125cfff 100644 --- a/lib/Perlbal/ClientHTTPBase.pm +++ b/lib/Perlbal/ClientHTTPBase.pm @@ -310,21 +310,22 @@ sub handle_request { # re-enable reads. $self->watch_read(0); - my $select = sub { - # now that we have headers, it's time to tell the selector - # plugin that it's time for it to select which real service to - # use - my $selector = $self->{'service'}->selector(); - return $self->_simple_response(500, "No service selector configured.") - unless ref $selector eq "CODE"; - $selector->($self); - }; - + # now that we have headers, it's time to tell the selector + # plugin that it's time for it to select which real service to + # use my $svc = $self->{'service'}; - if ($svc->{latency}) { - Danga::Socket->AddTimer($svc->{latency} / 1000, $select); + my $selector = $svc->selector(); + + return $self->_simple_response(500, "No service selector configured.") + unless ref $selector eq "CODE"; + + unless ($svc->{latency}) { + $selector->($self); } else { - $select->(); + my $select = sub { + $selector->($self); + }; + Danga::Socket->AddTimer($svc->{latency} / 1000, $select); } } From 2684ea545f90be62acd903eb409b70c2f0559991 Mon Sep 17 00:00:00 2001 From: Jason Stubbs Date: Mon, 6 Feb 2012 11:03:48 +1100 Subject: [PATCH 2/3] Removed needless closure --- lib/Perlbal/ClientProxy.pm | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/Perlbal/ClientProxy.pm b/lib/Perlbal/ClientProxy.pm index 7270f8c..ac0cc05 100644 --- a/lib/Perlbal/ClientProxy.pm +++ b/lib/Perlbal/ClientProxy.pm @@ -264,18 +264,12 @@ sub backend_response_received { # a certain size and got back something different my $code = $be->{res_headers}->response_code + 0; - my $bad_code = sub { - return 0 if $code >= 200 && $code <= 299; - return 0 if $code == 416; - return 1; - }; + my $bad_code = ($code < 200 || $code >= 300) && $code != 416; - my $bad_size = sub { - return 0 unless defined $self->{reproxy_expected_size}; - return $self->{reproxy_expected_size} != $be->{res_headers}->header('Content-length'); - }; + my $bad_size = (defined $self->{reproxy_expected_size} && + $self->{reproxy_expected_size} != $be->{res_headers}->header('Content-length')); - if ($bad_code->() || $bad_size->()) { + if ($bad_code || $bad_size) { # fall back to an alternate URL $be->{client} = undef; $be->close('non_200_reproxy'); From e0f70a2a532ca56d0fde5213929c70f9707bead5 Mon Sep 17 00:00:00 2001 From: Jason Stubbs Date: Mon, 6 Feb 2012 10:57:20 +1100 Subject: [PATCH 3/3] Replace a closure with goto for performance --- lib/Perlbal/Service.pm | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/Perlbal/Service.pm b/lib/Perlbal/Service.pm index 6efa277..a7485ab 100644 --- a/lib/Perlbal/Service.pm +++ b/lib/Perlbal/Service.pm @@ -1182,17 +1182,6 @@ sub mark_node_used { sub get_client { my Perlbal::Service $self = shift; - my $ret = sub { - my Perlbal::ClientProxy $cp = shift; - $self->{waiting_client_count}--; - delete $self->{waiting_client_map}{$cp->{fd}}; - - # before we return, start another round of connections - $self->spawn_backends; - - return $cp; - }; - # determine if we should jump straight to the high priority queue or # act as pressure relief on the standard queue my $hp_first = 1; @@ -1211,24 +1200,33 @@ sub get_client { my $backlog = scalar @{$self->{waiting_clients}}; print "Got from fast queue, in front of $backlog others\n"; } - return $ret->($cp); + goto RETURN_CP; } # regular clients: while ($cp = shift @{$self->{waiting_clients}}) { next if $cp->{closed}; print "Backend requesting client, got normal = $cp->{fd}.\n" if Perlbal::DEBUG >= 2; - return $ret->($cp); + goto RETURN_CP; } # low-priority (batch/idle) clients. while ($cp = shift @{$self->{waiting_clients_lowpri}}) { next if $cp->{closed}; print "Backend requesting client, got low priority = $cp->{fd}.\n" if Perlbal::DEBUG >= 2; - return $ret->($cp); + goto RETURN_CP; } return undef; + + RETURN_CP: + $self->{waiting_client_count}--; + delete $self->{waiting_client_map}{$cp->{fd}}; + + # before we return, start another round of connections + $self->spawn_backends; + + return $cp; } # given a backend, verify it's generation