Shared\Channel

OxPHP\Shared\Channel is a bounded multi-producer multi-consumer channel that lives in the shared registry and is visible to every PHP worker in the process. Use it when a request handler and a background worker, or two workers, need to exchange work items in FIFO order. Inside a fiber, blocking calls suspend cooperatively so the underlying worker thread stays free for other requests.

Overview

  • The channel is bounded. Capacity is fixed at construction. Once the channel is full, send blocks (or suspends a fiber), while trySend reports the result without waiting.
  • It is MPMC: any number of senders and receivers across threads, with FIFO delivery.
  • It is fiber-aware. Under worker mode with the async pool, blocking variants suspend the fiber instead of the OS thread wherever the fiber can be suspended (see Fiber vs blocking behaviour). In traditional mode they block the OS thread.
  • Results are typed. Every send/recv returns a Channel\SendResult / Channel\RecvResult. Closed / full / timeout are normal outcomes for fan-out dispatchers, so they appear as result variants instead of exceptions.

API reference

php
namespace OxPHP\Shared; final class Channel implements Shareable, \Countable { public function __construct(int $capacity); // ── Receive ── public function tryRecv(): Channel\RecvResult; // non-blocking public function recv(): Channel\RecvResult; // block forever / fiber-cancel public function recvTimeout(int $ms): Channel\RecvResult; // bounded ($ms > 0) // ── Send ── public function trySend(mixed $value): Channel\SendResult; public function send(mixed $value): Channel\SendResult; public function sendTimeout(mixed $value, int $ms): Channel\SendResult; // ── Batch ── public function sendMany(array $values, int $ms): int; // partial count, no throw public function recvMany(int $max, int $ms): array; // partial array, no throw // ── Lifecycle ── public function close(): bool; public function isClosed(): bool; public function count(): int; public function id(): int; } namespace OxPHP\Shared\Channel; enum RecvStatus { case Ok; case Empty; case Timeout; case Closed; } enum SendStatus { case Ok; case Full; case Timeout; case Closed; } final class RecvResult { public function isOk(): bool; public function isEmpty(): bool; public function isTimeout(): bool; public function isClosed(): bool; public function value(): mixed; // throws SharedException if not Ok public function valueOr(mixed $default): mixed; public function status(): RecvStatus; } final class SendResult { public function isOk(): bool; public function isFull(): bool; public function isTimeout(): bool; public function isClosed(): bool; public function status(): SendStatus; }

Size limits

These ceilings guard against allocation bombs from untrusted input. Each is set by an environment variable and raises a catchable exception rather than aborting the process:

