… | |
… | |
42 | } |
42 | } |
43 | |
43 | |
44 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
44 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
45 | |
45 | |
46 | my @fh; |
46 | my @fh; |
47 | my @pool; |
|
|
48 | |
47 | |
49 | sub handler { |
48 | # move the event main loop into a coroutine |
|
|
49 | async { loop }; |
|
|
50 | |
|
|
51 | slog 1, "accepting connections"; |
50 | while () { |
52 | while () { |
51 | my $fh = pop @fh; |
53 | $connections->down; |
52 | if ($fh) { |
54 | if (my $fh = $port->accept) { |
|
|
55 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
56 | async_pool { |
53 | eval { |
57 | eval { |
54 | conn->new($fh)->handle; |
58 | conn->new($fh)->handle; |
55 | }; |
59 | }; |
56 | close $fh; |
60 | close $fh; |
57 | slog 1, "$@" if $@ && !ref $@; |
61 | slog 1, "$@" if $@ && !ref $@; |
58 | $connections->up; |
62 | $connections->up; |
59 | } else { |
|
|
60 | last if @pool >= $MAX_POOL; |
|
|
61 | push @pool, $Coro::current; |
|
|
62 | schedule; |
|
|
63 | } |
63 | }; |
64 | } |
64 | } |
65 | } |
65 | } |
66 | |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @fh, $port->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 | loop; |
|
|
83 | print "ende\n";#d# |
|
|
84 | |
66 | |
85 | package conn; |
67 | package conn; |
86 | |
68 | |
87 | use Socket; |
69 | use Socket; |
88 | use HTTP::Date; |
70 | use HTTP::Date; |
… | |
… | |
363 | } |
345 | } |
364 | } |
346 | } |
365 | |
347 | |
366 | close $fh; |
348 | close $fh; |
367 | } |
349 | } |
|
|
350 | |