Skip to content

Add Swoole coroutine concurrency#84

Merged
loks0n merged 1 commit into
mainfrom
swoole-concurrency
Jun 2, 2026
Merged

Add Swoole coroutine concurrency#84
loks0n merged 1 commit into
mainfrom
swoole-concurrency

Conversation

@loks0n
Copy link
Copy Markdown
Contributor

@loks0n loks0n commented Jun 2, 2026

Summary

  • Add adapter-owned resources() and per-message context() containers, aligned with the HTTP adapter pattern
  • Add maxCoroutines support to the Swoole queue adapter so multiple coroutine consumers can process messages concurrently
  • Update queue server DI usage to delegate resource/context ownership to runner adapters
  • Add E2E coverage for adapter resources/context and Swoole coroutine concurrency
  • Remove stale Travis config and README badge
  • Replace deprecated PHPUnit @Depends metadata with attributes

Tests

  • docker compose exec -T tests vendor/bin/phpunit --display-phpunit-deprecations
  • docker compose exec -T tests composer lint
  • docker compose exec -T tests composer check

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jun 2, 2026

Greptile Summary

This PR introduces adapter-owned resources() and per-message context() containers (aligned with the HTTP adapter pattern), adds maxCoroutines support to the Swoole adapter so multiple coroutine consumers can process messages concurrently, and delegates resource/context ownership from Server to the adapter layer.

  • Adapter::consume() now resets $this->context to a fresh Container on every message, eliminating the inter-message context leak that the previous Server.php reset guarded against; Swoole::consume() replicates this per-coroutine via Coroutine::getContext().
  • The new ConsumerFailures exception collects errors from all coroutines, replacing the old behaviour where only the first failure was re-thrown.
  • Travis CI config is removed and the #[Depends] attribute replaces deprecated PHPUnit docblock annotations.

Confidence Score: 5/5

Safe to merge; the concurrency and context-isolation logic is sound for the expected consumer contract, and the concerns raised are edge-case design considerations rather than breakages in the normal flow.

The core changes are well-tested: per-message context reset is verified by testContextDoesNotLeakBetweenMessages, concurrent coroutine behaviour by testMaxCoroutinesConsumeInParallel, and full error collection by testPreservesAllCoroutineConsumerErrors. The only flagged items are a redundant property assignment and a context() fallback that could corrupt shared state only if a consumer calls errorCallback with a non-null message without having first called messageCallback — an atypical consumer contract violation.

src/Queue/Adapter/Swoole.php — the context() fallback to resources() and the redundant maxCoroutines assignment are worth a quick look before merging.

Important Files Changed

Filename Overview
src/Queue/Adapter.php Refactored base adapter: moves Consumer to constructor, adds resources/context containers, and implements a base consume() that resets context per message to prevent leaks
src/Queue/Adapter/Swoole.php Adds coroutine-per-message context isolation via Coroutine::getContext(), adds maxCoroutines concurrency support, and introduces ConsumerFailures for multi-error collection; maxCoroutines is set twice (promoted property + body)
src/Queue/Server.php Removes the Container constructor param and delegates resource/context ownership to the adapter; all container access now flows through adapter.resources() and adapter.context()
src/Queue/Error/ConsumerFailures.php New exception class that collects all coroutine consumer errors and exposes them via getErrors(); correctly chains the first error as the previous exception
src/Queue/Adapter/Workerman.php Constructor updated to accept resources Container and delegate to the updated parent; no functional changes
tests/Queue/E2E/Adapter/SwooleConcurrencyTest.php New E2E tests verifying concurrent coroutine consumption and full error collection across all failing coroutines
tests/Queue/E2E/Adapter/ServerTelemetryTest.php Adds tests for adapter resources/context injection and verifies context does not leak between consecutive messages
tests/Queue/E2E/Adapter/Base.php Replaces deprecated @Depends docblock annotation with the PHPUnit 10+ #[Depends] attribute

Reviews (3): Last reviewed commit: "Add Swoole coroutine concurrency" | Re-trigger Greptile

Comment thread src/Queue/Adapter.php
Comment thread src/Queue/Adapter/Swoole.php
@loks0n loks0n force-pushed the swoole-concurrency branch 2 times, most recently from 0ec38a5 to 6e479b7 Compare June 2, 2026 18:05
@loks0n loks0n force-pushed the swoole-concurrency branch from 6e479b7 to 88a5431 Compare June 2, 2026 18:11
@loks0n loks0n merged commit cd4248a into main Jun 2, 2026
8 checks passed
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