Class: PatientHttp::Configuration
- Inherits:
-
Object
- Object
- PatientHttp::Configuration
- Defined in:
- lib/patient_http/configuration.rb
Overview
Configuration for the PatientHttp processor.
This class holds all configuration options for the HTTP connection pool, including connection limits, timeouts, and other HTTP client settings. It has no dependencies on any job system.
Instance Attribute Summary collapse
-
#connection_pool_size ⇒ Integer
This is the maximum number of hosts for which connections will be kept alive for at one time.
-
#connection_timeout ⇒ Numeric?
Connection timeout in seconds.
-
#logger ⇒ Logger
Get the logger to use to report pool events.
-
#max_connections ⇒ Integer
Maximum number of concurrent connections.
-
#max_redirects ⇒ Integer
Maximum number of redirects to follow (0 disables redirects).
-
#max_response_size ⇒ Integer
Maximum response size in bytes.
-
#proxy_url ⇒ String?
HTTP/HTTPS proxy URL (supports authentication).
-
#raise_error_responses ⇒ Boolean
Whether to raise HttpError for non-2xx responses by default.
-
#request_timeout ⇒ Numeric
Default request timeout in seconds.
-
#retries ⇒ Integer
Number of retries for failed requests.
-
#shutdown_timeout ⇒ Numeric
Graceful shutdown timeout in seconds.
-
#user_agent ⇒ String?
Default User-Agent header value.
Instance Method Summary collapse
-
#decryption(callable = nil) {|data| ... } ⇒ Object
Set the decryption callable for decrypting payloads after deserialization.
-
#default_payload_store_name ⇒ Symbol?
Get the name of the default payload store.
-
#encryption(callable = nil) {|data| ... } ⇒ Object
Set the encryption callable for encrypting payloads before serialization.
- #encryption_key=(keys) ⇒ Object
-
#encryptor ⇒ Encryptor
Return an Encryptor instance.
-
#initialize(max_connections: 256, request_timeout: 60, shutdown_timeout: 30, logger: nil, max_response_size: 1024 * 1024, user_agent: "PatientHttp", raise_error_responses: false, max_redirects: 5, connection_pool_size: 100, connection_timeout: nil, proxy_url: nil, retries: 3, encryption_key: nil) ⇒ Configuration
constructor
Initializes a new Configuration with the specified options.
-
#payload_store(name = nil) ⇒ PayloadStore::Base?
Get a registered payload store by name.
-
#payload_stores ⇒ Hash{Symbol => PayloadStore::Base}
Get all registered payload stores.
-
#register_payload_store(name, adapter:, **options) ⇒ void
Register a payload store for external storage of large payloads.
-
#to_h ⇒ Hash
Convert to hash for inspection.
Constructor Details
#initialize(max_connections: 256, request_timeout: 60, shutdown_timeout: 30, logger: nil, max_response_size: 1024 * 1024, user_agent: "PatientHttp", raise_error_responses: false, max_redirects: 5, connection_pool_size: 100, connection_timeout: nil, proxy_url: nil, retries: 3, encryption_key: nil) ⇒ Configuration
Initializes a new Configuration with the specified options.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/patient_http/configuration.rb', line 63 def initialize( max_connections: 256, request_timeout: 60, shutdown_timeout: 30, logger: nil, max_response_size: 1024 * 1024, user_agent: "PatientHttp", raise_error_responses: false, max_redirects: 5, connection_pool_size: 100, connection_timeout: nil, proxy_url: nil, retries: 3, encryption_key: nil ) # Initialize payload store configuration @payload_stores = {} @default_payload_store_name = nil @payload_store_mutex = Mutex.new @encryptor = nil self.max_connections = max_connections self.request_timeout = request_timeout self.shutdown_timeout = shutdown_timeout self.logger = logger || Logger.new($stderr, level: Logger::ERROR) self.max_response_size = max_response_size self.user_agent = user_agent self.raise_error_responses = raise_error_responses self.max_redirects = max_redirects self.connection_pool_size = connection_pool_size self.connection_timeout = connection_timeout self.proxy_url = proxy_url self.retries = retries self.encryption_key = encryption_key end |
Instance Attribute Details
#connection_pool_size ⇒ Integer
Returns This is the maximum number of hosts for which connections will be kept alive for at one time.
38 39 40 |
# File 'lib/patient_http/configuration.rb', line 38 def connection_pool_size @connection_pool_size end |
#connection_timeout ⇒ Numeric?
Returns Connection timeout in seconds.
41 42 43 |
# File 'lib/patient_http/configuration.rb', line 41 def connection_timeout @connection_timeout end |
#logger ⇒ Logger
Get the logger to use to report pool events. Default is to log errors to STDERR.
102 103 104 |
# File 'lib/patient_http/configuration.rb', line 102 def logger @logger end |
#max_connections ⇒ Integer
Returns Maximum number of concurrent connections.
16 17 18 |
# File 'lib/patient_http/configuration.rb', line 16 def max_connections @max_connections end |
#max_redirects ⇒ Integer
Returns Maximum number of redirects to follow (0 disables redirects).
34 35 36 |
# File 'lib/patient_http/configuration.rb', line 34 def max_redirects @max_redirects end |
#max_response_size ⇒ Integer
Returns Maximum response size in bytes.
25 26 27 |
# File 'lib/patient_http/configuration.rb', line 25 def max_response_size @max_response_size end |
#proxy_url ⇒ String?
Returns HTTP/HTTPS proxy URL (supports authentication).
44 45 46 |
# File 'lib/patient_http/configuration.rb', line 44 def proxy_url @proxy_url end |
#raise_error_responses ⇒ Boolean
Returns Whether to raise HttpError for non-2xx responses by default.
31 32 33 |
# File 'lib/patient_http/configuration.rb', line 31 def raise_error_responses @raise_error_responses end |
#request_timeout ⇒ Numeric
Returns Default request timeout in seconds.
19 20 21 |
# File 'lib/patient_http/configuration.rb', line 19 def request_timeout @request_timeout end |
#retries ⇒ Integer
Returns Number of retries for failed requests.
47 48 49 |
# File 'lib/patient_http/configuration.rb', line 47 def retries @retries end |
#shutdown_timeout ⇒ Numeric
Returns Graceful shutdown timeout in seconds.
22 23 24 |
# File 'lib/patient_http/configuration.rb', line 22 def shutdown_timeout @shutdown_timeout end |
#user_agent ⇒ String?
Returns Default User-Agent header value.
28 29 30 |
# File 'lib/patient_http/configuration.rb', line 28 def user_agent @user_agent end |
Instance Method Details
#decryption(callable = nil) {|data| ... } ⇒ Object
Set the decryption callable for decrypting payloads after deserialization.
174 175 176 177 |
# File 'lib/patient_http/configuration.rb', line 174 def decryption(callable = nil, &block) @decryption = resolve_callable(:decryption, callable, &block) @encryptor = nil end |
#default_payload_store_name ⇒ Symbol?
Get the name of the default payload store.
271 272 273 274 275 |
# File 'lib/patient_http/configuration.rb', line 271 def default_payload_store_name @payload_store_mutex.synchronize do @default_payload_store_name end end |
#encryption(callable = nil) {|data| ... } ⇒ Object
Set the encryption callable for encrypting payloads before serialization.
164 165 166 167 |
# File 'lib/patient_http/configuration.rb', line 164 def encryption(callable = nil, &block) @encryption = resolve_callable(:encryption, callable, &block) @encryptor = nil end |
#encryption_key=(keys) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/patient_http/configuration.rb', line 179 def encryption_key=(keys) keys = Array(keys).map(&:to_s).reject(&:empty?) if keys.empty? @encryption = nil @decryption = nil @encryptor = nil return end unless defined?(ActiveSupport::MessageEncryptor) begin require "active_support/key_generator" require "active_support/message_encryptor" rescue LoadError raise ArgumentError.new("ActiveSupport::MessageEncryptor is required for encryption_key") end end key_length = ActiveSupport::MessageEncryptor.key_len key_generator = lambda do |key| ActiveSupport::KeyGenerator.new(key).generate_key(SALT, key_length) end encryptor = ActiveSupport::MessageEncryptor.new(key_generator.call(keys.first), cipher: "aes-256-gcm") keys[1..].each { |key| encryptor.rotate(key_generator.call(key)) } encryption { |data| encryptor.encrypt_and_sign(data) } decryption { |data| encryptor.decrypt_and_verify(data) } @encryptor = nil end |
#encryptor ⇒ Encryptor
Return an Encryptor instance. If encryption and decryption are not set, then this will be an empty Encryptor that returns data unchanged.
214 215 216 |
# File 'lib/patient_http/configuration.rb', line 214 def encryptor @encryptor ||= Encryptor.new(encryption: @encryption, decryption: @decryption) end |
#payload_store(name = nil) ⇒ PayloadStore::Base?
Get a registered payload store by name.
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/patient_http/configuration.rb', line 256 def payload_store(name = nil) @payload_store_mutex.synchronize do if name.nil? return nil unless @default_payload_store_name @payload_stores[@default_payload_store_name] else @payload_stores[name.to_sym] end end end |
#payload_stores ⇒ Hash{Symbol => PayloadStore::Base}
Get all registered payload stores.
280 281 282 283 284 |
# File 'lib/patient_http/configuration.rb', line 280 def payload_stores @payload_store_mutex.synchronize do @payload_stores.dup end end |
#register_payload_store(name, adapter:, **options) ⇒ void
This method returns an undefined value.
Register a payload store for external storage of large payloads.
The name is included in the serialized references to the stored data. Changing it will cause any existing reference to become invalid.
Multiple stores can be registered for migration purposes. The last store registered becomes the default used for new writes. References to other registered stores remain valid for reading.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/patient_http/configuration.rb', line 232 def register_payload_store(name, adapter:, **) name = name.to_sym adapter = adapter.to_sym # Trigger autoload for common adapters ensure_adapter_loaded(adapter) unless PayloadStore::Base.lookup(adapter) raise ArgumentError, "Unknown payload store adapter: #{adapter.inspect}. " \ "Available adapters: #{PayloadStore::Base.registered_adapters.inspect}" end store = PayloadStore::Base.create(adapter, **) @payload_store_mutex.synchronize do @payload_stores[name] = store @default_payload_store_name = name end end |
#to_h ⇒ Hash
Convert to hash for inspection
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/patient_http/configuration.rb', line 288 def to_h { "max_connections" => max_connections, "request_timeout" => request_timeout, "shutdown_timeout" => shutdown_timeout, "logger" => logger, "max_response_size" => max_response_size, "user_agent" => user_agent, "raise_error_responses" => raise_error_responses, "max_redirects" => max_redirects, "connection_pool_size" => connection_pool_size, "connection_timeout" => connection_timeout, "proxy_url" => proxy_url, "retries" => retries, "payload_stores" => payload_stores.keys, "default_payload_store" => default_payload_store_name } end |