diff options
-rw-r--r-- | src/http/network_thread.zig | 14 | ||||
-rw-r--r-- | src/io/io_linux.zig | 20 |
2 files changed, 31 insertions, 3 deletions
diff --git a/src/http/network_thread.zig b/src/http/network_thread.zig index d3826dcb3..4c47f1ffe 100644 --- a/src/http/network_thread.zig +++ b/src/http/network_thread.zig @@ -3,6 +3,7 @@ pub const Batch = ThreadPool.Batch; pub const Task = ThreadPool.Task; const std = @import("std"); const AsyncIO = @import("io"); +const Output = @import("../global.zig").Output; const NetworkThread = @This(); @@ -70,8 +71,19 @@ pub fn getAddressList(allocator: *std.mem.Allocator, name: []const u8, port: u16 pub fn init() !void { if ((global_loaded.swap(1, .Monotonic)) == 1) return; + AsyncIO.global = AsyncIO.init(512, 0) catch |err| { + 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)}); + Output.flush(); + + global = NetworkThread{ + .pool = ThreadPool.init(.{ .max_threads = 0, .stack_size = 64 * 1024 * 1024 }), + }; + global.pool.max_threads = 0; + AsyncIO.global_loaded = true; + return; + }; + AsyncIO.global_loaded = true; - AsyncIO.global = try AsyncIO.init(1024, 0); global = NetworkThread{ .pool = ThreadPool.init(.{ .max_threads = 1, .stack_size = 64 * 1024 * 1024 }), diff --git a/src/io/io_linux.zig b/src/io/io_linux.zig index f69f70c5b..b22dc92a5 100644 --- a/src/io/io_linux.zig +++ b/src/io/io_linux.zig @@ -62,8 +62,24 @@ unqueued: FIFO(Completion) = .{}, /// Completions that are ready to have their callbacks run. completed: FIFO(Completion) = .{}, -pub fn init(entries: u12, flags: u32) !IO { - return IO{ .ring = try IO_Uring.init(entries, flags) }; +pub fn init(entries_: u12, flags: u32) !IO { + var ring: IO_Uring = undefined; + var entries = entries_; + while (true) { + ring = IO_Uring.init(entries, flags) catch |err| { + if (err == error.SystemResources) { + if (entries < 4) return error.SystemResources; + entries /= 2; + continue; + } + + return err; + }; + break; + } + + + return IO{ .ring = ring }; } pub fn deinit(self: *IO) void { |