… | |
… | |
75 | $queue_file->force_wake_next; |
75 | $queue_file->force_wake_next; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
77 | } |
77 | } |
78 | } |
78 | } |
79 | |
79 | |
80 | my @newcons; |
|
|
81 | my @pool; |
|
|
82 | |
|
|
83 | # one "execution thread" |
|
|
84 | sub 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 | } |
|
|
101 | } |
|
|
102 | |
|
|
103 | sub listen_on { |
80 | sub listen_on { |
104 | my $listen = $_[0]; |
81 | my $listen = $_[0]; |
105 | |
82 | |
106 | push @listen_sockets, $listen; |
83 | push @listen_sockets, $listen; |
107 | |
84 | |
108 | # the "main thread" |
85 | # the "main thread" |
109 | async { |
86 | async { |
110 | slog 1, "accepting connections"; |
87 | slog 1, "accepting connections"; |
111 | while () { |
88 | while () { |
112 | $connections->down; |
89 | $connections->down; |
113 | push @newcons, [$listen->accept]; |
90 | my @conn = $listen->accept; |
114 | #slog 3, "accepted @$connections ".scalar(@pool); |
91 | #slog 3, "accepted @$connections ".scalar(@pool); |
115 | if (@pool) { |
92 | |
116 | (pop @pool)->ready; |
93 | async_pool { |
117 | } else { |
94 | eval { |
118 | async \&handler; |
95 | conn->new (@conn)->handle; |
|
|
96 | }; |
|
|
97 | slog 1, "$@" if $@ && !ref $@; |
|
|
98 | |
|
|
99 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
100 | |
|
|
101 | $connections->up; |
119 | } |
102 | } |
120 | } |
103 | } |
121 | }; |
104 | }; |
122 | } |
105 | } |
123 | |
106 | |