Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Scheduler#load and Async::Idler for load shedding and saturation. #309

Merged
merged 2 commits into from Mar 5, 2024

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented Mar 4, 2024

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.

Typical use cases include connection load shedding:

scheduler = Fiber.scheduler
while true
  server.wait_readable 
  if scheduler.load < 0.8
    handle_connection(server.accept)
  end
end

Request load shedding:

while request = client.read_request
  if scheduler.load > 0.95
    return too_many_requests #429
  end
  # ...handle request...

And load saturation:

idler = Async::Idler.new(0.95)
jobs.each do |job|
  idler.async do # schedule as many jobs concurrently up to 95% loading
    job.process
  end
end

While I'm confident the interface is good enough, there is a small chance that I've not anticipated some critical use case. We may introduce changes to this interface. Async::Idler is unlikely to change.

Fixes #277.

Types of Changes

  • New feature.

Contribution

`Async::Idler` introduces a `maximum_load` and functions like a
semaphore, in that it will schedule tasks until the maximum load is
reached.
@ioquatix ioquatix changed the title Add Scheduler#load and Async::Idler for scheduling tasks when idle. Introduce Scheduler#load and Async::Idler for load shedding and saturation. Mar 4, 2024
@ioquatix ioquatix self-assigned this Mar 4, 2024
@ioquatix ioquatix merged commit fd79eb4 into main Mar 5, 2024
44 of 48 checks passed
@ioquatix ioquatix deleted the idler branch March 5, 2024 02:22
ioquatix added a commit that referenced this pull request Mar 5, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic concurrency limiter / adaptive semaphore
1 participant