Plack::Middleware::Timeout
my $app = sub { ... }; Plack::Middleware::Timeout->wrap( $app, timeout => 120, # optional callback to set the custom response response => sub { my ($response_obj) = @_; $response_obj->code(HTTP_REQUEST_TIMEOUT); $response_obj->body( encode_json({ timeout => 1, other_info => {...}, })); return $plack_response; } );
Timeout any plack requests at an arbitrary time.
Numeric value accepted by subroutine defined in Time::HiRes::alarm, default: 120 seconds.
Optional subroutine which will be exeuted when timeout is reached. The subref receives a Plack::Response object as argument. If the response subref isn't defined, we resolve to emitting a warning:
'Terminated request for uri '%s' due to timeout (%ds)'
and return a response code 504 (HTTP_GATEWAY_TIMEOUT).
Same as timeout, except this value will be checked after the call to the app has completed. See also on_soft_timeout below.
on_soft_timeout
optional coderef that'll get executed when we established, that time required to serve the response was longer than a value provided in the soft_timeout parameter. If soft_timeout is set but no on_soft_timeout coderef is provided, we're going to issue a warning as follows; the response will be returned as normal.
'Soft timeout reached for uri '%s' (soft timeout: %ds) request took %ds'
The module won't correctly handle the IO operations in progress - where the signals aren't delivered until after the read/write ends.
Tomasz Czepiel <tjczepiel@gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Plack::Middleware::Timeout, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::Timeout
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::Timeout
For more information on module installation, please visit the detailed CPAN module installation guide.