diff options
author | 2022-02-01 20:47:48 -0800 | |
---|---|---|
committer | 2022-02-01 20:47:48 -0800 | |
commit | 64b74ede87b0a77a2f5ff838c3a5548fbb9f9002 (patch) | |
tree | 2f5b82a05a1a4acce8479841e5ad8dd0a9426cb4 /src/thread_pool.zig | |
parent | 213960a04a22bed4d0ffb3cdc9e20439bd1dcc10 (diff) | |
download | bun-64b74ede87b0a77a2f5ff838c3a5548fbb9f9002.tar.gz bun-64b74ede87b0a77a2f5ff838c3a5548fbb9f9002.tar.zst bun-64b74ede87b0a77a2f5ff838c3a5548fbb9f9002.zip |
Cleanup HTTP thread on idle
Diffstat (limited to 'src/thread_pool.zig')
-rw-r--r-- | src/thread_pool.zig | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/thread_pool.zig b/src/thread_pool.zig index fd1fb7e83..8281e473b 100644 --- a/src/thread_pool.zig +++ b/src/thread_pool.zig @@ -201,7 +201,7 @@ fn _wait(self: *ThreadPool, _is_waking: bool, comptime sleep_on_idle: bool) erro var is_idle = false; var is_waking = _is_waking; var sync = @bitCast(Sync, self.sync.load(.Monotonic)); - var idle_network_ticks: if (sleep_on_idle) u16 else void = if (comptime sleep_on_idle) 0 else void{}; + var idle_network_ticks: u32 = 0; while (true) { if (sync.state == .shutdown) return error.Shutdown; @@ -262,22 +262,26 @@ fn _wait(self: *ThreadPool, _is_waking: bool, comptime sleep_on_idle: bool) erro if (self.io) |io| { const HTTP = @import("http"); io.tick() catch {}; + const end_count = HTTP.AsyncHTTP.active_requests_count.loadUnchecked(); - if (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > 0) { - while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > HTTP.AsyncHTTP.max_simultaneous_requests) { + if (end_count > 0) { + while (HTTP.AsyncHTTP.active_requests_count.loadUnchecked() > HTTP.AsyncHTTP.max_simultaneous_requests) { io.run_for_ns(std.time.ns_per_us * 10) catch {}; } } + const idle = HTTP.AsyncHTTP.active_requests_count.loadUnchecked() == 0; + if (sleep_on_idle) { - idle_network_ticks += @as(u16, @boolToInt(HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) == 0)); + idle_network_ticks += @as(u32, @boolToInt(idle)); // If it's been roughly 2ms since the last network request, go to sleep! // this is 4ms because run_for_ns runs for 10 microseconds // 10 microseconds * 400 == 4ms if (idle_network_ticks > 40) { - self.idle_event.wait(); idle_network_ticks = 0; + HTTP.cleanup(true); + self.idle_event.wait(); } } |