Skip to content

Conversation

@samuel-williams-shopify
Copy link
Contributor

@samuel-williams-shopify samuel-williams-shopify commented Aug 28, 2025

This PR introduces Async::PriorityQueue, a unique concurrency primitive that implements the inverse of traditional task prioritization. Instead of prioritizing work items, it prioritizes the consumers requesting work, enabling sophisticated resource allocation strategies based on who is asking rather than what is being processed.

  • Worker Class Hierarchies: Critical system processes bypass background job consumers.
  • Load Balancing: High-priority services get first access to shared resource pools.
  • Capacity Management: Important consumers reserve access to constrained resources.

Example

Sync do
	queue = Async::PriorityQueue.new
	
	# Start consumers with different priorities:
	Async {puts queue.dequeue(priority: 1)}   # Low priority
	Async {puts queue.dequeue(priority: 10)}  # High priority
	
	# High priority consumer gets this first:
	queue.push("item")
end

Types of Changes

  • New feature.

Contribution

@franciscojmc
Copy link

Consideration: Handling Dead Fibers in the Waiter Queue

Great implementation. I wanted to raise a potential edge case for discussion regarding fiber lifecycle management.

Scenario to consider:

What happens if a fiber dies unexpectedly while waiting in the queue? Currently, the dead fiber's Waiter would remain in the priority heap, which could lead to items being lost:

queue = Async::PriorityQueue.new

# Fiber starts waiting
fiber = Async do
  queue.dequeue(priority: 10)
end

# Fiber terminates unexpectedly
fiber.stop

# The Waiter object remains in @waiting heap

# Later, when we push an item:
queue.push("item1")
# This would:
# 1. Pop the dead fiber's waiter from heap
# 2. Assign "item1" to the dead waiter
# 3. Signal a condition variable no one is waiting on
# Result: "item1" is effectively lost

What are your thoughts on whether this should be handled?

@samuel-williams-shopify samuel-williams-shopify force-pushed the async-priority-waiter branch 2 times, most recently from 557d30a to d4af748 Compare September 1, 2025 07:40
@ioquatix
Copy link
Member

ioquatix commented Sep 1, 2025

This implementation is both fiber and thread safe.

@ioquatix ioquatix enabled auto-merge (squash) September 1, 2025 09:04
Copy link
Member

@ioquatix ioquatix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ioquatix ioquatix merged commit 17b4e78 into main Sep 1, 2025
73 of 81 checks passed
@ioquatix ioquatix deleted the async-priority-waiter branch September 1, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants