Skip to content

Commit 20e0a94

Browse files
committed
Updated README Zig Version to 0.15.2
Update README example to 0.15.2 Signed-off-by: Bernard Assan <[email protected]>
1 parent 4b8e71f commit 20e0a94

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

README.md

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tardy is a thread-local, I/O driven runtime for Zig, providing the core implemen
1717
- Coroutines (internally called Frames).
1818

1919
## Installing
20-
Compatible Zig Version: `0.14.0`
20+
Compatible Zig Version: `0.15.2`
2121

2222
Latest Release: `0.3.0`
2323
```
@@ -60,46 +60,51 @@ A basic multi-threaded TCP echo server.
6060

6161
```zig
6262
const std = @import("std");
63-
const log = std.log.scoped(.@"tardy/example/echo");
6463
64+
const AcceptResult = @import("tardy").AcceptResult;
65+
const Cross = @import("tardy").Cross;
6566
const Pool = @import("tardy").Pool;
67+
const RecvResult = @import("tardy").RecvResult;
6668
const Runtime = @import("tardy").Runtime;
67-
const Task = @import("tardy").Task;
68-
const Tardy = @import("tardy").Tardy(.auto);
69-
const Cross = @import("tardy").Cross;
70-
69+
const SendResult = @import("tardy").SendResult;
7170
const Socket = @import("tardy").Socket;
71+
const Task = @import("tardy").Task;
7272
const Timer = @import("tardy").Timer;
7373
74-
const AcceptResult = @import("tardy").AcceptResult;
75-
const RecvResult = @import("tardy").RecvResult;
76-
const SendResult = @import("tardy").SendResult;
74+
const Tardy = @import("tardy").Tardy(.auto);
75+
const log = std.log.scoped(.@"tardy/example/echo");
7776
7877
fn echo_frame(rt: *Runtime, server: *const Socket) !void {
7978
const socket = try server.accept(rt);
8079
defer socket.close_blocking();
8180
82-
// you can use the standard Zig Reader/Writer if you want!
83-
const reader = socket.reader(rt);
84-
const writer = socket.writer(rt);
81+
var sock_reader = socket.reader(rt, &.{});
82+
const sock_r = &sock_reader.interface;
83+
84+
var sock_writer = socket.writer(rt, &.{});
85+
const sock_w = &sock_writer.interface;
86+
defer sock_w.flush() catch unreachable;
8587
8688
log.debug(
87-
"{d} - accepted socket [{}]",
89+
"{d} - accepted socket [{f}]",
8890
.{ std.time.milliTimestamp(), socket.addr },
8991
);
9092
93+
// spawn off a new frame.
9194
try rt.spawn(.{ rt, server }, echo_frame, 1024 * 16);
9295
93-
var buffer: [1024]u8 = undefined;
96+
var buffer: [501]u8 = undefined;
9497
while (true) {
95-
const recv_length = reader.read(&buffer) catch |e| {
96-
log.err("Failed to recv on socket | {}", .{e});
97-
return;
98+
const recv_length = sock_r.readSliceShort(&buffer) catch |e| {
99+
log.err("Failed to recv on socket | {t}", .{e});
100+
break;
98101
};
99102
100-
writer.writeAll(buffer[0..recv_length]) catch |e| {
101-
log.err("Failed to send on socket | {}", .{e});
102-
return;
103+
if (recv_length == 0) return;
104+
105+
sock_w.writeAll(buffer[0..recv_length]) catch |e| {
106+
log.err("Failed to send on socket | {t}", .{e});
107+
break;
103108
};
104109
105110
log.debug("Echoed: {s}", .{buffer[0..recv_length]});
@@ -111,21 +116,26 @@ pub fn main() !void {
111116
const allocator = gpa.allocator();
112117
defer _ = gpa.deinit();
113118
114-
// tardy by default is
115-
// - multithreaded
116-
// - unbounded in terms of spawnable tasks
117-
var tardy: Tardy = try .init(allocator, .{});
119+
var tardy: Tardy = try .init(allocator, .{
120+
.threading = .single,
121+
.pooling = .static,
122+
.size_tasks_initial = 256,
123+
.size_aio_reap_max = 256,
124+
});
118125
defer tardy.deinit();
119126
120-
const server: Socket = try .init(.{ .tcp = .{ .host = "127.0.0.1", .port = 9862 } });
127+
const host = "0.0.0.0";
128+
const port = 9862;
129+
130+
const server: Socket = try .init(.{ .tcp = .{ .host = host, .port = port } });
121131
try server.bind();
122-
try server.listen(256);
132+
try server.listen(1024);
123133
124134
try tardy.entry(
125135
&server,
126136
struct {
127137
fn start(rt: *Runtime, tcp_server: *const Socket) !void {
128-
try rt.spawn(.{ rt, tcp_server }, echo_frame, 1024 * 1024 * 4);
138+
try rt.spawn(.{ rt, tcp_server }, echo_frame, 1024 * 16);
129139
}
130140
}.start,
131141
);

examples/basic/main.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ fn log_frame(rt: *Runtime) !void {
1717
}
1818

1919
pub fn main() !void {
20-
const allocator = std.heap.page_allocator;
20+
var gpa: std.heap.DebugAllocator(.{}) = .init;
21+
const allocator = gpa.allocator();
22+
defer _ = gpa.deinit();
2123

2224
var tardy: Tardy = try .init(allocator, .{
2325
.threading = .single,

examples/echo/main.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,14 @@ fn echo_frame(rt: *Runtime, server: *const Socket) !void {
3232
// spawn off a new frame.
3333
try rt.spawn(.{ rt, server }, echo_frame, 1024 * 16);
3434

35-
//TODO: investigate why using readSliceShort with a buffer bigger
36-
// than reader size leads to a connection reset (try to get a repro)
3735
var buffer: [501]u8 = undefined;
3836
while (true) {
3937
const recv_length = sock_r.readSliceShort(&buffer) catch |e| {
4038
log.err("Failed to recv on socket | {t}", .{e});
4139
break;
4240
};
4341

44-
if (recv_length == 0) {
45-
break;
46-
}
42+
if (recv_length == 0) return;
4743

4844
sock_w.writeAll(buffer[0..recv_length]) catch |e| {
4945
log.err("Failed to send on socket | {t}", .{e});

0 commit comments

Comments
 (0)