web: Update to zig 0.16.0
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
const Server = @This();
|
||||
|
||||
const Address = @import("Address.zig").Address;
|
||||
const Connection = @import("Connection.zig");
|
||||
const FileDescriptor = @import("FileDescriptor.zig").FileDescriptor;
|
||||
const http = @import("http.zig");
|
||||
@@ -11,10 +12,10 @@ const Worker = @import("Worker.zig");
|
||||
|
||||
const log = std.log.scoped(.Server);
|
||||
const linux = std.os.linux;
|
||||
const errno = linux.E.init;
|
||||
const errno = linux.errno;
|
||||
|
||||
fd: FileDescriptor,
|
||||
address: std.net.Address,
|
||||
address: Address,
|
||||
ssl_ctx: ?*openssl.SslContext,
|
||||
workers: []Worker,
|
||||
threads: []std.Thread,
|
||||
@@ -29,9 +30,10 @@ connection_queue: std.DoublyLinkedList,
|
||||
connection_pool: std.DoublyLinkedList,
|
||||
connection_buffer: []Connection,
|
||||
|
||||
mutex: std.Thread.Mutex,
|
||||
cond_connection_queued: std.Thread.Condition,
|
||||
cond_connection_freed: std.Thread.Condition,
|
||||
io: std.Io,
|
||||
mutex: std.Io.Mutex,
|
||||
cond_connection_queued: std.Io.Condition,
|
||||
cond_connection_freed: std.Io.Condition,
|
||||
|
||||
/// 4 kiB
|
||||
const page_size = 4 * 1024;
|
||||
@@ -40,7 +42,7 @@ const huge_page_size = 2 * 1024 * 1024;
|
||||
|
||||
pub const Options = struct {
|
||||
request_handler: RequestHandler,
|
||||
address: std.net.Address = .initIp4(.{ 127, 0, 0, 1 }, 8000),
|
||||
address: Address = .initIp4(.{ 127, 0, 0, 1 }, 8000),
|
||||
/// If not `null`, the server will use TLS with the provided OpenSSL
|
||||
/// context.
|
||||
ssl_ctx: ?*openssl.SslContext = null,
|
||||
@@ -73,7 +75,7 @@ pub const Options = struct {
|
||||
read_timeout_us: u64 = 1 * std.time.us_per_s,
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
pub fn init(allocator: std.mem.Allocator, io: std.Io, options: Options) !Server {
|
||||
const worker_count = if (options.worker_count > 0) options.worker_count else try std.Thread.getCpuCount();
|
||||
|
||||
// Create socket fd
|
||||
@@ -123,7 +125,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
const read_buffer_ptr = try errOrPtr(linux.mmap(
|
||||
null,
|
||||
double_all_read_buffers_size,
|
||||
linux.PROT.NONE,
|
||||
.{},
|
||||
linux.MAP{ .TYPE = .PRIVATE, .ANONYMOUS = true },
|
||||
-1,
|
||||
0,
|
||||
@@ -138,8 +140,8 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
try err(linux.mmap(
|
||||
read_buffer_ptr + double_offset,
|
||||
single_read_buffer_size,
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .SHARED, .FIXED = true },
|
||||
.{ .READ = true, .WRITE = true },
|
||||
.{ .TYPE = .SHARED, .FIXED = true },
|
||||
@intFromEnum(read_buffer_fd),
|
||||
@intCast(offset),
|
||||
));
|
||||
@@ -147,8 +149,8 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
try err(linux.mmap(
|
||||
read_buffer_ptr + double_offset + single_read_buffer_size,
|
||||
single_read_buffer_size,
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .SHARED, .FIXED = true },
|
||||
.{ .READ = true, .WRITE = true },
|
||||
.{ .TYPE = .SHARED, .FIXED = true },
|
||||
@intFromEnum(read_buffer_fd),
|
||||
@intCast(offset),
|
||||
));
|
||||
@@ -162,8 +164,8 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
const header_write_buffer_ptr = try errOrPtr(linux.mmap(
|
||||
null,
|
||||
all_header_write_buffers_size,
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .PRIVATE, .ANONYMOUS = true },
|
||||
.{ .READ = true, .WRITE = true },
|
||||
.{ .TYPE = .PRIVATE, .ANONYMOUS = true },
|
||||
-1,
|
||||
0,
|
||||
));
|
||||
@@ -177,8 +179,8 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
const body_write_buffer_ptr = try errOrPtr(linux.mmap(
|
||||
null,
|
||||
all_body_write_buffers_size,
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .PRIVATE, .ANONYMOUS = true },
|
||||
.{ .READ = true, .WRITE = true },
|
||||
.{ .TYPE = .PRIVATE, .ANONYMOUS = true },
|
||||
-1,
|
||||
0,
|
||||
));
|
||||
@@ -232,9 +234,10 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
.connection_pool = connection_pool,
|
||||
.connection_buffer = connection_buffer,
|
||||
|
||||
.mutex = .{},
|
||||
.cond_connection_queued = .{},
|
||||
.cond_connection_freed = .{},
|
||||
.io = io,
|
||||
.mutex = .init,
|
||||
.cond_connection_queued = .init,
|
||||
.cond_connection_freed = .init,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -284,7 +287,7 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
log.debug("Storing `false` into worker_running.", .{});
|
||||
worker_running.store(false, .release);
|
||||
log.debug("Broadcasting connection queued condition variable.", .{});
|
||||
self.cond_connection_queued.broadcast();
|
||||
self.cond_connection_queued.broadcast(self.io);
|
||||
for (self.threads[0..spawned], 0..) |*thread, i| {
|
||||
log.debug("Joining the thread of worker #{d}.", .{i});
|
||||
thread.join();
|
||||
@@ -298,8 +301,8 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
}
|
||||
|
||||
while (running.load(.acquire)) {
|
||||
var address: std.net.Address = undefined;
|
||||
var address_size: u32 = @sizeOf(std.net.Address);
|
||||
var address: Address = undefined;
|
||||
var address_size: u32 = @sizeOf(Address);
|
||||
|
||||
log.debug("Accepting connection.", .{});
|
||||
const fd = self.fd.accept(&address.any, &address_size) catch |e| {
|
||||
@@ -322,11 +325,11 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
|
||||
{
|
||||
log.debug("Acquiring mutex.", .{});
|
||||
self.mutex.lock();
|
||||
self.mutex.lockUncancelable(self.io);
|
||||
log.debug("Acquired mutex.", .{});
|
||||
defer {
|
||||
log.debug("Unlocking mutex.", .{});
|
||||
self.mutex.unlock();
|
||||
self.mutex.unlock(self.io);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
@@ -339,13 +342,13 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
}
|
||||
|
||||
log.debug("Waiting on connection freed condition variable.", .{});
|
||||
self.cond_connection_freed.wait(&self.mutex);
|
||||
self.cond_connection_freed.waitUncancelable(self.io, &self.mutex);
|
||||
log.debug("Woken up on connection freed condition variable.", .{});
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("Signaling connection queued condition variable.", .{});
|
||||
self.cond_connection_queued.signal();
|
||||
self.cond_connection_queued.signal(self.io);
|
||||
} else {
|
||||
log.debug("Loaded `false` from running, the accept loop exited.", .{});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user