Skip to content

Commit b5ffd8d

Browse files
authored
Merge pull request #1024 from lightpanda-io/run_distant_tasks
Ability to run tasks even in the "distant" future.
2 parents bc82023 + 21e354d commit b5ffd8d

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/browser/Scheduler.zig

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,41 @@ const Allocator = std.mem.Allocator;
2222

2323
const Scheduler = @This();
2424

25-
primary: Queue,
25+
high_priority: Queue,
2626

2727
// For repeating tasks. We only want to run these if there are other things to
2828
// do. We don't, for example, want a window.setInterval or the page.runMicrotasks
2929
// to block the page.wait.
30-
secondary: Queue,
30+
low_priority: Queue,
3131

32-
// we expect allocator to be the page arena, hence we never call primary.deinit
32+
// we expect allocator to be the page arena, hence we never call high_priority.deinit
3333
pub fn init(allocator: Allocator) Scheduler {
3434
return .{
35-
.primary = Queue.init(allocator, {}),
36-
.secondary = Queue.init(allocator, {}),
35+
.high_priority = Queue.init(allocator, {}),
36+
.low_priority = Queue.init(allocator, {}),
3737
};
3838
}
3939

4040
pub fn reset(self: *Scheduler) void {
41-
self.primary.clearRetainingCapacity();
42-
self.secondary.clearRetainingCapacity();
41+
self.high_priority.clearRetainingCapacity();
42+
self.low_priority.clearRetainingCapacity();
4343
}
4444

4545
const AddOpts = struct {
4646
name: []const u8 = "",
4747
low_priority: bool = false,
4848
};
4949
pub fn add(self: *Scheduler, ctx: *anyopaque, func: Task.Func, ms: u32, opts: AddOpts) !void {
50+
var low_priority = opts.low_priority;
5051
if (ms > 5_000) {
51-
log.warn(.user_script, "long timeout ignored", .{ .delay = ms });
52-
// ignore any task that we're almost certainly never going to run
53-
return;
52+
// we don't want tasks in the far future to block page.wait from
53+
// completing. However, if page.wait is called multiple times (maybe
54+
// a CDP driver is wait for something to happen), then we do want
55+
// to [eventually] run these when their time is up.
56+
low_priority = true;
5457
}
5558

56-
var q = if (opts.low_priority) &self.secondary else &self.primary;
59+
var q = if (low_priority) &self.low_priority else &self.high_priority;
5760
return q.add(.{
5861
.ms = std.time.milliTimestamp() + ms,
5962
.ctx = ctx,
@@ -63,11 +66,11 @@ pub fn add(self: *Scheduler, ctx: *anyopaque, func: Task.Func, ms: u32, opts: Ad
6366
}
6467

6568
pub fn runHighPriority(self: *Scheduler) !?i32 {
66-
return self.runQueue(&self.primary);
69+
return self.runQueue(&self.high_priority);
6770
}
6871

6972
pub fn runLowPriority(self: *Scheduler) !?i32 {
70-
return self.runQueue(&self.secondary);
73+
return self.runQueue(&self.low_priority);
7174
}
7275

7376
fn runQueue(self: *Scheduler, queue: *Queue) !?i32 {
@@ -94,7 +97,7 @@ fn runQueue(self: *Scheduler, queue: *Queue) !?i32 {
9497

9598
var copy = task;
9699
copy.ms = now + repeat_delay;
97-
try self.secondary.add(copy);
100+
try self.low_priority.add(copy);
98101
}
99102
_ = queue.remove();
100103
next = queue.peek();
@@ -144,11 +147,11 @@ test "Scheduler" {
144147
try testing.expectEqualSlices(u32, &.{ 1, 1, 2 }, task.calls.items);
145148

146149
std.Thread.sleep(std.time.ns_per_ms * 5);
147-
// wont' run secondary
150+
// won't run low_priority
148151
try testing.expectEqual(null, try s.runHighPriority());
149152
try testing.expectEqualSlices(u32, &.{ 1, 1, 2 }, task.calls.items);
150153

151-
//runs secondary
154+
//runs low_priority
152155
try testing.expectDelta(2, try s.runLowPriority(), 1);
153156
try testing.expectEqualSlices(u32, &.{ 1, 1, 2, 2 }, task.calls.items);
154157
}

src/browser/html/window.zig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,6 @@ pub const Window = struct {
277277
};
278278
fn createTimeout(self: *Window, cbk: Function, delay_: ?u32, page: *Page, opts: CreateTimeoutOpts) !u32 {
279279
const delay = delay_ orelse 0;
280-
if (delay > 5000) {
281-
if (!@import("builtin").is_test) {
282-
log.warn(.user_script, "long timeout ignored", .{ .delay = delay, .interval = opts.repeat });
283-
}
284-
// self.timer_id is u30, so the largest value we can generate is
285-
// 1_073_741_824. Returning 2_000_000_000 makes sure that clients
286-
// can call cancelTimer/cancelInterval without breaking anything.
287-
return 2_000_000_000;
288-
}
289-
290280
if (self.timers.count() > 512) {
291281
return error.TooManyTimeout;
292282
}

0 commit comments

Comments
 (0)