  • Capacity. new Channel($capacity) pre-allocates a slot array. A capacity whose slot array would exceed SHARED_MAX_CHANNEL_BYTES (default 64 MiB, ~2M slots) throws CapacityException. Channels are normally tens to low thousands deep, so this fires only on pathological values. The budget always admits at least one slot, so even a zero SHARED_MAX_CHANNEL_BYTES allows a capacity-1 channel. A non-positive $capacity is a separate TypeException.
  • Per process. Like every Shared\* constructor, new Channel() also throws CapacityException when creating it would exceed SHARED_MAX_ENTRIES or SHARED_MAX_BYTES (see Configuration).
  • Per value. Each sent value is serialised, and one over SHARED_MAX_VALUE_SIZE (default 1 MiB) throws ValueTooLargeException. This applies to send, trySend, sendTimeout and sendMany, which rejects the whole batch, sending nothing, if any element is over the cap. Shared\Map enforces the same per-value cap.

Wait policies

Every direction has three method variants; the suffix encodes the wait policy:

Suffix Behaviour
try* Non-blocking. Reports Empty / Full if the call cannot proceed.
(bare name) Block forever, or until the request fiber is cancelled. Where the fiber cannot be suspended, throws SharedException instead of waiting (see Fiber vs blocking behaviour).
*Timeout Bounded wait. $ms must be > 0; use one of the other forms for forever or non-blocking.

$ms is always a positive integer of milliseconds for recvTimeout / sendTimeout / sendMany / recvMany. Zero, negative, non-int, and absent values raise OxPHP\Shared\TypeException.

Reachable result variants per method

SendResult::Full and RecvResult::Empty are emitted only by the non-blocking try* calls. A blocking variant either acquires the slot/item or runs out of budget, in which case the result is Timeout, not Full / Empty. The full reachability matrix:

Method Ok Full / Empty Timeout Closed
trySend ✓ Full ✓ — ✓
send ✓ — — ✓
sendTimeout ✓ — ✓ ✓
tryRecv ✓ Empty ✓ — ✓
recv ✓ — — ✓
recvTimeout ✓ — ✓ ✓

isFull() / isEmpty() checks on a result from a blocking call are dead code. The match examples below leave those arms as unreachable-comments to make the asymmetry obvious to readers.

Result dispatch

RecvResult and SendResult carry a status() discriminant plus (for RecvResult only) a payload. Two equivalent idioms:

php
use OxPHP\Shared\Channel; use OxPHP\Shared\Channel\RecvStatus; $ch = new Channel(capacity: 64); // Boolean accessors — terse when only one outcome matters. $result = $ch->tryRecv(); if ($result->isOk()) { process($result->value()); } elseif ($result->isEmpty()) { backoff(); } else { break; // closed } // Exhaustive match — exhaustiveness checking catches new variants. $r = $ch->recvTimeout(1500); match ($r->status()) { RecvStatus::Ok => process($r->value()), RecvStatus::Timeout => $logger->debug('idle'), RecvStatus::Closed => break, RecvStatus::Empty => /* unreachable: only tryRecv returns Empty */ , }; // Symmetric send-side dispatch — only Ok / Timeout / Closed are reachable. use OxPHP\Shared\Channel\SendStatus; $s = $ch->sendTimeout($value, 1500); match ($s->status()) { SendStatus::Ok => /* delivered */ , SendStatus::Timeout => $logger->debug('backpressure'), SendStatus::Closed => break, SendStatus::Full => /* unreachable: only trySend returns Full */ , }; // Safe accessor — never throws. $value = $ch->tryRecv()->valueOr('fallback');

RecvResult::value() throws OxPHP\Shared\SharedException if called on a non-Ok variant, so use isOk() / valueOr() / status() first. This is intentional: it surfaces the bug of acting on a missing payload as a loud failure rather than a silent null.

Fiber vs blocking behaviour

The same method calls behave differently depending on whether PHP is currently running inside a fiber:

  • Inside a fiber (worker mode + oxphp_async(...)), the blocking variants (send, recv, sendTimeout, recvTimeout) suspend the fiber. The worker thread serves other requests and tasks until the channel is ready.
  • Outside a fiber (traditional mode, or a non-async call path), the blocking variants block the OS worker thread. No other work runs on that thread until the call returns.
  • Inside a fiber that cannot be suspended, sendTimeout() and recvTimeout() block the thread as they do outside a fiber, for up to their timeout. send() and recv() have no timeout, so where they would have to wait they throw OxPHP\Shared\SharedException at once: Channel::send() found the channel full and cannot wait without a timeout where this request or task cannot be suspended; use sendTimeout(), or the same for recv() and recvTimeout(). They still succeed when no wait is needed (a value is there, or a slot is free) and still report Closed on a closed channel.

A fiber cannot be suspended inside a callback PHP runs where it refuses a fiber switch, such as a FILTER_CALLBACK of filter_input_array() given a definitions array or a __toString() that array_multisort() runs. The same goes for the cleanup a worker runs around a request's end, such as the destructors of objects the request left in its superglobals. A wait there holds the whole thread, so nothing else on it runs until the call returns, including a request on the same worker that would serve the other end of the channel.

Traditional mode still gets the channel semantics, at the cost of a blocked thread. Worker mode is the recommended deployment for any pipeline that relies on waiting.

php
<?php // Traditional mode: this recvTimeout blocks the worker thread for up to 2 seconds. $ch = new OxPHP\Shared\Channel(16); $r = $ch->recvTimeout(2000); // Worker mode: wrap in oxphp_async and recv suspends cooperatively. oxphp_worker(function () use ($ch) { $consumer = oxphp_async(function () use ($ch) { for (;;) { $r = $ch->recvTimeout(5000); if ($r->isOk()) { process($r->value()); continue; } if ($r->isClosed()) { break; } // Timeout — keep waiting. } }); oxphp_async_await($consumer); });
Fiber cancellation

Fiber cancellation (request abort, deadline elapsed at the SAPI level) still surfaces as an OxPHP\Async\AsyncException. It is not translated into RecvResult::Closed. The channel didn't close; the runtime cancelled you. Re-raise or catch as appropriate.

Close semantics

close() is idempotent; calling it a second time is a no-op. After close:

