-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
Scheduler#load
and Async::Idler
for load shedding and s…
…aturation. (#309) Introduce Scheduler#load which is a 1-second load average. This load average can be used to detect overload conditions in the event loop. In addition, introduce Async::Idler which will schedule tasks up to a given maximum_load.
- Loading branch information
Showing
5 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require_relative '../../lib/async' | ||
require_relative '../../lib/async/idler' | ||
|
||
Async do | ||
idler = Async::Idler.new(0.8) | ||
|
||
Async do | ||
while true | ||
idler.async do | ||
$stdout.write '.' | ||
while true | ||
sleep 0.1 | ||
end | ||
end | ||
end | ||
end | ||
|
||
scheduler = Fiber.scheduler | ||
while true | ||
load = scheduler.load | ||
|
||
$stdout.write "\nLoad: #{load} " | ||
sleep 1.0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
module Async | ||
class Idler | ||
def initialize(maximum_load = 0.8, backoff: 0.01, parent: nil) | ||
@maximum_load = maximum_load | ||
@backoff = backoff | ||
@parent = parent | ||
end | ||
|
||
def async(*arguments, parent: (@parent or Task.current), **options, &block) | ||
wait | ||
|
||
# It is crucial that we optimistically execute the child task, so that we prevent a tight loop invoking this method from consuming all available resources. | ||
parent.async(*arguments, **options, &block) | ||
end | ||
|
||
def wait | ||
scheduler = Fiber.scheduler | ||
backoff = nil | ||
|
||
while true | ||
load = scheduler.load | ||
break if load < @maximum_load | ||
|
||
if backoff | ||
sleep(backoff) | ||
backoff *= 2.0 | ||
else | ||
scheduler.yield | ||
backoff = @backoff | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2018-2023, by Samuel Williams. | ||
|
||
require 'async/idler' | ||
require 'sus/fixtures/async' | ||
|
||
require 'chainable_async' | ||
|
||
describe Async::Idler do | ||
include Sus::Fixtures::Async::ReactorContext | ||
let(:idler) {subject.new(0.5)} | ||
|
||
it 'can schedule tasks up to the desired load' do | ||
# Generate the load: | ||
Async do | ||
while true | ||
idler.async do | ||
while true | ||
sleep 0.1 | ||
end | ||
end | ||
end | ||
end | ||
|
||
# This test must be longer than the test window... | ||
sleep 1.1 | ||
|
||
# Verify that the load is within the desired range: | ||
expect(Fiber.scheduler.load).to be_within(0.1).of(0.5) | ||
end | ||
|
||
it_behaves_like ChainableAsync | ||
end |