frmdstryr / zhp

A Http server written in Zig
MIT License
355 stars 24 forks source link

Consumes 100% CPU when idle #14

Open atombender opened 3 years ago

atombender commented 3 years ago

I compiled and ran the example from the readme, and while it works, it consumes a constant 100% CPU when idle. The readme example doesn't compile due to syntax errors, by the way; here's the fixed version:

const std = @import("std");
const web = @import("zhp");

pub const io_mode = .evented;
pub const log_level = .info;

const MainHandler = struct {
    pub fn get(self: *MainHandler, request: *web.Request, response: *web.Response) !void {
        try response.headers.put("Content-Type", "text/plain");
        _ = try response.stream.write("Hello, World!");
    }
};

pub const routes = [_]web.Route{
    web.Route.create("home", "/", MainHandler),
};

pub const middleware = [_]web.Middleware{
    web.Middleware.create(web.middleware.LoggingMiddleware),
};

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(!gpa.deinit());
    const allocator = &gpa.allocator;

    var app = web.Application.init(allocator, .{.debug=true});
    defer app.deinit();

    try app.listen("127.0.0.1", 9000);
    try app.start();
}

Zig 0.8.0, macOS 11.2.2.

haze commented 2 years ago

@atombender could you retry running this? @kprotty submitted a fix to the zig event loop that should lower the cpu usage on macos