mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-04-04 00:20:32 +00:00
refactor: move timeout and busy logic to Runner
This commit is contained in:
@@ -297,13 +297,12 @@ pub const Client = struct {
|
||||
}
|
||||
|
||||
var cdp = &self.mode.cdp;
|
||||
var last_message = milliTimestamp(.monotonic);
|
||||
var ms_remaining = self.ws.timeout_ms;
|
||||
const timeout_ms = self.ws.timeout_ms;
|
||||
|
||||
while (true) {
|
||||
const result = cdp.pageWait(ms_remaining) catch |wait_err| switch (wait_err) {
|
||||
const result = cdp.pageWait(timeout_ms) catch |wait_err| switch (wait_err) {
|
||||
error.NoPage => {
|
||||
const status = http.tick(ms_remaining) catch |err| {
|
||||
const status = http.tick(timeout_ms) catch |err| {
|
||||
log.err(.app, "http tick", .{ .err = err });
|
||||
return;
|
||||
};
|
||||
@@ -314,10 +313,12 @@ pub const Client = struct {
|
||||
if (self.readSocket() == false) {
|
||||
return;
|
||||
}
|
||||
last_message = milliTimestamp(.monotonic);
|
||||
ms_remaining = self.ws.timeout_ms;
|
||||
continue;
|
||||
},
|
||||
error.Timeout => {
|
||||
log.info(.app, "CDP timeout", .{});
|
||||
return;
|
||||
},
|
||||
else => return wait_err,
|
||||
};
|
||||
|
||||
@@ -326,46 +327,12 @@ pub const Client = struct {
|
||||
if (self.readSocket() == false) {
|
||||
return;
|
||||
}
|
||||
last_message = milliTimestamp(.monotonic);
|
||||
ms_remaining = self.ws.timeout_ms;
|
||||
},
|
||||
.done => {
|
||||
if (self.isBusy()) {
|
||||
last_message = milliTimestamp(.monotonic);
|
||||
ms_remaining = self.ws.timeout_ms;
|
||||
continue;
|
||||
}
|
||||
const now = milliTimestamp(.monotonic);
|
||||
const elapsed = now - last_message;
|
||||
if (elapsed >= ms_remaining) {
|
||||
log.info(.app, "CDP timeout", .{});
|
||||
return;
|
||||
}
|
||||
ms_remaining -= @intCast(elapsed);
|
||||
last_message = now;
|
||||
},
|
||||
.done => unreachable,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn isBusy(self: *const Client) bool {
|
||||
if (self.http.active > 0 or self.http.intercepted > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const cdp = switch (self.mode) {
|
||||
.cdp => |*c| c,
|
||||
.http => return false,
|
||||
};
|
||||
|
||||
const session = cdp.browser.session orelse return false;
|
||||
if (session.browser.hasBackgroundTasks() or session.browser.msToNextMacrotask() != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn blockingReadStart(ctx: *anyopaque) bool {
|
||||
const self: *Client = @ptrCast(@alignCast(ctx));
|
||||
self.ws.setBlocking(true) catch |err| {
|
||||
|
||||
Reference in New Issue
Block a user