Skip to content

Refactor Snake into a queue 2/3 - #925

Draft
bgwines wants to merge 5 commits into
bwines/snake-22-snake-cleanupfrom
bwines/snake-22-snake-queue
Draft

Refactor Snake into a queue 2/3#925
bgwines wants to merge 5 commits into
bwines/snake-22-snake-cleanupfrom
bwines/snake-22-snake-queue

Conversation

@bgwines

@bgwines bgwines commented Aug 23, 2026

Copy link
Copy Markdown

Background / Why?

Snake now arbitrates queued work while the caller owns execution capacity, so its resident-grant lifecycle adds state and synchronization without serving the queue contract. Narrowing the implementation to enqueue, dequeue, cancellation, and drop reporting makes the load-shedding component reusable by connection pools and preserves the CoDel behavior that matters.

Cancellation is now caller-owned as well. Cancelling a context does not terminate its goroutine or immediately mark its queue entry; the waiting caller observes cancellation, takes the parent mutex, and calls Snake.Cancel. A dequeue may win during that transient window. If cancellation wins, it removes the entry; if dequeue wins, cancellation observes the request’s terminal state and the caller completes the committed handoff. Under normal execution, this guarantees that cancellation cannot leave a stale resident entry, so Peek no longer needs to clean up cancelled queue heads.

Testing

New unit tests covering queue dequeue, cancellation, valve promotion, and disabled shedding.

AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

@github-actions github-actions Bot added this to the v22.0.4 milestone Aug 23, 2026
@bgwines
bgwines force-pushed the bwines/snake-22-snake-queue branch 4 times, most recently from 7609baa to 484d937 Compare August 23, 2026 20:13
@bgwines
bgwines force-pushed the bwines/snake-22-snake-cleanup branch from 2a4ae73 to defb029 Compare August 23, 2026 21:11
@bgwines
bgwines force-pushed the bwines/snake-22-snake-queue branch from 5901915 to aaed370 Compare August 23, 2026 21:11
AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

Signed-off-by: Brett Wines <bwines@slack-corp.com>
AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

Signed-off-by: Brett Wines <bwines@slack-corp.com>
AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

Signed-off-by: Brett Wines <bwines@slack-corp.com>
AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

Signed-off-by: Brett Wines <bwines@slack-corp.com>
@bgwines
bgwines force-pushed the bwines/snake-22-snake-cleanup branch from defb029 to d43468e Compare August 23, 2026 21:21
@bgwines
bgwines force-pushed the bwines/snake-22-snake-queue branch from aaed370 to c5d8643 Compare August 23, 2026 21:21
// result channel, and updates bookkeeping. Use with care: this bypasses the
// health-state transitions in peek/dequeue, so callers are responsible for
// updating dropping state if appropriate.
func (q *CoDelQueue[T]) lockedPopElem(elem *list.Element, err error) *Request[T] {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this function existed publicly only for the cancel-after-grant race. Snake doesn't own granting anymore, so it isn't needed. There were one or two internal callers, dequeue and peek. Dequeue still gets all of the functionality it needs, and peek used it for cleanup of canceled requests, but cancelation is different now; entries are synchronously removed upon cancel, so we don't need the clean-up (see PR description)

AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

Signed-off-by: Brett Wines <bwines@slack-corp.com>
@bgwines
bgwines force-pushed the bwines/snake-22-snake-queue branch from c5d8643 to 4beb66b Compare August 24, 2026 02:03
Comment on lines -306 to -315
if r.isDroppable() {
q.droppable.remove(r)
q.droppableLen--
if q.droppableLen == 0 {
q.dropping = false
}
}
q.lockedAdvanceFirstWaiting(r.codelqElem)
q.queue.Remove(r.codelqElem)
r.codelqElem = nil

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this behavior wasn't removed; it just lives in lockedRemove()

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.

1 participant