File: Runtime.pm

package info (click to toggle)
libplack-perl 1.0033-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,472 kB
  • ctags: 406
  • sloc: perl: 5,259; python: 6; makefile: 4
file content (58 lines) | stat: -rw-r--r-- 1,030 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Plack::Middleware::Runtime;
use strict;
use parent qw(Plack::Middleware);
use Plack::Util;
use Plack::Util::Accessor qw(header_name);
use Time::HiRes;

sub call {
    my($self, $env) = @_;

    my $start = [ Time::HiRes::gettimeofday ];
    my $res = $self->app->($env);
    my $header = $self->header_name || 'X-Runtime';

    $self->response_cb($res, sub {
        my $res = shift;
        my $req_time = sprintf '%.6f', Time::HiRes::tv_interval($start);
        Plack::Util::header_set($res->[1], $header, $req_time);
    });
}

1;

__END__

=head1 NAME

Plack::Middleware::Runtime - Sets an X-Runtime response header

=head1 SYNOPSIS

  enable "Runtime";

=head1 DESCRIPTION

Plack::Middleware::Runtime is a Plack middleware component that sets
the application's response time (in seconds) in the I<X-Runtime> HTTP response
header.

=head1 OPTIONS

=over 4

=item header_name

Name of the header. Defaults to I<X-Runtime>.

=back

=head1 AUTHOR

Tatsuhiko Miyagawa

=head1 SEE ALSO

L<Time::HiRes> Rack::Runtime

=cut