web: fix compile errors and critical runtime errors
This commit is contained in:
@@ -13,6 +13,7 @@ const errno = linux.E.init;
|
||||
fd: FileDescriptor,
|
||||
address: std.net.Address,
|
||||
workers: []Worker,
|
||||
threads: []std.Thread,
|
||||
request_router: RequestRouter,
|
||||
|
||||
connection_queue: std.DoublyLinkedList,
|
||||
@@ -65,21 +66,22 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
|
||||
var socklen = options.address.getOsSockLen();
|
||||
try fd.bind(&options.address.any, socklen);
|
||||
try fd.listen(options.kernel_backlog);
|
||||
try fd.listen(options.max_connections);
|
||||
|
||||
var listen_address = options.address;
|
||||
try fd.getsockname(fd, &listen_address, &socklen);
|
||||
try fd.getsockname(&listen_address.any, &socklen);
|
||||
|
||||
// Allocate workers
|
||||
// Allocate arrays
|
||||
|
||||
const workers = try allocator.alloc(Worker, worker_count);
|
||||
errdefer allocator.free(workers);
|
||||
|
||||
// Allocate connection pool
|
||||
|
||||
const connection_buffer = try allocator.alloc(Connection, options.max_connections);
|
||||
errdefer allocator.free(connection_buffer);
|
||||
|
||||
const threads = try allocator.alloc(std.Thread, worker_count);
|
||||
errdefer allocator.free(threads);
|
||||
|
||||
// Allocate and remap read buffers
|
||||
|
||||
const single_read_buffer_size = @as(usize, options.read_buffer_pages) * huge_page_size;
|
||||
@@ -91,7 +93,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
const read_buffer_fd: FileDescriptor = try .memfd_create("read_buffer", 0);
|
||||
defer read_buffer_fd.close();
|
||||
|
||||
try read_buffer_fd.ftruncate(all_read_buffers_size);
|
||||
try read_buffer_fd.ftruncate(@intCast(all_read_buffers_size));
|
||||
|
||||
const read_buffer_ptr = try errOrPtr(linux.mmap(
|
||||
null,
|
||||
@@ -114,7 +116,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .SHARED, .FIXED = true },
|
||||
@intFromEnum(read_buffer_fd),
|
||||
offset,
|
||||
@intCast(offset),
|
||||
));
|
||||
|
||||
try err(linux.mmap(
|
||||
@@ -123,7 +125,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
linux.PROT.READ | linux.PROT.WRITE,
|
||||
linux.MAP{ .TYPE = .SHARED, .FIXED = true },
|
||||
@intFromEnum(read_buffer_fd),
|
||||
offset,
|
||||
@intCast(offset),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -162,13 +164,14 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
|
||||
var connection_pool: std.DoublyLinkedList = .{};
|
||||
for (connection_buffer) |*c| {
|
||||
connection_pool.prepend(c.node);
|
||||
connection_pool.prepend(&c.node);
|
||||
}
|
||||
|
||||
return .{
|
||||
.fd = fd,
|
||||
.address = listen_address,
|
||||
.workers = workers,
|
||||
.threads = threads,
|
||||
.request_router = options.request_router,
|
||||
|
||||
.connection_queue = .{},
|
||||
@@ -184,7 +187,9 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Server {
|
||||
pub fn deinit(self: *Server, allocator: std.mem.Allocator) void {
|
||||
// TODO Deinitialize workers
|
||||
self.fd.close();
|
||||
allocator.free(self.threads);
|
||||
allocator.free(self.connection_buffer);
|
||||
allocator.free(self.workers);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
@@ -198,13 +203,13 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
defer {
|
||||
worker_running.store(false, .release);
|
||||
self.cond_connection_queued.broadcast();
|
||||
for (self.workers[0..spawned]) |*worker| {
|
||||
worker.thread.join();
|
||||
for (self.threads[0..spawned]) |*thread| {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
for (self.workers) |*worker| {
|
||||
worker.thread = try std.Thread.spawn(.{}, Worker.worker, .{ worker, self, &worker_running });
|
||||
for (self.workers, 0..) |*worker, i| {
|
||||
self.threads[i] = try std.Thread.spawn(.{}, Worker.worker, .{ worker, self, &worker_running });
|
||||
spawned += 1;
|
||||
}
|
||||
|
||||
@@ -240,7 +245,7 @@ pub fn listen(self: *Server, running: *const std.atomic.Value(bool)) !void {
|
||||
|
||||
fn err(rc: usize) !void {
|
||||
const e = errno(rc);
|
||||
return if (e != .SUCCESS) error.SystemError else rc;
|
||||
return if (e != .SUCCESS) error.SystemError else {};
|
||||
}
|
||||
|
||||
fn errOrPtr(rc: usize) ![*]u8 {
|
||||
|
||||
Reference in New Issue
Block a user