summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/replica.zig23
1 files changed, 20 insertions, 3 deletions
diff --git a/core/replica.zig b/core/replica.zig
index c9a69bdf..01a0b79d 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -835,6 +835,16 @@ pub const Replica = struct {
/// The replica's own input: applied at once when it can be, and the
/// message for the authority returned (valid until the next call).
pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 {
+ const msg = try r.pend(ev);
+ r.applyPended(ev);
+ return msg;
+ }
+
+ /// An input's first half: stamped, numbered and pending, its message
+ /// for the authority returned (valid until the next call), which a
+ /// client sends before it applies it (`applyPended`, Link.input): the
+ /// session starts its step while the client speculates.
+ pub fn pend(r: *Replica, ev: pardes.Event) ![]const u8 {
r.stats.inputs += 1;
const seq = r.next_seq;
r.next_seq += 1;
@@ -853,13 +863,18 @@ pub const Replica = struct {
const msg = try r.gpa.dupe(u8, room[0 .. head_len + stamp_len + n]);
try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs(), .guess = g });
r.dirty = true;
- r.settle();
- if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, seq, r.applied == r.pending.items.len);
r.out.clearRetainingCapacity();
try r.out.appendSlice(r.gpa, msg);
return r.out.items;
}
+ /// An input's second half (`pend`'s, the last): applied at once when it
+ /// can be, its echo predicted.
+ pub fn applyPended(r: *Replica, ev: pardes.Event) void {
+ r.settle();
+ if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, r.next_seq - 1, r.applied == r.pending.items.len);
+ }
+
/// One unit of the authority's stream, in its order.
pub fn confirm(r: *Replica, msg: []const u8) void {
if (r.diverged != null) return;
@@ -3096,8 +3111,10 @@ pub const Link = struct {
// Its notice read: a key of its own puts it away.
if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0;
c.input_ns = c.time();
- const msg = c.r.input(ev) catch return;
+ // Sent first: the session steps it while this replica speculates.
+ const msg = c.r.pend(ev) catch return;
if (!sendInput(c.fd, msg)) c.cut();
+ c.r.applyPended(ev);
c.dirty = true;
}