Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected the logic to take into account max_requests with keep-alive connections #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/Starman/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ sub post_accept_hook {
headerbuf => '',
inputbuf => '',
keepalive => 1,
keepalive_requests => 0,
};
}

Expand Down Expand Up @@ -216,6 +217,9 @@ sub process_request {
# Read until we see all headers
last if !$self->_read_headers;

$self->{client}->{keepalive_requests} += 1;
$self->{server}->{requests} += 1 if $self->{client}->{keepalive_requests} > 1;

my $env = {
REMOTE_ADDR => $self->{server}->{peeraddr},
REMOTE_HOST => $self->{server}->{peerhost} || $self->{server}->{peeraddr},
Expand Down Expand Up @@ -322,6 +326,8 @@ sub process_request {
}
}

last if $self->max_requests_reached;

DEBUG && warn "[$$] Waiting on previous connection for keep-alive request...\n";

my $sel = IO::Select->new($conn);
Expand Down Expand Up @@ -516,7 +522,7 @@ sub _finalize_response {
}

# Should we keep the connection open?
if ( $self->{client}->{keepalive} ) {
if ( $self->{client}->{keepalive} && !$self->max_requests_reached ) {
push @headers, 'Connection: keep-alive';
} else {
push @headers, 'Connection: close';
Expand Down Expand Up @@ -586,4 +592,9 @@ sub post_client_connection_hook {
}
}

sub max_requests_reached {
my $self = shift;
return $self->{server}->{requests} >= $self->{server}->{max_requests};
}

1;