<?php
// config/packages/deeep_service_client.php
declare(strict_types=1);
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
return App::config([
'deeep_service_client' => [
// Настройки по умолчанию для всех сервисов
'defaults' => [
'timeout' => [
'connect' => 5.0, // Таймаут соединения в секундах
'request' => 30.0, // Общий таймаут запроса в секундах
],
'retry' => [
'enabled' => true,
'max_attempts' => 3, // Всего попыток (1 начальная + 2 повтора)
'delay' => 1000, // Начальная задержка в миллисекундах
'multiplier' => 2.0, // Множитель экспоненциальной задержки
'max_delay' => 30000, // Максимальная задержка в миллисекундах
'jitter' => 0.1, // Фактор случайного разброса (0.0 - 1.0)
'retry_on' => [429, 502, 503, 504],
'retry_on_methods' => ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'],
'respect_retry_after' => true,
],
'logging' => [
'enabled' => true,
'level' => 'info', // Уровень логирования: debug, info, warning, error
'log_body' => false, // Логировать тела запросов/ответов
'max_body_length' => 1000,
'sensitive_headers' => [ // Заголовки для маскировки в логах
'authorization',
'api-key',
'x-api-key',
'cookie',
'set-cookie',
],
],
'circuit_breaker' => [
'enabled' => false,
'failure_threshold' => 5, // Ошибок до размыкания цепи
'open_timeout' => 30, // Секунд до повторной попытки
'half_open_requests' => 1, // Тестовых запросов в полуоткрытом состоянии
],
],
'services' => [
// Простой сервис
'my_api' => [
'host' => '%env(resolve:MY_API_HOST)%',
'proxy' => '%env(resolve:PROXY_HOST)%', // Опционально
'auth' => [
'type' => 'bearer', // bearer, api_key, basic
'credentials' => [
'token' => '%env(resolve:API_TOKEN)%',
],
],
// Переопределения для конкретного сервиса
'timeout' => [
'connect' => 10.0,
'request' => 60.0,
],
'retry' => [
'max_attempts' => 5,
'delay' => 2000,
],
'logging' => [
'log_body' => true,
],
'circuit_breaker' => [
'enabled' => true,
],
],
// Multi-host конфигурация
'multi_host_api' => [
'hosts' => [
'strategy' => 'failover', // failover, round_robin, parallel
'mode' => 'first', // Для parallel: first, all
'timeout' => 5.0, // Таймаут стратегии
'health_check' => [
'enabled' => false,
'interval' => 30,
'path' => '/health',
'timeout' => 5,
],
'list' => [
[
'url' => 'https://primary.example.com',
'proxy' => null,
'weight' => 1,
],
[
'url' => 'https://backup.example.com',
],
],
],
],
],
],
]);
Переопределение настроек timeout и retry для отдельных запросов через ConfigurableRequestInterface:
use Deeep\ServiceClient\Config\RetryConfig;
use Deeep\ServiceClient\Config\TimeoutConfig;
use Deeep\ServiceClient\Contract\ConfigurableRequestInterface;
use Deeep\ServiceClient\Enum\HttpMethod;
use Deeep\ServiceClient\Request\AbstractRequest;
final readonly class SlowApiRequest extends AbstractRequest implements ConfigurableRequestInterface
{
public function __construct(
private array $payload,
) {}
public function getService(): string
{
return 'slow_api';
}
public function getMethod(): HttpMethod
{
return HttpMethod::POST;
}
public function getUri(): string
{
return '/process';
}
public function getBody(): array
{
return $this->payload;
}
public function getTimeout(): ?TimeoutConfig
{
return new TimeoutConfig(
connect: 10.0,
request: 300.0, // 5 минут для медленного API
);
}
public function getRetry(): ?RetryConfig
{
return new RetryConfig(
enabled: true,
maxAttempts: 5,
delay: 5000,
);
}
}
How can I help you explore Laravel packages today?