Module: A2A::Client::Middleware

Defined in:
lib/a2a/modules.rb,
lib/a2a/client/middleware.rb,
lib/a2a/client/middleware/retry_interceptor.rb,
lib/a2a/client/middleware/logging_interceptor.rb,
lib/a2a/client/middleware/rate_limit_interceptor.rb,
lib/a2a/client/middleware/circuit_breaker_interceptor.rb

Defined Under Namespace

Classes: Base, CircuitBreakerInterceptor, LoggingInterceptor, RateLimitInterceptor, RetryInterceptor

Class Method Summary collapse

Class Method Details

.default_stack(config = {}) ⇒ Array<Base>

Create default middleware stack

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options

Returns:

  • (Array<Base>)

    Default middleware stack



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/a2a/client/middleware.rb', line 89

def self.default_stack(config = {})
  stack = []

  # Add retry middleware
  stack << RetryInterceptor.new if config.fetch("retry", true)

  # Add logging middleware
  stack << LoggingInterceptor.new if config.fetch("logging", true)

  # Add rate limiting if configured
  if config["rate_limit"]
    stack << RateLimitInterceptor.new(
      requests_per_second: config["rate_limit"]["requests_per_second"] || 10
    )
  end

  # Add circuit breaker if configured
  if config["circuit_breaker"]
    stack << CircuitBreakerInterceptor.new(
      failure_threshold: config["circuit_breaker"]["failure_threshold"] || 5
    )
  end

  stack
end

.from_config(config) ⇒ Base

Create middleware from configuration

Parameters:

  • config (Hash)

    Middleware configuration

Returns:

  • (Base)

    Configured middleware instance



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/a2a/client/middleware.rb', line 48

def self.from_config(config)
  type = config["type"] || config[:type]

  case type
  when "retry"
    RetryInterceptor.new(
      max_attempts: config["max_attempts"] || config[:max_attempts] || 3,
      initial_delay: config["initial_delay"] || config[:initial_delay] || 1.0,
      max_delay: config["max_delay"] || config[:max_delay] || 60.0,
      backoff_multiplier: config["backoff_multiplier"] || config[:backoff_multiplier] || 2.0,
      retryable_errors: config["retryable_errors"] || config[:retryable_errors]
    )
  when "logging"
    LoggingInterceptor.new(
      logger: config["logger"] || config[:logger],
      log_level: config["log_level"] || config[:log_level] || :info,
      log_requests: config["log_requests"] || config[:log_requests] || true,
      log_responses: config["log_responses"] || config[:log_responses] || true,
      log_errors: config["log_errors"] || config[:log_errors] || true
    )
  when "rate_limit"
    RateLimitInterceptor.new(
      requests_per_second: config["requests_per_second"] || config[:requests_per_second] || 10,
      burst_size: config["burst_size"] || config[:burst_size] || 20
    )
  when "circuit_breaker"
    CircuitBreakerInterceptor.new(
      failure_threshold: config["failure_threshold"] || config[:failure_threshold] || 5,
      timeout: config["timeout"] || config[:timeout] || 60,
      expected_errors: config["expected_errors"] || config[:expected_errors]
    )
  else
    raise ArgumentError, "Unknown middleware type: #{type}"
  end
end