summaryrefslogtreecommitdiff
path: root/src/serve.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/serve.zig')
-rw-r--r--src/serve.zig175
1 files changed, 174 insertions, 1 deletions
diff --git a/src/serve.zig b/src/serve.zig
index 0311f3d..d70ebcf 100644
--- a/src/serve.zig
+++ b/src/serve.zig
@@ -32,7 +32,9 @@ pub const Limits = struct {
/// `Conn.reply()`, at once or later from any task or thread. A backend that
/// answers `Status.again` is asked again when someone calls `Conn.wake()`
/// (or `wakeAll()`), which is the runner's only spontaneous retry; new
-/// input, replies and closing do not retry parked requests.
+/// input, replies and closing do not retry parked requests. Or it answers
+/// `.again` through `Conn.hold()` and keeps the ticket, and answers that
+/// request itself through `Conn.answerWith()` when it has something.
pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits: Limits) type {
if (limits.msize < fs.msize_min) @compileError("9P runner msize must hold one full Rwalk");
if (limits.connections == 0 or limits.connections > 255) @compileError("9P runner needs 1..255 connection slots");
@@ -147,6 +149,55 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits
c.raise(output);
}
+ /// Answers a read or write `.again` and hands back the ticket
+ /// of the park, to answer it through `answerWith()` when the backend
+ /// has something (lib9p's kept Req); null when it could not
+ /// park, the engine having refused it instead.
+ pub fn hold(c: *Conn, r: *const Backend.Reply) ?fs.Ticket {
+ std.debug.assert(r.status == .again);
+ c.lock();
+ c.engine.reply(r, "");
+ const t = c.engine.ticket(r.tag);
+ c.unlock();
+ c.raise(output);
+ return t;
+ }
+
+ /// Whether the request a ticket names still waits: not flushed,
+ /// its fid not clunked, the connection not hung up, and not out
+ /// being asked again by a retry (whose asking answers it).
+ pub fn waiting(c: *Conn, t: fs.Ticket) bool {
+ c.lock();
+ defer c.unlock();
+ return c.engine.waiting(t);
+ }
+
+ /// A reply and the bytes that travel beside it.
+ pub const Answer = struct { reply: Backend.Reply, bytes: []const u8 = "" };
+
+ /// Answers the request a ticket names, from any task or thread,
+ /// with what `produce(ctx)` makes, which is asked only while
+ /// that request still waits and under the same hold of the
+ /// engine: an answer that costs something to make (a record
+ /// taken off a queue) is never spent on a request flushed
+ /// meanwhile. `produce` answers null when it has nothing yet.
+ /// Answers whether the request still waits afterwards.
+ pub fn answerWith(c: *Conn, t: fs.Ticket, ctx: anytype, comptime produce: fn (@TypeOf(ctx)) ?Answer) bool {
+ c.lock();
+ if (!c.engine.waiting(t)) {
+ c.unlock();
+ return false;
+ }
+ const made = produce(ctx) orelse {
+ c.unlock();
+ return true;
+ };
+ _ = c.engine.answer(t, &made.reply, made.bytes);
+ c.unlock();
+ c.raise(output);
+ return false;
+ }
+
/// Sends whatever the engine has produced; for a caller that
/// drove the engine itself under `lock()`.
pub fn flush(c: *Conn) void {
@@ -320,6 +371,9 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits
c.signal = .unset;
c.room = .unset;
c.used = .init(false);
+ // Counts on across the slot's connections (`Engine.initIn`),
+ // so a ticket of one never names a park of the next.
+ c.engine.seq = 0;
}
}
@@ -752,6 +806,125 @@ test "serve: a parked read completes when another task posts and wakes" {
_ = try tc.one(.{ .clunk = .{ .fid = 1 } });
}
+/// Answers at once, but holds a read that parks, keeping its ticket.
+const Holder = struct {
+ stub: *Stub,
+ conn: ?*TestRunner.Conn = null,
+ ticket: ?fs.Ticket = null,
+
+ fn serve(ctx: ?*anyopaque, conn: *TestRunner.Conn, req: fs.Req) void {
+ const hd: *Holder = @ptrCast(@alignCast(ctx.?));
+ const a = hd.stub.handle(req);
+ if (a.reply.status != .again) return conn.reply(&a.reply, a.bytes);
+ const t = conn.hold(&a.reply);
+ hd.stub.mutex.lockUncancelable(testing.io);
+ defer hd.stub.mutex.unlock(testing.io);
+ hd.conn = conn;
+ hd.ticket = t;
+ }
+
+ fn held(hd: *Holder) !struct { *TestRunner.Conn, fs.Ticket } {
+ var tries: usize = 0;
+ while (tries < 2000) : (tries += 1) {
+ hd.stub.mutex.lockUncancelable(testing.io);
+ const got = if (hd.ticket) |t| .{ hd.conn.?, t } else null;
+ hd.ticket = null;
+ hd.stub.mutex.unlock(testing.io);
+ if (got) |g| return g;
+ try testing.io.sleep(.fromMilliseconds(1), .awake);
+ }
+ return error.Timeout;
+ }
+};
+
+/// Makes an answer, counting the times it was asked to.
+const Producer = struct {
+ text: []const u8,
+ made: u32 = 0,
+
+ fn make(pr: *Producer) ?TestRunner.Conn.Answer {
+ pr.made += 1;
+ return .{ .reply = .{ .tag = 0 }, .bytes = pr.text };
+ }
+};
+
+test "serve: a held read is answered through its ticket, and a flushed one is not" {
+ var rig: Rig = .{ .dir = undefined };
+ var holder: Holder = .{ .stub = &rig.stub };
+ try rig.start(.{ .serve = Holder.serve }, 0);
+ defer rig.end();
+ rig.runner.handler.ctx = &holder;
+ var tc: TestClient = .{ .io = undefined, .stream = undefined };
+ try tc.open(testing.io, .{ .unix = rig.unix });
+ defer tc.close();
+ try tc.handshake();
+ _ = try tc.one(.{ .walk = .{ .fid = 0, .newfid = 1, .names = &.{"event"} } });
+ _ = try tc.one(.{ .open = .{ .fid = 1, .mode = c9.oread } });
+
+ // Flushed, the ticket answers nothing and its answer is never made, and
+ // the tag's next read is not answered with what was meant for the old.
+ const flushed = try tc.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 64 } });
+ const old_conn, const old = try holder.held();
+ _ = try tc.submit(.{ .flush = .{ .oldtag = flushed } });
+ try testing.expectEqualStrings(fs.e_interrupted, (try tc.reap()).result.fail);
+ try testing.expectEqual(Client.Op.flush, (try tc.reap()).op);
+ const again = try tc.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 64 } });
+ try testing.expectEqual(flushed, again);
+ const conn, const t = try holder.held();
+ var misrouted: Producer = .{ .text = "misrouted\n" };
+ try testing.expect(!old_conn.waiting(old));
+ try testing.expect(!old_conn.answerWith(old, &misrouted, Producer.make));
+ try testing.expectEqual(@as(u32, 0), misrouted.made);
+
+ // Held, the read is answered from here, with no retry asked for.
+ try testing.expect(conn.waiting(t));
+ var record: Producer = .{ .text = "record 1\n" };
+ try testing.expect(!conn.answerWith(t, &record, Producer.make));
+ const done = try tc.reap();
+ try testing.expectEqual(again, done.tag);
+ try testing.expectEqualStrings("record 1\n", done.result.read);
+ try testing.expect(!conn.answerWith(t, &record, Producer.make));
+ try testing.expectEqual(@as(u32, 1), record.made);
+ _ = try tc.one(.{ .clunk = .{ .fid = 1 } });
+}
+
+test "serve: a ticket of a hung-up client never names a park of the next one in its slot" {
+ var rig: Rig = .{ .dir = undefined };
+ var holder: Holder = .{ .stub = &rig.stub };
+ try rig.start(.{ .serve = Holder.serve }, 0);
+ defer rig.end();
+ rig.runner.handler.ctx = &holder;
+ var stale: struct { *TestRunner.Conn, fs.Ticket } = undefined;
+ for (0..2) |round| {
+ var tc: TestClient = .{ .io = undefined, .stream = undefined };
+ try tc.open(testing.io, .{ .unix = rig.unix });
+ defer tc.close();
+ try tc.handshake();
+ _ = try tc.one(.{ .walk = .{ .fid = 0, .newfid = 1, .names = &.{"event"} } });
+ _ = try tc.one(.{ .open = .{ .fid = 1, .mode = c9.oread } });
+ _ = try tc.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 64 } });
+ const held = try holder.held();
+ if (round == 0) {
+ stale = held;
+ continue; // hangs up with the read held
+ }
+ // The same slot and, request for request, the same park: only the
+ // sequence number, which counts on across connections, tells them
+ // apart.
+ try testing.expectEqual(stale[0], held[0]);
+ try testing.expectEqual(stale[1].slot, held[1].slot);
+ var wrong: Producer = .{ .text = "for the last client\n" };
+ try testing.expect(!held[0].answerWith(stale[1], &wrong, Producer.make));
+ try testing.expectEqual(@as(u32, 0), wrong.made);
+ try testing.expect(held[0].waiting(held[1]));
+ // Clunked, the held read is interrupted first.
+ _ = try tc.submit(.{ .clunk = .{ .fid = 1 } });
+ try testing.expectEqualStrings(fs.e_interrupted, (try tc.reap()).result.fail);
+ try testing.expectEqual(Client.Op.clunk, (try tc.reap()).op);
+ try testing.expect(!held[0].waiting(held[1]));
+ }
+}
+
test "serve: two clients at once, a third is refused, and stop() hangs up" {
var rig: Rig = .{ .dir = undefined };
try rig.start(.{ .serve = serveNow, .closed = countClosed }, 0);