… | |
… | |
5 | |
5 | |
6 | use HTTP::Date; |
6 | use HTTP::Date; |
7 | |
7 | |
8 | no utf8; |
8 | no utf8; |
9 | use bytes; |
9 | use bytes; |
10 | |
|
|
11 | our @wait_time = (); # used to calculcate avg. waiting time |
|
|
12 | our $wait_time_length = 25; |
|
|
13 | |
10 | |
14 | # at least on my machine, this thingy serves files |
11 | # at least on my machine, this thingy serves files |
15 | # quite a bit faster than apache, ;) |
12 | # quite a bit faster than apache, ;) |
16 | # and quite a bit slower than thttpd :( |
13 | # and quite a bit slower than thttpd :( |
17 | |
14 | |
… | |
… | |
31 | my $format = shift; |
28 | my $format = shift; |
32 | printf "---: $format\n", @_; |
29 | printf "---: $format\n", @_; |
33 | } |
30 | } |
34 | |
31 | |
35 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
36 | our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; |
33 | |
|
|
34 | our $wait_factor = 0.95; |
|
|
35 | |
|
|
36 | our @transfers = ( |
|
|
37 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 600], |
|
|
38 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 600], |
|
|
39 | ); |
37 | |
40 | |
38 | my @newcons; |
41 | my @newcons; |
39 | my @pool; |
42 | my @pool; |
40 | |
43 | |
41 | # one "execution thread" |
44 | # one "execution thread" |
… | |
… | |
69 | our $HTTP_NOW; |
72 | our $HTTP_NOW; |
70 | |
73 | |
71 | Event->timer(interval => 1, hard => 1, cb => sub { |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
72 | $NOW = time; |
75 | $NOW = time; |
73 | $HTTP_NOW = time2str $NOW; |
76 | $HTTP_NOW = time2str $NOW; |
74 | }); |
77 | })->now; |
75 | |
78 | |
76 | # the "main thread" |
79 | # the "main thread" |
77 | async { |
80 | async { |
78 | slog 1, "accepting connections"; |
81 | slog 1, "accepting connections"; |
79 | while () { |
82 | while () { |
… | |
… | |
426 | $idx); |
429 | $idx); |
427 | } |
430 | } |
428 | |
431 | |
429 | sub handle_file { |
432 | sub handle_file { |
430 | my $self = shift; |
433 | my $self = shift; |
431 | my $length = -s _; |
434 | my $length = $self->{stat}[7]; |
|
|
435 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
432 | my $hdr = { |
436 | my $hdr = { |
433 | "Last-Modified" => time2str ((stat _)[9]), |
437 | "Last-Modified" => time2str ((stat _)[9]), |
434 | }; |
438 | }; |
435 | |
439 | |
436 | my @code = (200, "ok"); |
440 | my @code = (200, "ok"); |
… | |
… | |
482 | $self->response(@code, $hdr, ""); |
486 | $self->response(@code, $hdr, ""); |
483 | |
487 | |
484 | if ($self->{method} eq "GET") { |
488 | if ($self->{method} eq "GET") { |
485 | $self->{time} = $::NOW; |
489 | $self->{time} = $::NOW; |
486 | |
490 | |
487 | my $transfer = $::transfers->guard; |
491 | my $transfer = $queue->[0]->guard; |
488 | $self->{fh}->writable or return; |
492 | $self->{fh}->writable or return; |
489 | |
493 | |
490 | push @::wait_time, $::NOW - $self->{time}; |
494 | $queue->[1] = $queue->[1] * $::wait_factor |
491 | shift @::wait_time if @wait_time > $wait_time_length; |
495 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
492 | $self->{time} = $::NOW; |
496 | $self->{time} = $::NOW; |
493 | |
497 | |
494 | my ($fh, $buf, $r); |
498 | my ($fh, $buf, $r); |
495 | my $current = $Coro::current; |
499 | my $current = $Coro::current; |
496 | open $fh, "<", $self->{path} |
500 | open $fh, "<", $self->{path} |