  • send, sendTimeout, trySend return SendResult::Closed.
  • sendMany returns the count actually accepted before the close (0 if already closed).
  • recv, recvTimeout, tryRecv continue to drain buffered items as RecvResult::Ok($value), then return RecvResult::Closed once empty.
  • recvMany returns the items it can drain, possibly an empty array.
  • isClosed() returns true.
  • Blocked senders wake with SendResult::Closed; blocked receivers wake with RecvResult::Closed.
php
<?php $ch = new OxPHP\Shared\Channel(4); $ch->send('one'); $ch->send('two'); $ch->close(); // Drain leftovers. for (;;) { $r = $ch->recv(); if ($r->isClosed()) { break; } echo $r->value(), "\n"; // one, two } // Further sends report Closed without throwing. $result = $ch->send('three'); assert($result->isClosed());

The pattern for a graceful pipeline shutdown is: producers stop, producer side calls close(), consumers drain in a for (;;) { $r = $ch->recv(); if ($r->isClosed()) break; … } loop and exit naturally. Either consumer never observes a null payload by mistake; the variant carries that signal.

Shutdown drain

When the OxPHP process shuts down, the OxPHP\Shared registry calls close() on every entry, including channels. From PHP's perspective this looks identical to an explicit close():

  • Blocked recv* calls return RecvResult::Closed.
  • Blocked send* calls return SendResult::Closed.
Always check the result variant

A caller that assumes recv()->value() is always safe will throw SharedException at shutdown or whenever another holder closes the channel.

Batched operations

sendMany and recvMany exist for pipelines that move items in groups. Prefer them when you routinely handle 10+ items at a time: each batch is one call into the server instead of N, which cuts per-item overhead in throughput-bound loops.

php
<?php $ch = new OxPHP\Shared\Channel(1024); // Send an array; returns the count actually accepted. $sent = $ch->sendMany([1, 2, 3, 4, 5], 100); // 5 within 100ms budget // Drain up to 10 items with a 100ms deadline. $batch = $ch->recvMany(10, 100);

Semantics worth noting:

  • Both batch methods return partial results on timeout or mid-batch close instead of throwing an exception. Check the integer / array length to detect partial progress.
  • sendMany on an already-closed channel returns 0.
  • recvMany on closed+empty returns [].
  • $ms must be > 0. There is no "drain whatever is buffered" overload, so call tryRecv() in a tight loop if that pattern matters.

Exceptions

Exception Raised by
CapacityException new Channel($capacity) when the slot array would exceed SHARED_MAX_CHANNEL_BYTES (default 64 MiB, ~2M slots), or past SHARED_MAX_ENTRIES or SHARED_MAX_BYTES.
ValueTooLargeException send / trySend / sendTimeout / sendMany when a value's serialised size exceeds SHARED_MAX_VALUE_SIZE (default 1 MiB).
TypeException Non-positive $capacity; a $ms that is zero, negative, non-int, or absent; or sending a value that is not a scalar, null, or a nested array of shareables.
SharedException RecvResult::value() on a non-Ok variant; clone $channel; send() / recv() that would have to wait where the fiber cannot be suspended.
AsyncException A blocking recv / send interrupted by fiber cancellation; surfaces as OxPHP\Async\AsyncException, not a Result variant.

Observability

The internal server exposes channels in the generic shared-registry endpoints. It starts only when INTERNAL_ADDR is set (for example INTERNAL_ADDR=127.0.0.1:9090); without it neither these endpoints nor /metrics below are reachable:

