The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Plack::Middleware::Timeout

SYNOPSIS

    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;
        }
    );

DESCRIPTION

Timeout any plack requests at an arbitrary time.

PARAMETERS

timeout

Numeric value accepted by subroutine defined in Time::HiRes::alarm, default: 120 seconds.

response

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).

soft_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'

KNOWN LIMITATIONS

The module won't correctly handle the IO operations in progress - where the signals aren't delivered until after the read/write ends.

AUTHOR

Tomasz Czepiel <tjczepiel@gmail.com>

LICENCE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.