diff options
Diffstat (limited to 'web/main.zig')
| -rw-r--r-- | web/main.zig | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/web/main.zig b/web/main.zig index eaf6804..9d108c6 100644 --- a/web/main.zig +++ b/web/main.zig @@ -186,7 +186,7 @@ pub fn main(init: std.process.Init) !void { }; defer m.stop(); - var listener = c9.transport.listen(io, .{ .tcp = address }, max_connections) catch |err| { + var listener = listenHttp(io, address) catch |err| { if (err == error.AddressInUse) { std.debug.print("9web: cannot listen on {s}: address already in use.\nUse --listen 127.0.0.1:0 to select a free port; the URL is printed at startup.\n", .{listen_text}); } else std.debug.print("9web: cannot listen on {s}: {s}\n", .{ listen_text, @errorName(err) }); @@ -227,7 +227,7 @@ pub fn main(init: std.process.Init) !void { const l = try allocator.create(Io.net.Server); var bind_address = address; bind_address.setPort(site.port); - l.* = try c9.transport.listen(io, .{ .tcp = bind_address }, max_connections); + l.* = try listenHttp(io, bind_address); try group.concurrent(io, siteAcceptLoop, .{ &app, l, &group, si }); std.debug.print("9web: site {s} on {f} from {s}\n", .{ site.name, l.socket.address, site.root }); } @@ -278,6 +278,36 @@ pub fn main(init: std.process.Init) !void { } } +fn listenHttp(io: Io, address: Io.net.IpAddress) !Io.net.Server { + // HTTP's short connections leave TIME_WAIT sockets behind. Reuse addresses + // across service restarts, but do not share a live port between instances. + if (@import("builtin").os.tag != .linux) + return address.listen(io, .{ .kernel_backlog = max_connections }); + // std.Io's reuse_address also sets REUSEPORT. Configure only REUSEADDR + // before bind, then hand the socket back to std.Io for cancellable accepts. + const linux = std.os.linux; + var addr: std.c.sockaddr.storage = undefined; + var len = c9.transport.ipSockaddr(address, &addr); + const rc = linux.socket(addr.family, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, linux.IPPROTO.TCP); + if (linux.errno(rc) != .SUCCESS) return error.Socket; + const fd: i32 = @intCast(rc); + errdefer _ = linux.close(fd); + const on: c_int = 1; + if (linux.errno(linux.setsockopt(fd, linux.SOL.SOCKET, linux.SO.REUSEADDR, @ptrCast(&on), @sizeOf(c_int))) != .SUCCESS) + return error.SocketOption; + switch (linux.errno(linux.bind(fd, @ptrCast(&addr), len))) { + .SUCCESS => {}, + .ADDRINUSE => return error.AddressInUse, + else => return error.Bind, + } + if (linux.errno(linux.listen(fd, max_connections)) != .SUCCESS) return error.Listen; + if (linux.errno(linux.getsockname(fd, @ptrCast(&addr), &len)) != .SUCCESS) return error.SocketAddress; + var bound = address; + const actual: *const std.c.sockaddr.in = @ptrCast(&addr); // port has the same offset in sockaddr_in6 + bound.setPort(std.mem.bigToNative(u16, actual.port)); + return .{ .socket = .{ .handle = fd, .address = bound }, .options = {} }; +} + fn siteAcceptLoop(app: *App, listener: *Io.net.Server, group: *Io.Group, si: usize) void { defer listener.deinit(app.io); while (true) { |
