ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.87 by root, Wed Nov 19 11:41:06 2008 UTC vs.
Revision 1.94 by root, Sat Aug 13 23:42:05 2011 UTC

1use AnyEvent ();
2
1use Coro; 3use Coro;
2use Coro::Semaphore; 4use Coro::Semaphore;
5use Coro::SemaphoreSet;
3use Coro::EV; 6use Coro::EV;
4use Coro::Socket; 7use Coro::Socket;
5use Coro::Signal; 8use Coro::Signal;
6use Coro::AIO (); 9use Coro::AIO ();
7 10
11use Fcntl;
8use HTTP::Date; 12use HTTP::Date;
9use POSIX (); 13use POSIX ();
10 14
11use Compress::Zlib (); 15use Compress::Zlib ();
12 16
13no utf8; 17use common::sense;
14use bytes;
15 18
16# at least on my machine, this thingy serves files 19# at least on my machine, this thingy serves files
17# quite a bit faster than apache, ;) 20# quite a bit faster than apache, ;)
18# and quite a bit slower than thttpd :( 21# and quite a bit slower than thttpd :(
19 22
20$SIG{PIPE} = 'IGNORE'; 23$SIG{PIPE} = 'IGNORE';
21 24
22our $accesslog; 25our $accesslog;
23our $errorlog; 26our $errorlog;
27our @listen_sockets;
24 28
25our $NOW; 29our $NOW;
26our $HTTP_NOW; 30our $HTTP_NOW;
27 31
28our $ERROR_LOG; 32our $ERROR_LOG;
29our $ACCESS_LOG; 33our $ACCESS_LOG;
34our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip
30 35
31our $update_time = EV::periodic 0, 1, undef, sub { 36our $update_time = EV::periodic 0, 1, undef, sub {
32 $NOW = time; 37 $NOW = time;
33 $HTTP_NOW = time2str $NOW; 38 $HTTP_NOW = time2str $NOW;
34}; 39};
49} 54}
50 55
51sub slog { 56sub slog {
52 my $level = shift; 57 my $level = shift;
53 my $format = shift; 58 my $format = shift;
59
60 $format = sprintf $format, @_ if @_;
61
54 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); 62 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW);
55 printf "$NOW: $format\n", @_; 63 print "$NOW: $format\n";
56 printf $errorlog "$NOW: $format\n", @_ if $errorlog; 64 print $errorlog "$NOW: $format\n", @_ if $errorlog;
57} 65}
58 66
59our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 67our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250;
60our $httpevent = new Coro::Signal; 68our $httpevent = new Coro::Signal;
61 69
62our $queue_file = new transferqueue $MAX_TRANSFERS; 70our $queue_file = new transferqueue $::MAX_TRANSFERS;
63our $queue_index = new transferqueue 10; 71our $queue_index = new transferqueue 10;
64 72
65our $tbf_top = new tbf rate => $TBF_RATE || 100000; 73our $tbf_top = new tbf rate => $::TBF_RATE || 100000;
66 74
67my $unused_bytes = 0; 75my $unused_bytes = 0;
68my $unused_last = time; 76my $unused_last = time;
69 77
70sub unused_bandwidth { 78sub unused_bandwidth {
71 $unused_bytes += $_[0]; 79 $unused_bytes += $_[0];
72 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { 80 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
73 $unused_last = $NOW; 81 $unused_last = $NOW;
74 $unused_bytes = 0; 82 $unused_bytes = 0;
75 $queue_file->force_wake_next; 83 $queue_file->force_wake_next
76 slog 1, "forced filetransfer due to unused bandwidth"; 84 and slog 1, "forced filetransfer due to unused bandwidth";
77 }
78}
79
80my @newcons;
81my @pool;
82
83# one "execution thread"
84sub handler {
85 while () {
86 if (@newcons) {
87 eval {
88 conn->new (@{pop @newcons})->handle;
89 };
90 slog 1, "$@" if $@ && !ref $@;
91
92 $httpevent->broadcast; # only for testing, but doesn't matter much
93
94 $connections->up;
95 } else {
96 last if @pool >= $MAX_POOL;
97 push @pool, $Coro::current;
98 schedule;
99 }
100 } 85 }
101} 86}
102 87
103sub listen_on { 88sub listen_on {
104 my $listen = $_[0]; 89 my $listen = $_[0];
108 # the "main thread" 93 # the "main thread"
109 async { 94 async {
110 slog 1, "accepting connections"; 95 slog 1, "accepting connections";
111 while () { 96 while () {
112 $connections->down; 97 $connections->down;
113 push @newcons, [$listen->accept]; 98 my @conn = $listen->accept;
114 #slog 3, "accepted @$connections ".scalar(@pool); 99 #slog 3, "accepted @$connections ".scalar(@pool);
115 if (@pool) { 100
116 (pop @pool)->ready; 101 async_pool {
117 } else { 102 eval {
118 async \&handler; 103 conn->new (@conn)->handle;
104 };
105 slog 1, "$@" if $@ && !ref $@;
106
107 $httpevent->broadcast; # only for testing, but doesn't matter much
108
109 $connections->up;
119 } 110 }
120 } 111 }
121 }; 112 };
122} 113}
123 114
124my $http_port = new Coro::Socket 115my $http_port = new Coro::Socket
125 LocalAddr => $SERVER_HOST, 116 LocalAddr => $::SERVER_HOST,
126 LocalPort => $SERVER_PORT, 117 LocalPort => $::SERVER_PORT,
127 ReuseAddr => 1, 118 ReuseAddr => 1,
128 Listen => 50, 119 Listen => 50,
129 or die "unable to start server"; 120 or die "unable to start server";
130 121
131listen_on $http_port; 122listen_on $http_port;
132 123
133if ($SERVER_PORT2) { 124if ($::SERVER_PORT2) {
134 my $http_port = new Coro::Socket 125 my $http_port = new Coro::Socket
135 LocalAddr => $SERVER_HOST, 126 LocalAddr => $::SERVER_HOST,
136 LocalPort => $SERVER_PORT2, 127 LocalPort => $::SERVER_PORT2,
137 ReuseAddr => 1, 128 ReuseAddr => 1,
138 Listen => 50, 129 Listen => 50,
139 or die "unable to start server"; 130 or die "unable to start server";
140 131
141 listen_on $http_port; 132 listen_on $http_port;
142} 133}
143 134
144package conn; 135package conn;
145 136
146use strict; 137use common::sense;
147use bytes;
148 138
149use Socket; 139use Socket;
150use HTTP::Date; 140use HTTP::Date;
151use Convert::Scalar 'weaken'; 141use Convert::Scalar 'weaken';
152use IO::AIO; 142use IO::AIO;
143use AnyEvent::AIO;
153 144
154IO::AIO::min_parallel $::AIO_PARALLEL; 145IO::AIO::min_parallel $::AIO_PARALLEL;
155
156our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
157 146
158our %conn; # $conn{ip}{self} => connobj 147our %conn; # $conn{ip}{self} => connobj
159our %uri; # $uri{ip}{uri}{self} 148our %uri; # $uri{ip}{uri}{self}
160our %blocked; 149our %blocked;
161our %mimetype; 150our %mimetype;
221 for (keys %blocked) { 210 for (keys %blocked) {
222 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 211 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
223 } 212 }
224} 213}
225 214
226our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; 215our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches;
227 216
228sub slog { 217sub slog {
229 my $self = shift; 218 my $self = shift;
230 main::slog($_[0], "$self->{remote_id}> $_[1]"); 219 main::slog($_[0], "$self->{remote_id}> $_[1]");
231} 220}
483 } else { 472 } else {
484 $self->err (404, "not found"); 473 $self->err (404, "not found");
485 } 474 }
486 } else { 475 } else {
487 476
488 stat $path 477 Coro::AIO::aio_stat $path
489 or $self->err (404, "not found"); 478 and $self->err (404, "not found");
490 479
491 $self->{stat} = [stat _]; 480 $self->{stat} = [stat _];
492 481
493 # idiotic netscape sends idiotic headers AGAIN 482 # idiotic netscape sends idiotic headers AGAIN
494 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 483 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
604 593
605 if ($self->{method} eq "GET") { 594 if ($self->{method} eq "GET") {
606 $self->{time} = $::NOW; 595 $self->{time} = $::NOW;
607 $self->{written} = 0; 596 $self->{written} = 0;
608 597
609 open my $fh, "<", $self->{path} 598 my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0
610 or die "$self->{path}: late open failure ($!)"; 599 or die "$self->{path}: late open failure ($!)";
611 600
612 $h -= $l - 1; 601 $h -= $l - 1;
613 602
614 my $transfer = $queue->start_transfer ($h); 603 my $transfer = $queue->start_transfer ($h);
615 my $locked; 604 my $locked;
616 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 605 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
617 606
618 while ($h > 0) { 607 while ($h > 0) {
608 Coro::cede;
609 my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id});
610
619 unless ($locked) { 611 unless ($locked) {
620 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { 612 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
621 $bufsize = $::BUFSIZE; 613 $bufsize = $::BUFSIZE;
622 $self->{time} = $::NOW; 614 $self->{time} = $::NOW;
623 $self->{written} = 0; 615 $self->{written} = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines