Client-side deadline interceptor for thesis/grpc. It gives every outgoing call a deadline and applies it in two places so the client and server agree on it:
- the
grpc-timeoutheader, which the server enforces (cancelling the RPC and returningDEADLINE_EXCEEDED); - a local
TimeoutCancellation, so the client gives up on its own even if the server never responds (a plain client deadline otherwise never reaches the server — the transport does not setgrpc-timeoutby itself).
composer require thesis/grpc-deadlineRegister the interceptor on the client's unary and stream chains. A default deadline covers every call:
use Thesis\Grpc\Client;
use Thesis\Grpc\Deadline;
use Thesis\Grpc\Metadata\Timeout;
$deadline = new Deadline\ClientInterceptor(Timeout::seconds(30));
$client = new Client\Builder()
->withUnaryInterceptors($deadline)
->withStreamInterceptors($deadline)
->build();Override it per call by attaching a Timeout to the call's metadata — the call's own deadline wins
over the default:
use Thesis\Grpc\Metadata;
use Thesis\Grpc\Metadata\Timeout;
$client->invoke($request, $invoke, new Metadata()->withKey(Timeout::milliseconds(200)));Constructed with no default (new Deadline\ClientInterceptor()), the interceptor only acts on calls
that carry their own grpc-timeout, and leaves the rest untouched.