  • GET /__ox_shared/summary includes a Channel bucket with count, bytes, and ops.
  • GET /__ox_shared/entries lists registry entries with their IDs (accepts a limit query parameter).
  • GET /__ox_shared/entry?id=<id> returns per-channel state: capacity, count, closed, senders_blocked, receivers_blocked.

The ops counter (and the registry-wide oxphp_shared_operations_total{type="Channel"}) counts every recv attempt regardless of outcome: a hit, an empty/closed channel, and a timed-out recvTimeout all increment it. The metric tracks channel accesses, not only successfully transferred values.

Prometheus exposition on /metrics:

text
oxphp_shared_channel_count{channel_id="<id>"} gauge oxphp_shared_channel_senders_blocked{channel_id="<id>"} gauge oxphp_shared_channel_receivers_blocked{channel_id="<id>"} gauge oxphp_shared_channel_items_sent_total{channel_id="<id>"} counter oxphp_shared_channel_items_dropped_total{channel_id="<id>"} counter

items_dropped_total increments for the tail of a partial sendMany that could not fit.

Common patterns

HTTP producer, async consumer

php
<?php $work = new OxPHP\Shared\Channel(256); $consumer = oxphp_async(function () use ($work) { for (;;) { $r = $work->recvTimeout(30000); if ($r->isOk()) { process_job($r->value()); continue; } if ($r->isClosed()) { break; } // Timeout — health-check, then keep waiting. } }); oxphp_worker(function () use ($work) { $work->send(['url' => $_POST['url'], 'tries' => 3]); echo "queued"; });

Fan-out across multiple consumers

Spawn N async consumers on the same channel; the registry ensures exactly one of them receives each item.

php
<?php $ch = new OxPHP\Shared\Channel(1024); for ($i = 0; $i < 4; $i++) { oxphp_async(function () use ($ch, $i) { for (;;) { $r = $ch->recvTimeout(60000); if ($r->isOk()) { handle($i, $r->value()); continue; } if ($r->isClosed()) { break; } } }); }

Bounded pipeline with backpressure

trySend plus a drop counter lets a producer shed load rather than block under overload:

php
<?php if ($ch->trySend($event)->isFull()) { increment_dropped_metric(); }

Pitfalls

  • recvTimeout(0) is a TypeException, not "non-blocking". Use tryRecv() for non-blocking.
  • Values must be shareable. Scalars, null, and nested arrays of shareables are allowed. Passing an object that is not a Shared\* instance raises TypeException on send.
  • Clone is forbidden. clone $channel throws SharedException. Transfer the channel through a closure use instead, as in oxphp_async(function () use ($ch) { ... }), so both sides see the same registry entry.
  • value() on non-Ok throws. This is deliberate: it converts the "I forgot to check isOk" mistake into a loud failure. Use valueOr($default) if a default is acceptable.
  • Fiber cancellation propagates as an exception, not a Result variant. A recv() interrupted by request cancellation raises OxPHP\Async\AsyncException. Channels that close themselves still produce RecvResult::Closed.
  • Cancelled waiters can hold on to in-flight payloads. If many fibers wait on send / recv and are cancelled while their payload was about to cross, the payload can stay referenced until the next wake. Keep waiter counts bounded (e.g. cap concurrency with a Shared\Counter or channel-capacity semaphore).

Migrating from the previous API

Was Is
$ch->tryRecv() → null on empty, throws ClosedException $ch->tryRecv() → RecvResult::Empty / Closed
$ch->recv($secs) → null on timeout/close $ch->recv() (forever) / $ch->recvTimeout($ms) → RecvResult
$ch->trySend($v): bool $ch->trySend($v): SendResult
$ch->send($v, $secs): bool (throws TimeoutException/ClosedException) $ch->send($v) / $ch->sendTimeout($v, $ms) → SendResult
$ch->sendMany($vs, $secs) (throws TimeoutException on partial) $ch->sendMany($vs, $ms): int (partial count, no throw)
?float $timeout (seconds, NaN/INF tolerated) int $ms (milliseconds, must be > 0)

Prompts for your agent

These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.

prompt
Use an OxPHP Shared Channel in my code instead of the single-host job queue I have now — a database table, a Redis list, or a polling loop. Read the `shared-state/shared-channel` page through the OxPHP documentation MCP server. Find the producer and consumer that move work that way in this repository, and show me the Channel plus `oxphp_async` replacement for both sides.
  • Worker Mode: prerequisite for fiber-suspending blocking variants.
  • Async Promises: the oxphp_async() closure is the normal way to hand a Channel to a background fiber.
  • Fiber Multiplexing: how suspension keeps the worker thread productive while channel operations wait.
Found a mistake? Report it