Skip to content

Commit ef58fe4

Browse files
committed
Update num_threads and fixed headers
1 parent d4eab64 commit ef58fe4

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

frameworks/Zig/zinc/build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.dependencies = .{
55
.zinc = .{
66
.url = "https://github.com/zon-dev/zinc/archive/refs/heads/main.zip",
7-
.hash = "1220bbd81a53f8268d970dfbf92268c36a25b50978dd8e7dcbf551e69b2c03677e33",
7+
.hash = "12201f5d2138d81c9aa85bf18a550df6329d98f02645bc5f9e1eb82b2ed98d211bb6",
88
},
99
.@"zig-datetime" = .{
1010
.url = "https://github.com/frmdstryr/zig-datetime/archive/refs/heads/master.zip",

frameworks/Zig/zinc/src/main.zig

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ const zinc = @import("zinc");
33
const Datetime = @import("datetime").datetime.Datetime;
44

55
pub fn main() !void {
6-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7-
const allocator = gpa.allocator();
8-
9-
var z = try zinc.init(.{ .port = 8080, .allocator = allocator, .num_threads = 255 });
6+
var gpa = std.heap.GeneralPurposeAllocator(.{
7+
.thread_safe = true,
8+
}){};
9+
var tsa = std.heap.ThreadSafeAllocator{
10+
.child_allocator = gpa.allocator(),
11+
};
12+
const allocator = tsa.allocator();
13+
14+
const cpuCount = @as(u8, @intCast(std.Thread.getCpuCount() catch 1));
15+
16+
var z = try zinc.init(.{
17+
.port = 8080,
18+
.allocator = allocator,
19+
.num_threads = 16 * cpuCount,
20+
});
1021

1122
var router = z.getRouter();
1223
try router.use(&.{setupHeader});
1324

1425
try router.get("/json", json);
1526
try router.get("/plaintext", plaintext);
1627

17-
try z.run();
28+
z.run() catch |err| std.debug.print("Error: {any}\n", .{err});
1829
}
1930

2031
fn plaintext(ctx: *zinc.Context) anyerror!void {
@@ -27,12 +38,10 @@ fn json(ctx: *zinc.Context) anyerror!void {
2738

2839
fn setupHeader(ctx: *zinc.Context) anyerror!void {
2940
try ctx.setHeader("Server", "Zinc");
30-
// try ctx.setHeader("date", "Sun Sep 22 10:01:11 CEST 2024");
31-
// In UTC
41+
try ctx.setHeader("Connection", "keep-alive");
42+
3243
const now = Datetime.now();
3344
const now_str = try now.formatHttp(ctx.allocator);
34-
// defer ctx.allocator.free(now_str);
35-
// std.debug.warn("The time is now: {}\n", .{now_str});
3645
// The time is now: Fri, 20 Dec 2019 22:03:02 UTC
3746
try ctx.setHeader("date", now_str);
3847
}

0 commit comments

Comments
 (0)