简体   繁体   中英

Perl WebService increase max connections using HTTP::Server::Simple::CGI

I'm running a little Perl Webservice, based on the example i found on this page : https://www.perlmonks.org/?node_id=1078567 (first example)

However, when a lot of clients are calling it at once, it looks like the requests are suddenly crashing, and there's a lot of TIME_WAIT tcp connections left on the server running the webservice, as if the webservice was not able to handle that many connections at once.

is there a parameter in that module or other that i could use to extend this ? or a way to put some kind of queue for the incoming requests ?

some parts of my code, to help :

{
 package TACWebService;

 use HTTP::Server::Simple::CGI;
 use base qw(HTTP::Server::Simple::CGI);
 use Cwd 'abs_path';
 use POSIX;
 use DBI;
 use warnings;

 .........

     my %dispatch = (
     '/insertunix'    => \&resp_insertunix,
     '/insertwin'     => \&resp_insertwin,
     '/getpwdate'     => \&resp_getpwdate,
 );

 # ---------------------------------------------------------------------
 # Requests Handling
 # ---------------------------------------------------------------------
 sub handle_request {
     my $self = shift;
     my $cgi  = shift;

     my $path = $cgi->path_info();
     my $handler = $dispatch{$path};

     if (ref($handler) eq "CODE") {
         print "HTTP/1.0 200 OK\r\n";
         $handler->($cgi);

     } else {
         print "HTTP/1.0 404 Not found\r\n";
         print $cgi->header,
               $cgi->start_html('Not found'),
               $cgi->h1('Not found'),
               $cgi->end_html;
     }
 }

sub resp_insertwin {
     my $cgi  = shift;   # CGI.pm object
     return if !ref $cgi;

     ....

     } else {
             print $cgi->header("text/plain"), "INSERT"; 
         }


 .....

 # ---------------------------------------------------------------------
 # WebService Start in background
 # ---------------------------------------------------------------------
 my $pid = TACWebService->new($TACWebService::conf{tac_ws_port})->background();
 print "Use 'kill $pid' to stop TAC WebService.\n";

the clients themselves are using use LWP::UserAgent like this :

my $ua       = LWP::UserAgent->new();
$ua->timeout($timeout);
my $response = $ua->post($TAC_Url,
   [
        'args' => $here,
   ]

   if (!$response->is_success) {
    print "Timeout while connecting to $TAC_Url\n";
} else {
    my $content  = $response->as_string();
    print $content if (grep(/INSERT_/,$content));
}

to describe the exact issue would be complicated. In short : the clients are Unix servers sending their user database (user accounts). and when lots of clients are sending this user db at once, i can see the webservice receiving half of the data, and answering "timeout" after a couple of accounts (probably because it's overloaded in some way)

thanks again

The problem is, that the client waits to long for the server to respond. To solve this you have to start the server multiple times. The easiest Solution to this is to add

sub net_server { 'Net::Server::PreFork' }

to your package TACWebService and the HTTP::Server::Simple::CGI will do the rest of the magick.

Or you can use HTTP::Server::Simple::CGI::PreFork instead. See https://metacpan.org/pod/HTTP::Server::Simple::CGI::PreFork

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM