Repository navigation
fix(messaging): bound worker memory per push batch - #14438
Conversation
… batch A 5000-token FCM batch built all 5000 PSR-7 requests (each with its own php://temp stream) before sending, then kept all 5000 results with decoded bodies and header maps until the batch finished. Two overlapping 12.6k-token pushes in one worker process pushed worker-messaging past its 512Mi limit. requestMulti now builds each request when a worker coroutine sends it, and FCM and APNS take each result through onResult as it completes, so a batch only holds the requests in flight.
streamRecipients kept the page's subscriber and target documents in scope across the yield, so 10k documents stayed alive for the whole send, and the subscriber page was still alive while the target page was fetched.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
The change replaces batch-result aggregation in the messaging adapter's request pool with a streaming
Reviewed commit: d9352f8 |
🟢 Tier S · Ready to merge
The messaging adapter now builds HTTP requests in bounded worker coroutines and can stream completed results through an optional callback. FCM and APNS use that callback to build their responses incrementally, while recipient streaming releases fetched document pages before yielding them for delivery.
📂 Walkthrough · 5
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
This PR rewrites the shared messaging worker pool in requestMulti to stream per-request results through an onResult callback as they complete, and updates the APNS/FCM push adapters plus the FCM test to the new callback contract.
Key findings
- 🟡 Non-JSON APNs error body crashes the batch callback — APNS.php:131
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
What does this PR do?
Since #14238, a push to a large FCM topic actually sends to every token. Each 5,000-token batch now holds a lot of memory, and fra1
worker-messagingwas OOMKilled at 512Mi twice (2026-10-12 ~21:37 and 2026-10-13 ~09:37 UTC). Both kills happened during back-to-back pushes of about 12.6k tokens, when two of them overlapped in one worker process. Between pushes, the pod sat at about 445 of 512 MiB.This change keeps a batch's memory to the requests in flight, instead of growing with the number of recipients:
php://tempstream)Adapter::requestMulti()onResultcallback that receives each result as it completes, so results are not kept. Without it,requestMulti()still returns every result in request order.Adapter::requestMulti()ResponsethroughonResultFCM,APNSMessaging::streamRecipients()Responseresults now come back in completion order instead of recipient order. The worker matches results by recipient, so it does not depend on the order.Benchmark
Setup:
Messaging::sendExternalMessage()path:streamRecipients→retrySend→ FCM adapter →requestMulti.appwrite/postgres:0.1.0, collections fromapp/config/collections/common.php).appwrite-dev(Alpine, PHP 8.5, Swoole 6.2,SWOOLE_HOOK_ALL), limited to 1 CPU._APP_WORKER_MAX_COROUTINES=4).The process starts at about 65 MB RSS. Peak RSS stayed within ±7 MB across the 3 runs of each case.
Before the change, one process running two overlapping big pushes peaks at about 185 MB and keeps about 180 MB afterwards. With 3 processes per pod, that crosses the 512Mi limit, which matches what fra1 saw. After the change, the same overlap peaks at about 107 MB per process.
Where the remaining memory goes in one 5,000-recipient page, after the change:
Responsebuilt (was 27.0)Dropping
headersfrom the multi results without the callback saved only about 2 MB per batch, so the result shape is unchanged.Test Plan
bin/monorepo check messaging: Pint, PHPStan and Rector passbin/monorepo test messaging --linked: 31 tests pass, 7 skippedcomposer lintand PHPStan onsrc/Appwrite/Platform/Workers/Messaging.phppasssentRelated PRs and Issues
Checklist
Related: #14462