Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Last active December 14, 2015 16:28
Show Gist options
  • Select an option

  • Save kazeburo/5114870 to your computer and use it in GitHub Desktop.

Select an option

Save kazeburo/5114870 to your computer and use it in GitHub Desktop.
diff --git a/lib/Starlet/Server.pm b/lib/Starlet/Server.pm
index 4b0bcdc..45664a3 100644
--- a/lib/Starlet/Server.pm
+++ b/lib/Starlet/Server.pm
@@ -89,8 +89,25 @@ sub accept_loop {
my($self, $app, $max_reqs_per_child) = @_;
my $proc_req_count = 0;
+ $self->{can_exit} = 1;
+ $self->{term_received} = 0;
+ my $is_keepalive = 0;
+ local $SIG{TERM} = sub {
+ exit 0 unless $self->{register_sigterm};
+ $self->{term_received}++;
+ exit 0
+ if ($is_keepalive && $self->{can_exit}) || $self->{term_received} > 1;
+ # warn "server termination delayed while handling current HTTP request";
+ };
+
+ $self->{ignore_sigpipe} = 0;
+ local $SIG{PIPE} = sub {
+ return if $self->{ignore_sigpipe};
+ exit 1;
+ };
+
while (! defined $max_reqs_per_child || $proc_req_count < $max_reqs_per_child) {
- local $SIG{PIPE} = 'IGNORE';
+ local $self->{ignore_sigpipe} = 1;
if (my $conn = $self->{listen_sock}->accept) {
$self->{_is_deferred_accept} = $self->{_using_defer_accept};
$conn->blocking(0)
@@ -124,6 +141,7 @@ sub accept_loop {
if ($may_keepalive && $max_reqs_per_child && $proc_req_count >= $max_reqs_per_child) {
$may_keepalive = undef;
}
+ $is_keepalive = ($req_count != 1) ? 1 : 0;
$self->handle_connection($env, $conn, $app, $may_keepalive, $req_count != 1)
or last;
# TODO add special cases for clients with broken keep-alive support, as well as disabling keep-alive for HTTP/1.0 proxies
@@ -139,21 +157,14 @@ sub handle_connection {
my $buf = '';
my $res = [ 400, [ 'Content-Type' => 'text/plain' ], [ 'Bad Request' ] ];
- my $can_exit = 1;
- my $term_received = 0;
- local $SIG{TERM} = sub {
- $term_received++;
- exit 0
- if ($is_keepalive && $can_exit) || $term_received > 1;
- # warn "server termination delayed while handling current HTTP request";
- };
-
+ $self->{can_exit} = 1;
+ local $self->{register_sigterm} = 1;
while (1) {
my $rlen = $self->read_timeout(
$conn, \$buf, MAX_REQUEST_SIZE - length($buf), length($buf),
$is_keepalive ? $self->{keepalive_timeout} : $self->{timeout},
) or return;
- undef $can_exit;
+ $self->{can_exit} = 0;
my $reqlen = parse_http_request($buf, $env);
if ($reqlen >= 0) {
# handle request
@@ -206,8 +217,7 @@ sub handle_connection {
} else {
die "Bad response $res";
}
-
- if ($term_received) {
+ if ($self->{term_received}) {
exit 0;
}
@kazeburo

kazeburo commented Mar 8, 2013

Copy link
Copy Markdown
Author

before

Concurrency Level:      1
Time taken for tests:   21.485 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Total transferred:      4550000 bytes
HTML transferred:       100000 bytes
Requests per second:    2327.22 [#/sec] (mean)
Time per request:       0.430 [ms] (mean)   
Time per request:       0.430 [ms] (mean, across all concurrent requests)
Transfer rate:          206.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max 
Connect:        0    0   0.0      0       3 
Processing:     0    0   0.2      0      11 
Waiting:        0    0   0.2      0      11 
Total:          0    0   0.2      0      11 

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      1
  98%      1
  99%      1
 100%     11 (longest request)

after

Concurrency Level:      1
Time taken for tests:   18.738 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Total transferred:      4550000 bytes
HTML transferred:       100000 bytes
Requests per second:    2668.31 [#/sec] (mean)
Time per request:       0.375 [ms] (mean)   
Time per request:       0.375 [ms] (mean, across all concurrent requests)
Transfer rate:          237.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max 
Connect:        0    0   0.0      0       2 
Processing:     0    0   0.1      0       8 
Waiting:        0    0   0.1      0       8 
Total:          0    0   0.1      0       8 

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      1
  99%      1
 100%      8 (longest request)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment