Skip to content

feat(queue): middleware hooks that can end a message without failing it - #339

Closed
loks0n wants to merge 1 commit into
mainfrom
feat/queue-middleware
Closed

loks0n wants to merge 1 commit into
mainfrom
feat/queue-middleware

Conversation

@loks0n

@loks0n loks0n commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Why

An init hook can only stop a job by throwing, and a throw is rejected. On Redis the payload stays on the failed list for the retry sweep. Appwrite Cloud refused over-limit tenants that way in its functions worker, and fra1's queue Dragonfly ended up holding 273k refusals (~2 GB) after one week and started evicting live jobs (appwrite-labs/cloud#6062).

A refusal is an outcome, not a failure. The hook needs a way to say "handled, don't run the job" and have the broker commit the message.

What

Server::middleware() returns a Hook that matches groups the same way init() does and wraps the job:

$server->middleware()
    ->groups(['functions'])
    ->inject('next')
    ->action(function (callable $next) {
        if ($refused) {
            return; // committed
        }
        return $next();
    });
  • next is injected like any other resource. It runs the rest of the chain and then the job, and returns the job's result.
  • Middleware runs after the init hooks. The first one registered is the outermost.
  • A middleware that throws is a failure like any other and is rejected.
  • The init-hook group matching moves into hooksFor(), so both hook kinds share one copy of it.

Additive: nothing changes for servers that don't register middleware.

Tests

MiddlewareTest pushes one message through Server::start() and the shared commit/reject phases:

  • returning without next commits and skips the job
  • next runs the job and passes its result back
  • init hooks run first, middleware runs outermost-first, and middleware for other groups is skipped
  • a throwing middleware rejects

With the middleware loop removed, all four fail. The full queue unit suite passes (157 tests), and bin/monorepo check queue and Vale are clean.

Release

Cloud is on queue ^5.0.2, so this would ship as queue/6.1.0 and cloud would move to 6.x.

🤖 Generated with Claude Code

An init hook can only stop a job by throwing, and a throw is rejected: on
Redis the payload stays on the failed list for the retry sweep. Appwrite
Cloud refused over-limit tenants that way and kept 273k refusals (~2 GB) in
one queue in a week, until the instance started evicting live jobs.

Server::middleware() returns a Hook that matches groups like init() and
wraps the job. The action injects `next` and calls it to run the rest of
the chain; returning without calling it ends the message as handled, so the
adapter commits it. Middleware runs after the init hooks, and the first one
registered is the outermost. The init-hook matching moves into hooksFor()
so both hook kinds share it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@walter-o-brien

walter-o-brien Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment on lines +444 to +446
$inner = $next;
$next = function () use ($hook, $inner, $message): mixed {
$this->context()->set('next', fn(): \Closure => $inner);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bug · medium
$inner is invoked on every call to the injected continuation, so middleware can call $next() twice and execute the job (and any inner middleware) twice during one message delivery; the adapter will then commit the message as a success. Guard the continuation as one-shot (for example, throw on a second invocation) to prevent accidental duplicate side effects.

@loks0n

loks0n commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

Release path: server-ce requires utopia-php/queue: ^5.0.1, so cloud can't take 6.x yet. The same commit is cherry-picked onto queue/5.0.2 as feat/queue-middleware-5.x (all 149 unit tests pass there), and that branch is published to the mirror via Split Dev. appwrite-labs/cloud#6063 pins dev-feat/queue-middleware-5.x as 5.1.0 until a queue/5.1.0 tag exists.

@github-actions

Copy link
Copy Markdown

Benchmark results

queue — workload shapes across both concurrency axes (4 cores, 600 messages, median of 3)

workload shape redis msg/s nats msg/s
io 1p x 1c x 1f 39 39
io 1p x 4c x 4f 156 154
io 4p x 1c x 1f 156 157
cpu 1p x 1c x 1f 309 309
cpu 1p x 4c x 4f 325 320
cpu 4p x 1c x 1f 741 797
mixed 1p x 1c x 1f 72 72
mixed 1p x 4c x 4f 217 214
mixed 4p x 1c x 1f 275 270
null 1p x 16c x 16f 18629 15755
null 1p x 16c x 64f 25090 25385
null 1p x 16c x 100f 27515 27551

Np x Mc x Ff = N consumer processes x M handler coroutines x a prefetch limit of F
unacknowledged messages. io yields and should follow coroutines; cpu does not and should follow
processes; mixed is in between. null has no handler at all, so it measures the broker
rather than the job -- the only row where prefetch can matter.

Shared CI runners — treat absolute numbers as rough, compare modes within a run. Commit 718d341.

@ChiragAgg5k

Copy link
Copy Markdown
Member

Queue now lives in packages/queue in appwrite/appwrite (appwrite/appwrite#13931), and this copy is being retired. @loks0n, please re-open this against packages/queue there if you still want it.

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.

2 participants