summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 7bf0574f..95135307 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -1140,6 +1140,8 @@ pub const Peer = struct {
limit: usize = 64 << 20,
closing: bool = false,
cut: bool = false,
+ /// Its writer has ended (what it was sent, written).
+ done: std.atomic.Value(bool) = .init(false),
/// `parts` queued for it, in order; never waits. A client whose queue
/// passes its limit is cut off: its link is shut down, and its reader
@@ -1169,6 +1171,7 @@ pub const Peer = struct {
fn writeLoop(peer: *Peer) void {
const io = peer.peers.io;
+ defer peer.done.store(true, .release);
while (true) {
peer.lock.lockUncancelable(io);
while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock);
@@ -1457,7 +1460,7 @@ pub const Peers = struct {
var out: [wire.hello_max + 64]u8 = undefined;
// The session's digest as dumped, for the joiner to check its load by.
const digest = tape.digestOf(core);
- const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest } }) catch unreachable;
+ const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable;
peer.send(&.{welcome});
var at: usize = 0;
while (at < bytes.len) : (at += dump_piece) {
@@ -1535,6 +1538,33 @@ pub const Peers = struct {
ps.gpa.destroy(peer);
}
+ /// Every client told `why` and let go, its view with it: an Upgrade's
+ /// exec is next (tty.zig `upgradeNow`). What each was sent goes first,
+ /// for up to 200 ms; a client then dials the session again.
+ pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void {
+ if (ps.count() == 0) return;
+ var out: [600]u8 = undefined;
+ const words = wire.encodeServer(&out, .{ .refused = why }) catch return;
+ const io = ps.io;
+ for (ps.list) |slot| if (slot) |peer| {
+ peer.send(&.{words});
+ peer.lock.lockUncancelable(io);
+ peer.closing = true;
+ peer.cond.signal(io);
+ peer.lock.unlock(io);
+ };
+ const until = nowNs() + 200 * std.time.ns_per_ms;
+ while (nowNs() < until) {
+ const busy = for (ps.list) |slot| {
+ if (slot) |peer| if (!peer.done.load(.acquire)) break true;
+ } else false;
+ if (!busy) break;
+ const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+ }
+ ps.dropAll(core);
+ }
+
/// Detach typed in client view `k`'s: its link ends (its reader says
/// it is gone, and the next boundary lets it go).
pub fn detach(ps: *Peers, k: u8) bool {
@@ -1612,6 +1642,9 @@ pub const Joined = struct {
dump: []u8,
/// The session's digest as dumped (`Replica.loaded`).
digest: u64 = 0,
+ /// Its build's (wire.semantics): the daemon judged the hello by what it
+ /// last heard, which an upgrade under way may not have said yet.
+ semantics: u64 = 0,
/// What was read past the dump: the stream's (`readStream`).
rest: Framer = .{},
};
@@ -1657,6 +1690,7 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J
r.j.view = w.view;
r.j.gmtoff = w.gmtoff;
r.j.digest = w.digest;
+ r.j.semantics = w.semantics;
r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch {
r.failed = "out of memory for the session's dump";
return false;