… | |
… | |
42 | my @pool; |
42 | my @pool; |
43 | |
43 | |
44 | # one "execution thread" |
44 | # one "execution thread" |
45 | sub handler { |
45 | sub handler { |
46 | while () { |
46 | while () { |
47 | my $new = pop @newcons; |
|
|
48 | if ($new) { |
47 | if (@newcons) { |
49 | eval { |
48 | eval { |
50 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
51 | }; |
50 | }; |
52 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
53 | $connections->up; |
52 | $connections->up; |
54 | } else { |
53 | } else { |
55 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
57 | schedule; |
56 | schedule; |
58 | } |
57 | } |
59 | } |
58 | } |
60 | } |
59 | } |
61 | |
60 | |
|
|
61 | sub listen_on { |
|
|
62 | my $listen = $_[0]; |
|
|
63 | |
|
|
64 | push @listen_sockets, $listen; |
|
|
65 | |
|
|
66 | # the "main thread" |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @newcons, [$listen->accept]; |
|
|
72 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
73 | if (@pool) { |
|
|
74 | (pop @pool)->ready; |
|
|
75 | } else { |
|
|
76 | async \&handler; |
|
|
77 | } |
|
|
78 | |
|
|
79 | } |
|
|
80 | }; |
|
|
81 | } |
|
|
82 | |
62 | my $http_port = new Coro::Socket |
83 | my $http_port = new Coro::Socket |
63 | LocalAddr => $SERVER_HOST, |
84 | LocalAddr => $SERVER_HOST, |
64 | LocalPort => $SERVER_PORT, |
85 | LocalPort => $SERVER_PORT, |
65 | ReuseAddr => 1, |
86 | ReuseAddr => 1, |
66 | Listen => 50, |
87 | Listen => 50, |
67 | or die "unable to start server"; |
88 | or die "unable to start server"; |
68 | |
89 | |
69 | push @listen_sockets, $http_port; |
90 | listen_on $http_port; |
|
|
91 | |
|
|
92 | if ($SERVER_PORT2) { |
|
|
93 | my $http_port = new Coro::Socket |
|
|
94 | LocalAddr => $SERVER_HOST, |
|
|
95 | LocalPort => $SERVER_PORT2, |
|
|
96 | ReuseAddr => 1, |
|
|
97 | Listen => 50, |
|
|
98 | or die "unable to start server"; |
|
|
99 | |
|
|
100 | listen_on $http_port; |
|
|
101 | } |
70 | |
102 | |
71 | our $NOW; |
103 | our $NOW; |
72 | our $HTTP_NOW; |
104 | our $HTTP_NOW; |
73 | |
105 | |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
106 | Event->timer(interval => 1, hard => 1, cb => sub { |
75 | $NOW = time; |
107 | $NOW = time; |
76 | $HTTP_NOW = time2str $NOW; |
108 | $HTTP_NOW = time2str $NOW; |
77 | })->now; |
109 | })->now; |
78 | |
|
|
79 | # the "main thread" |
|
|
80 | async { |
|
|
81 | slog 1, "accepting connections"; |
|
|
82 | while () { |
|
|
83 | $connections->down; |
|
|
84 | push @newcons, [$http_port->accept]; |
|
|
85 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
86 | if (@pool) { |
|
|
87 | (pop @pool)->ready; |
|
|
88 | } else { |
|
|
89 | async \&handler; |
|
|
90 | } |
|
|
91 | |
|
|
92 | } |
|
|
93 | }; |
|
|
94 | |
110 | |
95 | package conn; |
111 | package conn; |
96 | |
112 | |
97 | use Socket; |
113 | use Socket; |
98 | use HTTP::Date; |
114 | use HTTP::Date; |
… | |
… | |
141 | $self; |
157 | $self; |
142 | } |
158 | } |
143 | |
159 | |
144 | sub DESTROY { |
160 | sub DESTROY { |
145 | my $self = shift; |
161 | my $self = shift; |
146 | |
|
|
147 | $::conns--; |
162 | $::conns--; |
148 | |
|
|
149 | $self->eoconn; |
163 | $self->eoconn; |
150 | } |
164 | } |
151 | |
165 | |
152 | # end of connection |
166 | # end of connection |
153 | sub eoconn { |
167 | sub eoconn { |