feat: implement endpoint link and exit handling

This commit is contained in:
CJ van den Berg 2026-03-10 23:01:59 +01:00
parent 3bee4a9e40
commit 46cbd53a24
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 121 additions and 12 deletions

View file

@ -61,10 +61,13 @@ const Endpoint = struct {
}
fn deinit(self: *@This()) void {
// Exit all live proxies (best-effort; endpoint may already be shutting down).
// Shut down all live proxies. Use "endpoint_exit" rather than "exit"
// so the proxy can distinguish this deinit-time message (which has no
// live sender context, i.e. from.instance_id()==0) from a Thespian
// trapped-exit sent by a linked local actor.
var proxy_it = self.proxies.valueIterator();
while (proxy_it.next()) |p| {
p.send(.{ "exit", "transport_closed" }) catch {};
p.send(.{ "endpoint_exit", "transport_closed" }) catch {};
p.deinit();
}
self.proxies.deinit();
@ -87,6 +90,7 @@ const Endpoint = struct {
var to_name: []const u8 = "";
var payload: []const u8 = "";
var remote_id: u64 = 0;
var reason: []const u8 = "";
if (try m.match(.{ proc_tag, "stdout", tp.extract(&bytes) })) {
if (self.accumulator.feed(bytes)) |frame| try self.dispatch_inbound(frame);
@ -99,6 +103,19 @@ const Endpoint = struct {
} else if (try m.match(.{ "send", tp.extract(&from_id), tp.extract(&to_name), cbor.extract_cbor(&payload) })) {
// Outbound send_named from a local actor addressing a well-known remote actor by name.
try self.send_wire_named(from_id, to_name, payload);
} else if (try m.match(.{ "link_wire", tp.extract(&from_id), tp.extract(&to_id) })) {
// A local proxy is establishing a remote link: from_id=local handle pointer,
// to_id=remote_id of the target on the remote system.
const wire_from_id = try self.get_or_assign_outbound_id(from_id);
try self.send_wire_link(wire_from_id, to_id);
} else if (try m.match(.{ "local_link_exit", tp.extract(&from_id), tp.extract(&reason) })) {
// A local actor that a proxy was Thespian-linked to has exited.
// from_id is the actor's handle pointer; look up its wire ID and
// forward an exit wire message so the remote side can propagate it.
const key: usize = @intCast(from_id);
if (self.outbound.get(key)) |wire_id|
try self.send_wire_exit(wire_id, reason);
// If not in the outbound table the actor never sent outbound; ignore.
} else if (try m.match(.{ "proxy_exit", tp.extract(&remote_id), tp.any })) {
// A local proxy has exited; remove it from the proxy table.
if (self.proxies.fetchRemove(remote_id)) |entry| entry.value.deinit();
@ -165,8 +182,17 @@ const Endpoint = struct {
if (self.proxies.get(e.id)) |p|
p.send(.{ "exit", e.reason }) catch {};
},
.link => |lnk| {
// Remote side is establishing a link: lnk.local_id is the remote
// actor's wire ID (= our proxy key), lnk.remote_id is the local
// actor's wire ID in our local_actors table.
const prx = try self.get_or_create_proxy(lnk.local_id);
if (self.local_actors.getPtr(lnk.remote_id)) |actor_ptr|
try prx.send(.{ "set_notify", @as(u64, @intFromPtr(actor_ptr.h)) })
else
return tp.exit_error(error.UnknownLocalActor, null);
},
.transport_error => |te| return tp.exit(te.reason),
.link => return tp.exit_error(error.UnexpectedMessage, null),
}
}
@ -181,6 +207,28 @@ const Endpoint = struct {
try self.proc.send(frame_stream.buffered());
}
fn send_wire_link(self: *@This(), local_id: u64, remote_id: u64) !void {
var msg_buf: [framing.max_frame_size]u8 = undefined;
var msg_stream: std.Io.Writer = .fixed(&msg_buf);
try protocol.encode_link(&msg_stream, local_id, remote_id);
var frame_buf: [framing.max_frame_size + 4]u8 = undefined;
var frame_stream: std.Io.Writer = .fixed(&frame_buf);
try framing.write_frame(&frame_stream, msg_stream.buffered());
try self.proc.send(frame_stream.buffered());
}
fn send_wire_exit(self: *@This(), id: u64, reason: []const u8) !void {
var msg_buf: [framing.max_frame_size]u8 = undefined;
var msg_stream: std.Io.Writer = .fixed(&msg_buf);
try protocol.encode_exit(&msg_stream, id, reason);
var frame_buf: [framing.max_frame_size + 4]u8 = undefined;
var frame_stream: std.Io.Writer = .fixed(&frame_buf);
try framing.write_frame(&frame_stream, msg_stream.buffered());
try self.proc.send(frame_stream.buffered());
}
fn send_wire_named(self: *@This(), from_id: u64, to_name: []const u8, payload: []const u8) !void {
var msg_buf: [framing.max_frame_size]u8 = undefined;
var msg_stream: std.Io.Writer = .fixed(&msg_buf);