Skip to main content
gRPC

Deadlines & Timeouts

RequestParseValidateMedium

What you will learn

  • Deadlines propagate down the call chain โ€” child RPCs inherit. Test cancellation mid-streaming to verify handlers abort
  • Deadline-exceeded (code 4) is client-side by default โ€” server-side must respect client deadline, not continue work
  • Never set deadlines in user-facing APIs above 30s โ€” forces clients to fail fast, preventing resource holds

Description

Set request deadlines and handle timeout scenarios.

Test Scenario

// grpcurl with timeout
grpcurl -plaintext \
  -max-time 2 \
  -d '{"query": "slow-search"}' \
  localhost:50051 \
  practice.products.ProductService/SearchProducts

// If server takes > 2s:
ERROR:
  Code: DeadlineExceeded
  Message: context deadline exceeded

Testing Tips

  • โ€ขDeadline = absolute time when RPC should complete
  • โ€ขTimeout = duration from now
  • โ€ขServer should check context.Done() regularly
// Playwright API Testing - gRPC (via REST gateway)
import { test, expect } from '@playwright/test';
test('gRPC unary call via gateway', async ({ request }) => {
const response = await request.post('/api/grpc/UserService/GetUser', {
data: { id: '1' }
});
expect(response.ok()).toBeTruthy();
const data = await response.json();
expect(data.user).toBeDefined();
});

Challenge ID: grpc-deadline

2 frameworks available