aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-08-31 22:37:27 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-08-31 22:37:27 -0700
commita626a07ee86d7b5dd8441002b2c0ba0bf9b68220 (patch)
treebb2832f90b11baff1dfc5caa11426b7c5bb39fe8
parent3e37f5a8eb088c15a6b74373088e0197281ac894 (diff)
downloadbun-a626a07ee86d7b5dd8441002b2c0ba0bf9b68220.tar.gz
bun-a626a07ee86d7b5dd8441002b2c0ba0bf9b68220.tar.zst
bun-a626a07ee86d7b5dd8441002b2c0ba0bf9b68220.zip
wip
-rw-r--r--.vscode/launch.json13
-rw-r--r--src/http_server.zig34
-rw-r--r--src/io/io_linux.zig2
3 files changed, 36 insertions, 13 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 932a71dc4..36f98a9bb 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -99,8 +99,17 @@
{
"type": "lldb",
"request": "launch",
- "name": "fetch debug",
- "program": "${workspaceFolder}/misctools/fetch",
+ "name": "toyhttpserver debug",
+ "program": "${workspaceFolder}/misctools/toyhttpserver",
+ "args": ["https://example.com", "--verbose", "3001"],
+ "cwd": "${workspaceFolder}",
+ "console": "internalConsole"
+ },
+ {
+ "type": "lldb",
+ "request": "launch",
+ "name": "toyhttpserver lite debug",
+ "program": "${workspaceFolder}/misctools/toyhttpserver-lite",
"args": ["https://example.com", "--verbose"],
"cwd": "${workspaceFolder}",
"console": "internalConsole"
diff --git a/src/http_server.zig b/src/http_server.zig
index 2eb86029f..bb815cfd0 100644
--- a/src/http_server.zig
+++ b/src/http_server.zig
@@ -408,26 +408,44 @@ pub const ToyHTTPServer = struct {
_: *Connection,
incoming: IncomingRequest,
) bool {
- this.outgoing_list.load(.Monotonic).appendAssumeCapacity(incoming);
- this.has_scheduled.storeUnchecked(1);
+ {
+ this.lock.lock();
+ defer this.lock.unlock();
+ this.outgoing_list.loadUnchecked().appendAssumeCapacity(incoming);
+ }
+
+ if (this.outgoing_list.loadUnchecked().len > 20)
+ this.waker.wake() catch unreachable;
+
return true;
}
pub fn drain(this: *ToyHTTPServer) void {
var ctx = this.ctx;
+
{
+ this.lock.lock();
+ defer {
+ this.lock.unlock();
+ }
+
if (this.first_is_active) {
this.first_is_active = false;
this.second_list.len = 0;
- this.incoming_list = this.outgoing_list.swap(&this.second_list, .SeqCst);
+
+ this.incoming_list = this.outgoing_list.value;
+ this.outgoing_list.value = &this.second_list;
} else {
this.first_is_active = true;
this.first_list.len = 0;
- this.incoming_list = this.outgoing_list.swap(&this.first_list, .SeqCst);
+
+ this.incoming_list = this.outgoing_list.value;
+ this.outgoing_list.value = &this.first_list;
}
}
- for (this.outgoing_list.load(.Monotonic).slice()) |incoming| {
+ const slice = this.incoming_list.slice();
+ for (slice) |incoming| {
const sent = AsyncIO.darwin.@"sendto"(incoming.fd, hello_world, hello_world.len, 0, null, 0);
if (sent < hello_world.len) {
var socket = uWS.SocketTCP.attach(incoming.fd, ctx) orelse continue;
@@ -536,9 +554,8 @@ pub const ToyHTTPServer = struct {
};
fn scheduleWakeup(this: *ToyHTTPServer) void {
- if (this.has_scheduled.value == 0) return;
+ if (this.has_scheduled.load(.Monotonic) == 0) return;
- this.has_scheduled.value = 0;
this.waker.wake() catch unreachable;
}
pub fn startServer(toy: *ToyHTTPServer) void {
@@ -588,13 +605,10 @@ pub const ToyHTTPServer = struct {
_ = http.loop.addPostHandler(*ToyHTTPServer, http, drain);
var thread = std.Thread.spawn(.{}, startServer, .{http}) catch unreachable;
http.drain();
-
thread.detach();
while (true) {
- http.loop.nextTick(*ToyHTTPServer, http, drain);
_ = http.waker.wait() catch 0;
http.drain();
- http.loop.run();
}
}
};
diff --git a/src/io/io_linux.zig b/src/io/io_linux.zig
index af5382053..ccefa4a1b 100644
--- a/src/io/io_linux.zig
+++ b/src/io/io_linux.zig
@@ -995,7 +995,7 @@ pub const Waker = struct {
pub fn wait(this: Waker) !u64 {
var bytes: usize = 0;
- _ = std.os.read(this.fd, @ptrCast(*[8]u8, &bytes)) catch 0;
+ _ = try std.os.read(this.fd, @ptrCast(*[8]u8, &bytes));
return @intCast(u64, bytes);
}