diff options
author | 2022-09-11 13:37:17 -0700 | |
---|---|---|
committer | 2022-09-11 13:37:17 -0700 | |
commit | 9a5aa059f91ecf14b5dfd28ec610df6d8373b5a1 (patch) | |
tree | b96909744baddf65bc4175c40bcc0d9d1658dbb1 /src/network_thread.zig | |
parent | 8b91360a33b782af423c85f9ec7277394e27beb4 (diff) | |
download | bun-9a5aa059f91ecf14b5dfd28ec610df6d8373b5a1.tar.gz bun-9a5aa059f91ecf14b5dfd28ec610df6d8373b5a1.tar.zst bun-9a5aa059f91ecf14b5dfd28ec610df6d8373b5a1.zip |
New HTTP client (#1231)
* wip
* It mostly works!
* Support `bun install`
* Support `bun create`
* Support chunked transfer encoding
* Handle Keep Alive when redirecting to a different domain
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/network_thread.zig')
-rw-r--r-- | src/network_thread.zig | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/src/network_thread.zig b/src/network_thread.zig index aa6fcf15d..56ea6a698 100644 --- a/src/network_thread.zig +++ b/src/network_thread.zig @@ -26,6 +26,79 @@ pub var global: NetworkThread = undefined; pub var global_loaded: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0); const log = Output.scoped(.NetworkThread, true); +const Global = @import("./global.zig").Global; +pub fn onStartIOThread(waker: AsyncIO.Waker) void { + NetworkThread.address_list_cached = NetworkThread.AddressListCache.init(@import("./global.zig").default_allocator); + AsyncIO.global = AsyncIO.init(1024, 0, waker) catch |err| { + log: { + if (comptime Environment.isLinux) { + if (err == error.SystemOutdated) { + Output.prettyErrorln( + \\<red>error<r>: Linux kernel version doesn't support io_uring, which Bun depends on. + \\ + \\ To fix this error: please upgrade to a newer Linux kernel. + \\ + \\ If you're using Windows Subsystem for Linux, here's how: + \\ 1. Open PowerShell as an administrator + \\ 2. Run this: + \\ wsl --update + \\ wsl --shutdown + \\ + \\ Please make sure you're using WSL version 2 (not WSL 1). To check: wsl -l -v + \\ If you are on WSL 1, update to WSL 2 with the following commands: + \\ 1. wsl --set-default-version 2 + \\ 2. wsl --set-version [distro_name] 2 + \\ 3. Now follow the WSL 2 instructions above. + \\ Where [distro_name] is one of the names from the list given by: wsl -l -v + \\ + \\ If that doesn't work (and you're on a Windows machine), try this: + \\ 1. Open Windows Update + \\ 2. Download any updates to Windows Subsystem for Linux + \\ + \\ If you're still having trouble, ask for help in bun's discord https://bun.sh/discord + , .{}); + break :log; + } else if (err == error.SystemResources) { + Output.prettyErrorln( + \\<red>error<r>: memlock limit exceeded + \\ + \\To fix this error: <b>please increase the memlock limit<r> or upgrade to Linux kernel 5.11+ + \\ + \\If Bun is running inside Docker, make sure to set the memlock limit to unlimited (-1) + \\ + \\ docker run --rm --init --ulimit memlock=-1:-1 jarredsumner/bun:edge + \\ + \\To bump the memlock limit, check one of the following: + \\ /etc/security/limits.conf + \\ /etc/systemd/user.conf + \\ /etc/systemd/system.conf + \\ + \\You can also try running bun as root. + \\ + \\If running many copies of Bun via exec or spawn, be sure that O_CLOEXEC is set so + \\that resources are not leaked when the child process exits. + \\ + \\Why does this happen? + \\ + \\Bun uses io_uring and io_uring accounts memory it + \\needs under the rlimit memlocked option, which can be + \\quite low on some setups (64K). + \\ + \\ + , .{}); + break :log; + } + } + + Output.prettyErrorln("<r><red>error<r>: Failed to initialize network thread: <red><b>{s}<r>.\nHTTP requests will not work. Please file an issue and run strace().", .{@errorName(err)}); + } + + Global.exit(1); + }; + AsyncIO.global_loaded = true; + NetworkThread.global.io = &AsyncIO.global; + NetworkThread.global.processEvents(); +} fn queueEvents(this: *@This()) void { this.queued_tasks_mutex.lock(); @@ -41,6 +114,7 @@ pub fn processEvents(this: *@This()) void { processEvents_(this) catch {}; unreachable; } + /// Should only be called on the HTTP thread! fn processEvents_(this: *@This()) !void { while (true) { @@ -164,7 +238,7 @@ pub fn init() !void { @compileLog("TODO: Waker"); } - global.thread = try std.Thread.spawn(.{ .stack_size = 64 * 1024 * 1024 }, HTTP.onThreadStartNew, .{ + global.thread = try std.Thread.spawn(.{ .stack_size = 64 * 1024 * 1024 }, onStartIOThread, .{ global.waker, }); global.thread.detach(); |