-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_basic_5_uring.zig
More file actions
74 lines (62 loc) · 2.52 KB
/
Copy pathhttp_basic_5_uring.zig
File metadata and controls
74 lines (62 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const std = @import("std");
const zix = @import("zix");
const IP: []const u8 = "127.0.0.1";
const PORT: u16 = 9004;
const DISPATCH_MODEL: zix.Tcp.DispatchModel = .URING;
const KERNEL_BACKLOG: usize = 1024 * 4;
const MAX_RECV_BUF: usize = 1024 * 4;
const MAX_ALLOCATOR_SIZE: usize = 1024 * 4;
const WORKERS: usize = 0; // 0 = auto (cpu_count ring workers). Used by .URING as the worker count.
const POOL_SIZE: usize = 0; // 0 = auto. Not used by .URING.
// --------------------------------------------------------- //
// Note:
// .URING is Linux-only (ADR-037). Shared-nothing: each worker owns one
// SO_REUSEPORT listener and one io_uring ring. The kernel distributes new
// connections across workers with no shared queue. Each readable batch recvs into
// the connection buffer, runs one request, and submits one coalesced send.
// One request per buffer (no pipelined drain), matching the .EPOLL path. On other
// platforms the server falls back to .POOL.
// --------------------------------------------------------- //
// curl usage: curl -X GET "http://localhost:9004/"
pub fn homeHandler(req: *zix.Http.Request, res: *zix.Http.Response, ctx: *zix.Http.Context) !void {
_ = req;
_ = ctx;
try res.send("Hello, World!");
}
// curl usage: curl -X GET "http://localhost:9004/echo"
pub fn echoHandler(req: *zix.Http.Request, res: *zix.Http.Response, ctx: *zix.Http.Context) !void {
_ = req;
_ = ctx;
res.setContentType(.APPLICATION_JSON);
res.setKeepAlive(true);
try res.send("{\"status\":\"ok\"}");
}
// curl usage: curl -X GET "http://localhost:9004/about"
pub fn aboutHandler(req: *zix.Http.Request, res: *zix.Http.Response, ctx: *zix.Http.Context) !void {
_ = req;
_ = ctx;
try res.send("zix basic server example");
}
// --------------------------------------------------------- //
const Routes = [_]zix.Http.Route{
.{ .path = "/", .handler = homeHandler },
.{ .path = "/echo", .handler = echoHandler },
.{ .path = "/about", .handler = aboutHandler },
};
pub fn main(process: std.process.Init) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
defer arena.deinit();
var server = zix.Http.Server.init(&Routes, .{
.io = process.io,
.ip = IP,
.port = PORT,
.dispatch_model = DISPATCH_MODEL,
.kernel_backlog = KERNEL_BACKLOG,
.max_recv_buf = MAX_RECV_BUF,
.max_allocator_size = MAX_ALLOCATOR_SIZE,
.workers = WORKERS,
.pool_size = POOL_SIZE,
});
defer server.deinit();
try server.run();
}