diff options
| -rw-r--r-- | src/client.zig | 6 | ||||
| -rw-r--r-- | src/serve.zig | 44 |
2 files changed, 45 insertions, 5 deletions
diff --git a/src/client.zig b/src/client.zig index 307cd7f..2ae6df6 100644 --- a/src/client.zig +++ b/src/client.zig @@ -351,6 +351,12 @@ pub fn ClientWith(comptime tag_count: usize) type { fn consume(c: *@This(), got: Decoded) ?Done { if (isT(got.msg.msgType())) return c.die(); if (got.msg == .rversion) return c.version(got); + // A server may refuse the Tversion itself (all its slots taken): + // the call fails with its words, and nothing more is sent. + if (got.msg == .rerror and c.versioning and got.tag == notag) { + c.versioning = false; + return .{ .tag = notag, .op = .version, .result = .{ .fail = got.msg.rerror.ename } }; + } if (c.versioning or c.msize == 0) return c.die(); if (got.tag >= c.tags.len) return c.die(); const slot = &c.tags[got.tag]; diff --git a/src/serve.zig b/src/serve.zig index d70ebcf..5166b5f 100644 --- a/src/serve.zig +++ b/src/serve.zig @@ -60,6 +60,9 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits /// The connection hung up; the releases it owed have gone through /// `serve`, and the slot is free once this returns. closed: ?*const fn (ctx: ?*anyopaque, conn: *Conn) void = null, + /// A client was turned away because every slot was taken (it + /// got an Rerror "too many connections"). On the accepting task. + refused: ?*const fn (ctx: ?*anyopaque) void = null, }; pub const InitOptions = struct { @@ -470,7 +473,8 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits }, }; const c = r.take() orelse { - stream.close(io); + refuseFull(io, stream); + if (r.handler.refused) |f| f(r.handler.ctx); continue; }; c.start(stream); @@ -481,6 +485,33 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits } } + /// Every slot is taken: say so rather than hang up without a word. + /// An Rerror under NOTAG answers the Tversion a client opens with. + fn refuseFull(io: Io, stream: Io.net.Stream) void { + const ename = "too many connections"; + var msg: [9 + ename.len]u8 = undefined; + std.mem.writeInt(u32, msg[0..4], msg.len, .little); + msg[4] = 107; // Rerror + std.mem.writeInt(u16, msg[5..7], 0xFFFF, .little); + std.mem.writeInt(u16, msg[7..9], ename.len, .little); + @memcpy(msg[9..], ename); + var buf: [64]u8 = undefined; + var w = stream.writer(io, &buf); + w.interface.writeAll(&msg) catch {}; + w.interface.flush() catch {}; + // Read what the client already sent (its Tversion) before the + // close: over TCP a close with unread data resets, and the reset + // can take the Rerror with it. What arrives later still can. + stream.shutdown(io, .send) catch {}; + var fds = [_]std.posix.pollfd{.{ .fd = stream.socket.handle, .events = std.posix.POLL.IN, .revents = 0 }}; + var sink: [512]u8 = undefined; + while ((std.posix.poll(&fds, 0) catch 0) != 0 and fds[0].revents & std.posix.POLL.IN != 0) { + const n = std.posix.read(stream.socket.handle, &sink) catch break; + if (n == 0) break; + } + stream.close(io); + } + fn take(r: *Self) ?*Conn { r.slots.lockUncancelable(r.io); defer r.slots.unlock(r.io); @@ -953,7 +984,7 @@ test "serve: two clients at once, a third is refused, and stop() hangs up" { try testing.expectEqual(@as(u32, 16), rig.stub.releases); try testing.expectEqual(@as(u32, 2), rig.stub.closed); - // Two slots: the third connection is closed without a word. + // Two slots: the third connection is told why, and closed. var a: TestClient = .{ .io = undefined, .stream = undefined }; try a.open(testing.io, .{ .unix = rig.unix }); defer a.close(); @@ -966,6 +997,8 @@ test "serve: two clients at once, a third is refused, and stop() hangs up" { var c: TestClient = .{ .io = undefined, .stream = undefined }; try c.open(testing.io, .{ .unix = rig.unix }); defer c.close(); + const refused = try c.one(.{ .version = .{} }); + try testing.expectEqualStrings("too many connections", refused.result.fail); try expectHangup(c.one(.{ .version = .{} })); try rig.settle(2); // a holds an open fid: stop() pays its release and ends the connection. @@ -1248,7 +1281,7 @@ fn countClosedBig(ctx: ?*anyopaque, conn: *BigRunner.Conn) void { st.closed += 1; } -test "serve: sixteen connections fill the table, the seventeenth is closed, stop ends all" { +test "serve: sixteen connections fill the table, the seventeenth is refused, stop ends all" { const io = testing.io; var dir = testing.tmpDir(.{}); defer dir.cleanup(); @@ -1271,11 +1304,12 @@ test "serve: sixteen connections fill the table, the seventeenth is closed, stop defer for (&clients) |*tc| tc.close(); try waitCount(&runner, 16); - // The seventeenth connection is accepted and closed at once: it never - // gets a version reply, and the sixteen keep their slots. + // The seventeenth connection is told there is no room and closed: its + // version gets an Rerror, and the sixteen keep their slots. var extra: TestClient = .{ .io = undefined, .stream = undefined }; try extra.open(io, .{ .unix = unix }); defer extra.close(); + try testing.expectEqualStrings("too many connections", (try extra.one(.{ .version = .{} })).result.fail); try expectHangup(extra.one(.{ .version = .{} })); try waitCount(&runner, 16); |
