mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Handle concurrent calls to sendLater
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -43,31 +43,42 @@ fn respCallback(
|
|||||||
std.log.debug("send ok", .{});
|
std.log.debug("send ok", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timeoutCallback(
|
const SendLaterContext = struct {
|
||||||
ctx: *CmdContext,
|
cmd_ctx: *CmdContext,
|
||||||
|
completion: *public.IO.Completion,
|
||||||
|
buf: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn sendLaterCallback(
|
||||||
|
ctx: *SendLaterContext,
|
||||||
completion: *public.IO.Completion,
|
completion: *public.IO.Completion,
|
||||||
result: public.IO.TimeoutError!void,
|
result: public.IO.TimeoutError!void,
|
||||||
) void {
|
) void {
|
||||||
std.log.debug("sending after", .{});
|
std.log.debug("sending after", .{});
|
||||||
_ = result catch |err| {
|
_ = result catch |err| {
|
||||||
ctx.close = true;
|
ctx.cmd_ctx.close = true;
|
||||||
std.debug.print("timeout error: {s}\n", .{@errorName(err)});
|
std.debug.print("timeout error: {s}\n", .{@errorName(err)});
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
ctx.alloc().destroy(completion);
|
ctx.cmd_ctx.alloc().destroy(completion);
|
||||||
send(ctx, ctx.write_buf) catch unreachable;
|
defer ctx.cmd_ctx.alloc().destroy(ctx);
|
||||||
|
send(ctx.cmd_ctx, ctx.buf) catch unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendLater(ctx: *CmdContext, msg: []const u8) !void {
|
pub fn sendLater(ctx: *CmdContext, msg: []const u8) !void {
|
||||||
ctx.write_buf = msg;
|
|
||||||
// NOTE: it seems we can't use the same completion for concurrent
|
// NOTE: it seems we can't use the same completion for concurrent
|
||||||
// recv and timeout operations
|
// recv and timeout operations, that's why we create a new completion here
|
||||||
// TODO: maybe instead of allocating this each time we can create
|
|
||||||
// a timeout_completion on the context?
|
|
||||||
// Not sure if there is several concurrent timeout operations on the same context
|
|
||||||
const completion = try ctx.alloc().create(public.IO.Completion);
|
const completion = try ctx.alloc().create(public.IO.Completion);
|
||||||
ctx.loop().io.timeout(*CmdContext, ctx, timeoutCallback, completion, 1000);
|
// NOTE: to handle concurrent calls to sendLater we create each time a new context
|
||||||
|
// If no concurrent calls are required we could just use the main CmdContext
|
||||||
|
const sendLaterCtx = try ctx.alloc().create(SendLaterContext);
|
||||||
|
sendLaterCtx.* = .{
|
||||||
|
.cmd_ctx = ctx,
|
||||||
|
.completion = completion,
|
||||||
|
.buf = msg,
|
||||||
|
};
|
||||||
|
ctx.loop().io.timeout(*SendLaterContext, sendLaterCtx, sendLaterCallback, completion, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(ctx: *CmdContext, msg: []const u8) !void {
|
fn send(ctx: *CmdContext, msg: []const u8) !void {
|
||||||
|
|||||||
Reference in New Issue
Block a user