Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8994003

Browse files
committedJan 25, 2025
feat: initial frame migration steps
1 parent 28b3e6c commit 8994003

File tree

9 files changed

+227
-1201
lines changed

9 files changed

+227
-1201
lines changed
 

‎build.zig.zon

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
.version = "0.2.0",
44
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
6-
.tardy = .{
7-
.url = "git+https://github.com/mookums/tardy#c2851c1e8ec5c66a16a0fb318d6bacea4ae9cc0b",
8-
.hash = "122054b7c88eca71ab699e7ff530cd56303459356ea9ff3c9c78794943b035cadfea",
9-
},
6+
// .tardy = .{
7+
// .url = "git+https://github.com/mookums/tardy#e4bd6b8f84d888378f786405b10c7d3f16228ce7",
8+
// .hash = "12201f73a4c8c7fcaa7e16b8b1795abc718320a726292284ba4b94a3320b207e2ac8",
9+
// },
10+
.tardy = .{ .path = "../tardy" },
1011
.bearssl = .{
1112
.url = "git+https://github.com/mookums/bearssl-zig#37a96eee56fe2543579bbc6da148ca886f3dd32b",
1213
.hash = "12200e89d16612100a2f145cfa292537ac25b2205735fc1c644c799d2995f94e8e20",

‎examples/basic/main.zig

+24-48
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,13 @@ const http = zzz.HTTP;
77
const tardy = zzz.tardy;
88
const Tardy = tardy.Tardy(.auto);
99
const Runtime = tardy.Runtime;
10+
const Socket = tardy.Socket;
1011

1112
const Server = http.Server;
1213
const Router = http.Router;
1314
const Context = http.Context;
1415
const Route = http.Route;
1516

16-
fn root_handler(ctx: *Context, id: i8) !void {
17-
const body_fmt =
18-
\\ <!DOCTYPE html>
19-
\\ <html>
20-
\\ <body>
21-
\\ <h1>Hello, World!</h1>
22-
\\ <p>id: {d}</p>
23-
\\ </body>
24-
\\ </html>
25-
;
26-
const body = try std.fmt.allocPrint(ctx.allocator, body_fmt, .{id});
27-
// This is the standard response and what you
28-
// will usually be using. This will send to the
29-
// client and then continue to await more requests.
30-
return try ctx.respond(.{
31-
.status = .OK,
32-
.mime = http.Mime.HTML,
33-
.body = body[0..],
34-
});
35-
}
36-
37-
fn echo_handler(ctx: *Context, _: void) !void {
38-
const body = if (ctx.request.body) |b|
39-
try ctx.allocator.dupe(u8, b)
40-
else
41-
"";
42-
return try ctx.respond(.{
43-
.status = .OK,
44-
.mime = http.Mime.HTML,
45-
.body = body[0..],
46-
});
47-
}
48-
4917
pub fn main() !void {
5018
const host: []const u8 = "0.0.0.0";
5119
const port: u16 = 9862;
@@ -56,36 +24,44 @@ pub fn main() !void {
5624

5725
// Creating our Tardy instance that
5826
// will spawn our runtimes.
59-
var t = try Tardy.init(.{
60-
.allocator = allocator,
61-
.threading = .single,
27+
var t = try Tardy.init(allocator, .{
28+
.threading = .auto,
29+
.size_tasks_initial = 1024,
30+
.size_aio_reap_max = 1024,
6231
});
6332
defer t.deinit();
6433

65-
const num: i8 = 12;
34+
//var router = try Router.init(allocator, &.{}, .{});
35+
//defer router.deinit(allocator);
36+
//router.print_route_tree();
37+
38+
// create socket for tardy
39+
var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
40+
defer socket.close_blocking();
41+
try socket.bind();
42+
try socket.listen(4096);
43+
44+
const EntryParams = struct {
45+
router: *const Router,
46+
socket: *Socket,
47+
};
6648

67-
var router = try Router.init(allocator, &.{
68-
Route.init("/").get(num, root_handler).layer(),
69-
Route.init("/echo").post({}, echo_handler).layer(),
70-
}, .{});
71-
defer router.deinit(allocator);
72-
router.print_route_tree();
49+
const params: EntryParams = .{ .router = undefined, .socket = &socket };
7350

7451
// This provides the entry function into the Tardy runtime. This will run
7552
// exactly once inside of each runtime (each thread gets a single runtime).
7653
try t.entry(
77-
&router,
54+
&params,
7855
struct {
79-
fn entry(rt: *Runtime, r: *const Router) !void {
56+
fn entry(rt: *Runtime, p: *const EntryParams) !void {
8057
var server = Server.init(rt.allocator, .{});
81-
try server.bind(.{ .ip = .{ .host = host, .port = port } });
82-
try server.serve(r, rt);
58+
try server.serve(rt, p.socket);
8359
}
8460
}.entry,
8561
{},
8662
struct {
8763
fn exit(rt: *Runtime, _: void) !void {
88-
try Server.clean(rt);
64+
_ = rt;
8965
}
9066
}.exit,
9167
);

‎src/core/case_string_map.zig

+5-11
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,19 @@ pub fn CaseStringMap(comptime T: type) type {
1818
pool: InnerPool,
1919

2020
pub fn init(allocator: std.mem.Allocator, size: usize) !Self {
21-
const pool = try Pool(Entry).init(allocator, size, null, null);
21+
const pool = try Pool(Entry).init(allocator, size, .grow);
2222
return .{ .pool = pool };
2323
}
2424

2525
pub fn deinit(self: *Self) void {
26-
self.pool.deinit(null, null);
26+
self.pool.deinit();
2727
}
2828

2929
pub fn put(self: *Self, name: []const u8, data: T) !void {
3030
const name_hash = hash(name);
31-
const entry = try self.pool.borrow_hint(@intCast(name_hash));
32-
entry.item.* = .{ .key = name, .hash = name_hash, .data = data };
33-
}
34-
35-
pub fn put_assume_capacity(self: *Self, name: []const u8, data: T) void {
36-
assert(self.pool.clean() > 0);
37-
const name_hash = hash(name);
38-
const entry = self.pool.borrow_hint(@intCast(name_hash)) catch unreachable;
39-
entry.item.* = .{ .key = name, .hash = name_hash, .data = data };
31+
const index = try self.pool.borrow_hint(@intCast(name_hash));
32+
const entry = self.pool.get_ptr(index);
33+
entry.* = .{ .key = name, .hash = name_hash, .data = data };
4034
}
4135

4236
pub fn get(self: *const Self, name: []const u8) ?T {

‎src/core/lib.zig

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
pub const Job = @import("job.zig").Job;
2-
pub const ZeroCopyBuffer = @import("zc_buffer.zig").ZeroCopyBuffer;
32
pub const Pseudoslice = @import("pseudoslice.zig").Pseudoslice;

‎src/core/zc_buffer.zig

-164
This file was deleted.

‎src/http/provision.zig

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const std = @import("std");
22

3+
const ZeroCopy = @import("tardy").ZeroCopy;
4+
35
const Job = @import("../core/job.zig").Job;
4-
const ZeroCopyBuffer = @import("../core/zc_buffer.zig").ZeroCopyBuffer;
56
const Capture = @import("router/routing_trie.zig").Capture;
67
const QueryMap = @import("router/routing_trie.zig").QueryMap;
78
const Request = @import("request.zig").Request;
@@ -17,18 +18,15 @@ pub const Stage = union(enum) {
1718
};
1819

1920
pub const Provision = struct {
21+
initalized: bool = false,
2022
index: usize,
21-
job: Job,
22-
socket: std.posix.socket_t,
23+
recv_buffer: ZeroCopy(u8),
2324
buffer: []u8,
24-
recv_buffer: ZeroCopyBuffer,
2525
arena: std.heap.ArenaAllocator,
2626
captures: []Capture,
2727
queries: QueryMap,
2828
request: Request,
2929
response: Response,
30-
stage: Stage,
31-
context: Context,
3230

3331
pub const InitContext = struct {
3432
allocator: std.mem.Allocator,
@@ -40,17 +38,13 @@ pub const Provision = struct {
4038
const config = ctx.config;
4139
for (provisions) |*provision| {
4240
provision.job = .empty;
43-
provision.socket = undefined;
4441
// Create Recv Buffer
45-
provision.recv_buffer = ZeroCopyBuffer.init(ctx.allocator, config.socket_buffer_bytes) catch {
42+
provision.recv_buffer = ZeroCopy(u8).init(ctx.allocator, config.socket_buffer_bytes) catch {
4643
@panic("attempting to statically allocate more memory than available. (ZeroCopyBuffer)");
4744
};
48-
// Create Buffer
49-
provision.buffer = provision.recv_buffer.get_write_area_assume_space(config.socket_buffer_bytes);
5045
// Create the Context Arena
5146
provision.arena = std.heap.ArenaAllocator.init(ctx.allocator);
5247

53-
provision.stage = .header;
5448
provision.captures = ctx.allocator.alloc(Capture, config.capture_count_max) catch {
5549
@panic("attempting to statically allocate more memory than available. (Captures)");
5650
};
@@ -63,26 +57,6 @@ pub const Provision = struct {
6357
provision.response = Response.init(ctx.allocator, config.header_count_max) catch {
6458
@panic("attempting to statically allocate more memory than available. (Response)");
6559
};
66-
67-
// Anything undefined within here is sourced at Handler time.
68-
provision.context = .{
69-
.provision = provision,
70-
.allocator = provision.arena.allocator(),
71-
.request = &provision.request,
72-
.response = &provision.response,
73-
.runtime = ctx.runtime,
74-
.next = .{
75-
.ctx = &provision.context,
76-
.stage = .pre,
77-
// Handler Time.
78-
.pre_chain = undefined,
79-
.post_chain = undefined,
80-
},
81-
82-
// Handler Time.
83-
.captures = undefined,
84-
.queries = undefined,
85-
};
8660
}
8761
}
8862

There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.