Create timeout

From dykyi-roman/awesome-claude-code · View interactive version

Create timeout is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. Generates Timeout pattern components for PHP 8.4. Creates execution time limit infrastructure with configurable timeouts, fallback support, stream timeouts, and unit tests.

How to use this skill

Skill file

Timeout Pattern Generator

Creates Timeout pattern infrastructure for execution time limits with fallback support.

When to Use

| Scenario | Example |

|----------|---------|

| External API calls | HTTP requests to third-party services |

| Database queries | Long-running or unoptimized queries |

| Queue consumers | Message processing time limits |

| File operations | Large file uploads/downloads |

| Distributed calls | gRPC, SOAP, or REST inter-service calls |

| Batch processing | Individual item timeout within batch |

Component Characteristics

TimeoutConfig

TimeoutInterface

TimeoutExecutor

StreamTimeoutExecutor

TimeoutException

TimeoutMiddleware


Generation Process

Step 1: Analyze Request

Determine:

Step 2: Generate Core Components

  1. Domain Layer (src/Domain/Shared/Timeout/)
  1. Infrastructure Layer (src/Infrastructure/Resilience/Timeout/)
  1. Presentation Layer (src/Presentation/Middleware/)
  1. Tests

File Placement

| Layer | Path |

|-------|------|

| Domain Types | src/Domain/Shared/Timeout/ |

| Infrastructure | src/Infrastructure/Resilience/Timeout/ |

| Middleware | src/Presentation/Middleware/ |

| Unit Tests | tests/Unit/{Layer}/{Path}/ |


Key Principles

Signal-Based Timeout (CLI)

  1. Register SIGALRM handler
  2. Set pcntl_alarm with timeout duration
  3. Execute operation
  4. Cancel alarm on completion
  5. Restore previous handler
  6. If alarm fires → throw TimeoutException

Stream-Based Timeout (FPM/CLI)

  1. Set stream_set_timeout on resource
  2. Check stream_get_meta_data for timed_out flag
  3. Works for network I/O operations

Fallback Strategy

  1. On timeout, check if fallback provided
  2. Execute fallback within separate timeout (prevent cascading)
  3. If no fallback, throw TimeoutException
  4. Log timeout event with context

Naming Conventions

| Component | Pattern | Example |

|-----------|---------|---------|

| Config VO | TimeoutConfig | TimeoutConfig |

| Interface | TimeoutInterface | TimeoutInterface |

| Signal Impl | SignalTimeoutExecutor | SignalTimeoutExecutor |

| Stream Impl | StreamTimeoutExecutor | StreamTimeoutExecutor |

| Null Impl | NullTimeoutExecutor | NullTimeoutExecutor |

| Exception | TimeoutException | TimeoutException |

| Factory | TimeoutExecutorFactory | TimeoutExecutorFactory |

| Middleware | TimeoutMiddleware | TimeoutMiddleware |

| Test | {ClassName}Test | SignalTimeoutExecutorTest |


Quick Template Reference

TimeoutConfig

final readonly class TimeoutConfig
{
    public function __construct(
        public float $durationSeconds,
        public ?\Closure $fallback = null, // `callable` is not a valid property type — store a Closure
        public bool $shouldRetry = false,
        public string $operationName = 'unknown',
    ) {}

    public static function fast(): self; // 3 seconds
    public static function standard(): self; // 10 seconds
    public static function slow(): self; // 30 seconds
    public static function of(float $seconds): self;

    /** Accepts any callable at the boundary, stores it as a Closure via `$fallback(...)` */
    public function withFallback(callable $fallback): self;
}

TimeoutInterface

interface TimeoutInterface
{
    /**
     * @template T
     * @param callable(): T $operation
     * @return T
     * @throws TimeoutException
     */
    public function execute(callable $operation, TimeoutConfig $config): mixed;
}

TimeoutException

final class TimeoutException extends \RuntimeException
{
    public function __construct(
        public readonly float $elapsedSeconds,
        public readonly float $timeoutSeconds,
        public readonly string $operationName,
        ?\Throwable $previous = null,
    ) {
        parent::__construct(
            sprintf('Operation "%s" timed out after %.2fs (limit: %.2fs)', $operationName, $elapsedSeconds, $timeoutSeconds),
            0,
            $previous,
        );
    }
}

Usage Example

$timeout = $timeoutFactory->create();

try {
    $result = $timeout->execute(
        operation: fn() => $httpClient->request('GET', $url),
        config: TimeoutConfig::of(5.0),
    );
} catch (TimeoutException $e) {
    $logger->warning('API call timed out', [
        'operation' => $e->operationName,
        'elapsed' => $e->elapsedSeconds,
        'timeout' => $e->timeoutSeconds,
    ]);
    return $cachedResult;
}

Composition with Circuit Breaker

$result = $circuitBreaker->execute(
    operation: fn() => $timeout->execute(
        operation: fn() => $api->call($request),
        config: TimeoutConfig::of(5.0),
    ),
    fallback: fn() => $cache->get($cacheKey),
);

Anti-patterns to Avoid

| Anti-pattern | Problem | Solution |

|--------------|---------|----------|

| No timeout | Indefinite blocking | Always set explicit timeout |

| Too aggressive | Fail on normal slow responses | Tune per-operation |

| Global timeout | One size doesn't fit all | Per-operation config |

| No fallback | Hard failure on timeout | Provide degraded response |

| Ignoring context | Can't debug timeouts | Include operation name |

| Nested timeouts | Outer timeout kills inner | Coordinate timeout budgets |


References

For complete PHP templates and examples, see:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.