patient_http 1.1.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +116 -3
- data/VERSION +1 -1
- data/lib/patient_http/client.rb +7 -10
- data/lib/patient_http/configuration.rb +56 -1
- data/lib/patient_http/http_headers.rb +7 -0
- data/lib/patient_http/inline_task_handler.rb +30 -0
- data/lib/patient_http/lifecycle_manager.rb +20 -8
- data/lib/patient_http/outgoing_request.rb +72 -0
- data/lib/patient_http/payload.rb +1 -1
- data/lib/patient_http/payload_store/redis_store.rb +4 -1
- data/lib/patient_http/processor.rb +299 -113
- data/lib/patient_http/redirect_error.rb +5 -0
- data/lib/patient_http/request.rb +26 -3
- data/lib/patient_http/request_error.rb +1 -1
- data/lib/patient_http/request_helper.rb +16 -6
- data/lib/patient_http/request_preparer.rb +52 -0
- data/lib/patient_http/request_task.rb +16 -7
- data/lib/patient_http/request_template.rb +9 -3
- data/lib/patient_http/response.rb +3 -0
- data/lib/patient_http/response_reader.rb +16 -4
- data/lib/patient_http/synchronous_executor.rb +53 -45
- data/lib/patient_http.rb +182 -2
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ba7400987a1a1483e88f8c0e3c3fb3579544a02f5f3bdb6da7752f5864792fcb
|
|
4
|
+
data.tar.gz: cc1ab11246151f2b9843e81c730653e922a34db6f4a57600267637fbe72d7f49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e021b76930cc0c1d6c0741723d2db084947f04b8daee1564bc1b04d56d24399443dc6567fe153a99795d5c322810ab56f9cd218f1b8ddafb3a127785352f0bb7
|
|
7
|
+
data.tar.gz: 5d920b85e033e9fbfcd551ca2f7702d260da8ddbea04f105173e591c07fab7b66df2c6d6a85303463fa6faa6d2c8e6357296c317e6ccec8e7599dbbd97b1f842
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,37 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## 1.3.0
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `PatientHttp.inline!` registers a request handler that executes requests inline (synchronously, in-process) through `SynchronousExecutor`, for consoles, tests, and development environments with no job-system integration. `PatientHttp.inline?` checks whether the inline handler is currently registered, and `PatientHttp.execute_inline` executes a single request inline without registering a handler.
|
|
12
|
+
- `PatientHttp.register_secret` registers named secrets at the module level, independent of any `Configuration`. Module-level secrets are applied to the new `PatientHttp.default_configuration` (immediately if set, or when it is set later), making registration order between application code and integration gem configuration irrelevant. `PatientHttp.secret_registered?` checks whether a secret is registered at the module level or on the default configuration.
|
|
13
|
+
- `PatientHttp::RequestHelper`'s `request_template` and `async_request` now accept `preprocessors:`, matching `PatientHttp.request` and `RequestTemplate`.
|
|
14
|
+
- `PatientHttp.handler_registered?` checks whether a request handler is registered.
|
|
15
|
+
|
|
16
|
+
## 1.2.0
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- Request preprocessors for modifying the outgoing request just before it is sent — most usefully, to sign requests (e.g. AWS SigV4 signatures that must set multiple headers computed over the final request). Register a preprocessor on the `Configuration` with `register_preprocessor(name)` and attach it to a request with `preprocessors: name`. The request serializes only the preprocessor name; the callable (and any credentials it uses) stays on the processor side. Preprocessors receive an `OutgoingRequest` — a view of the request after secret resolution with read access to the method, URL, and body, mutable headers, and an `add_param` method for appending query parameters. On redirects, preprocessors re-run against each redirect URL and are dropped on cross-origin hops, consistent with sensitive header stripping.
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
24
|
+
- `SynchronousExecutor` no longer resolves secret query params twice per request, which previously invoked callable secrets twice.
|
|
25
|
+
- Graceful shutdown now works as documented: requests that complete while the processor is stopping have their responses delivered instead of being discarded and retried, and `Processor#stop` returns as soon as in-flight requests finish rather than always blocking for the full `shutdown_timeout`.
|
|
26
|
+
- `Processor#stop` performs a second re-enqueue pass after the reactor thread exits, closing a race where a task dequeued but not yet tracked at shutdown could be silently lost.
|
|
27
|
+
- Task results are now claimed atomically at shutdown so a completing request can no longer trigger both its completion callback and a `TaskHandler#retry` for the same task.
|
|
28
|
+
- `Processor#start` on a draining processor is now a no-op instead of spawning a second reactor thread that shared the queue and connection pools with the draining one.
|
|
29
|
+
- The processor can no longer end up in a running state with a dead reactor thread when the reactor fails during startup.
|
|
30
|
+
- Processor observers are now invoked outside of internal locks, so an observer callback can safely call back into processor methods such as `total_count` without raising a recursive locking error.
|
|
31
|
+
- `SynchronousExecutor` no longer invokes the error callback twice when a redirect fails with too many redirects or a redirect loop.
|
|
32
|
+
- `HttpHeaders#merge` no longer mutates the receiver. This also fixes `RequestTemplate` permanently absorbing per-request headers into its defaults, which could leak headers (including credentials) across requests built from the same template.
|
|
33
|
+
- `Request` now copies headers passed to its constructor instead of holding (and potentially mutating) the caller's `HttpHeaders` instance.
|
|
34
|
+
- `Errno::ETIMEDOUT` is now treated as a connection error, evicting the pooled client and classifying the `RequestError` as `:connection`.
|
|
35
|
+
- `RedirectError.load` validates that the serialized error class is a `RedirectError` subclass instead of instantiating an arbitrary class name from the payload.
|
|
36
|
+
- Fixed the Redis payload store documentation to use the `redis` gem interface (`Redis.new`); the previously documented `RedisClient` does not respond to the methods the store calls.
|
|
37
|
+
|
|
7
38
|
## 1.1.2
|
|
8
39
|
|
|
9
40
|
### Added
|
data/README.md
CHANGED
|
@@ -194,6 +194,30 @@ PatientHttp.get(
|
|
|
194
194
|
|
|
195
195
|
If you are using the [patient_http-sidekiq](https://github.com/bdurand/patient_http-sidekiq) gem or the [patient_http-solid_queue](https://github.com/bdurand/patient_http-solid_queue) gem, the appropriate handler will automatically be registered for you.
|
|
196
196
|
|
|
197
|
+
### Inline Execution
|
|
198
|
+
|
|
199
|
+
For consoles, tests, and development environments where no job system is configured, you can register a handler that executes requests inline — synchronously, in-process — instead of dispatching them to a queue:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
PatientHttp.inline!
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Now every request made through the `PatientHttp` interface (or the `RequestHelper` mixin) runs immediately through the full request lifecycle (timeouts, redirects, error wrapping) and invokes its callback on the calling thread before returning. Callbacks can make further requests; those execute inline as well.
|
|
206
|
+
|
|
207
|
+
```ruby
|
|
208
|
+
PatientHttp.inline!
|
|
209
|
+
PatientHttp.get("https://api.example.com/users/123", callback: FetchUserCallback)
|
|
210
|
+
# FetchUserCallback#on_complete has already been invoked by this point
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Inline requests run against `PatientHttp.default_configuration` by default (or a lazily created default configuration that includes any secrets registered with `PatientHttp.register_secret` — see [Secrets](#secrets)). You can also pass an explicit configuration:
|
|
214
|
+
|
|
215
|
+
```ruby
|
|
216
|
+
PatientHttp.inline!(config: PatientHttp::Configuration.new(raise_error_responses: true))
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Use `PatientHttp.inline?` to check whether the inline handler is the currently registered handler. To execute a single request inline without registering a handler, use `PatientHttp.execute_inline(request:, callback:)`.
|
|
220
|
+
|
|
197
221
|
### RequestHelper Mixin
|
|
198
222
|
|
|
199
223
|
Use `PatientHttp::RequestHelper` when you want a simple API for creating and dispatching async HTTP requests directly from your class.
|
|
@@ -281,6 +305,8 @@ error = PatientHttp::HttpError.load(json_data)
|
|
|
281
305
|
|
|
282
306
|
The `Response` object includes the HTTP status code, headers, body, and callback arguments. Error objects (`HttpError`, `RedirectError`, `RequestError`) include the error message, context about the request, and callback arguments.
|
|
283
307
|
|
|
308
|
+
Response headers are case insensitive. Headers that appear multiple times in the response (such as `set-cookie`) are flattened into a single joined string value.
|
|
309
|
+
|
|
284
310
|
Response bodies are automatically encoded for JSON serialization. Binary content is Base64 encoded, and large text content is gzipped and then Base64 encoded to reduce payload size. Decoding is handled transparently when you access the `body` or `json` methods on the `Response` object.
|
|
285
311
|
|
|
286
312
|
### Payload Stores
|
|
@@ -321,10 +347,10 @@ config.register_payload_store(:files, adapter: :file, directory: "/tmp/payloads"
|
|
|
321
347
|
|
|
322
348
|
#### Redis Store
|
|
323
349
|
|
|
324
|
-
For production with shared state across processes:
|
|
350
|
+
For production with shared state across processes (requires the `redis` gem; the client must respond to `set`, `get`, `del`, and `exists`):
|
|
325
351
|
|
|
326
352
|
```ruby
|
|
327
|
-
redis =
|
|
353
|
+
redis = Redis.new(url: ENV["REDIS_URL"])
|
|
328
354
|
config.register_payload_store(:redis, adapter: :redis, redis: redis, ttl: 86400)
|
|
329
355
|
```
|
|
330
356
|
|
|
@@ -501,7 +527,24 @@ config.register_secret(:authorization, "Bearer #{ENV['API_TOKEN']}") # static va
|
|
|
501
527
|
config.register_secret(:api_key) { ENV["MY_API_KEY"] } # lazy block
|
|
502
528
|
```
|
|
503
529
|
|
|
504
|
-
|
|
530
|
+
If a secret is not found when resolving a request, a `PatientHttp::SecretManager::SecretNotFoundError` is raised, which surfaces through the normal request error path.
|
|
531
|
+
|
|
532
|
+
#### Module-level registration
|
|
533
|
+
|
|
534
|
+
If the `Configuration` is owned by an integration gem (patient_http-sidekiq, patient_http-solid_queue), your application code may not have a convenient reference to it — or may load before it exists. In that case, register secrets at the module level instead:
|
|
535
|
+
|
|
536
|
+
```ruby
|
|
537
|
+
PatientHttp.register_secret(:authorization, "Bearer #{ENV['API_TOKEN']}")
|
|
538
|
+
PatientHttp.register_secret(:api_key) { ENV["MY_API_KEY"] }
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
Module-level secrets are applied to `PatientHttp.default_configuration` — immediately if one is already set, or as soon as one is set later — so registration order between your application code and the integration gem's configuration does not matter. Integration gems set the default configuration at the end of their configure step; you can also set it yourself:
|
|
542
|
+
|
|
543
|
+
```ruby
|
|
544
|
+
PatientHttp.default_configuration = config
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
Use `PatientHttp.secret_registered?(name)` to check whether a secret is available, either at the module level or on the default configuration.
|
|
505
548
|
|
|
506
549
|
### Referencing secrets when building a request
|
|
507
550
|
|
|
@@ -518,6 +561,76 @@ PatientHttp.get(
|
|
|
518
561
|
|
|
519
562
|
The request serializes the secret header as `{"$secret" => "api_token"}` and keeps the secret query parameter out of the URL (non-secret params like `page` are still folded into the URL as usual). The processor dereferences both just before sending: the header is set to its resolved value and the resolved query parameter is appended to the URL.
|
|
520
563
|
|
|
564
|
+
## Request Preprocessors
|
|
565
|
+
|
|
566
|
+
Preprocessors let you modify a request just before it is sent — most usefully, to sign it. Signing schemes like AWS SigV4 need to compute values over the final outgoing request (method, URL, headers, body) and set multiple headers, which cannot be expressed as a static header value at build time.
|
|
567
|
+
|
|
568
|
+
Like secrets, preprocessors are registered on the `Configuration` and referenced from requests by name only. The serialized request carries just the name, so the signing logic and its credentials live on the processor side and are never written to the job queue.
|
|
569
|
+
|
|
570
|
+
### Defining preprocessors
|
|
571
|
+
|
|
572
|
+
Register a named preprocessor as a block or callable taking a single argument:
|
|
573
|
+
|
|
574
|
+
```ruby
|
|
575
|
+
config = PatientHttp::Configuration.new
|
|
576
|
+
config.register_preprocessor(:aws_sigv4) do |request|
|
|
577
|
+
signer = Aws::Sigv4::Signer.new(
|
|
578
|
+
service: "execute-api",
|
|
579
|
+
region: "us-east-1",
|
|
580
|
+
credentials_provider: Aws::CredentialProviderChain.new.resolve
|
|
581
|
+
)
|
|
582
|
+
signature = signer.sign_request(
|
|
583
|
+
http_method: request.http_method.to_s.upcase,
|
|
584
|
+
url: request.url,
|
|
585
|
+
headers: request.headers.to_h,
|
|
586
|
+
body: request.body.to_s
|
|
587
|
+
)
|
|
588
|
+
signature.headers.each { |name, value| request.headers[name] = value }
|
|
589
|
+
end
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
The argument is a `PatientHttp::OutgoingRequest` — a view of the request as it is about to be sent, after all secret references have been resolved and the `x-request-id` and default `User-Agent` headers have been set. It exposes:
|
|
593
|
+
|
|
594
|
+
- `http_method`, `url`, and `body` (read-only; the URL includes any resolved secret query params)
|
|
595
|
+
- `headers` — mutable, case-insensitive headers
|
|
596
|
+
- `add_param(name, value)` — appends a query parameter to the URL, for signed-query-param schemes
|
|
597
|
+
|
|
598
|
+
### Attaching preprocessors to a request
|
|
599
|
+
|
|
600
|
+
Reference registered preprocessors by name when building a request:
|
|
601
|
+
|
|
602
|
+
```ruby
|
|
603
|
+
PatientHttp.post(
|
|
604
|
+
"https://api.example.com/data",
|
|
605
|
+
callback: MyCallback,
|
|
606
|
+
json: {value: 1},
|
|
607
|
+
preprocessors: :aws_sigv4
|
|
608
|
+
)
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
Multiple preprocessors can be given as an array; they run in order, each seeing the changes made by the ones before it. `RequestTemplate` and the `RequestHelper` mixin's `request_template` also accept `preprocessors:` as a template-wide default, and the mixin's `async_*` helpers accept `preprocessors:` per request.
|
|
612
|
+
|
|
613
|
+
If a request references a preprocessor name that is not registered, a `PatientHttp::RequestPreparer::PreprocessorNotFoundError` is raised, which surfaces through the normal request error path.
|
|
614
|
+
|
|
615
|
+
When redirects are followed, preprocessors are re-run against each redirect URL so signatures stay valid. On cross-origin redirects they are dropped entirely, consistent with the stripping of `Authorization` and `Cookie` headers, so signed credentials are never sent to an unexpected origin.
|
|
616
|
+
|
|
617
|
+
## Troubleshooting
|
|
618
|
+
|
|
619
|
+
### Warning: `ThreadError: Attempt to unlock a mutex which is not locked`
|
|
620
|
+
|
|
621
|
+
On some Ruby versions you may see a warning like this in your logs:
|
|
622
|
+
|
|
623
|
+
```
|
|
624
|
+
warn: Async::Task: Async::Pool::Controller Gardener [...]
|
|
625
|
+
| Task may have ended with unhandled exception.
|
|
626
|
+
| ThreadError: Attempt to unlock a mutex which is not locked
|
|
627
|
+
| → .../async-pool-x.y.z/lib/async/pool/controller.rb:132 in `synchronize'
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
This is caused by [Ruby bug #20907](https://bugs.ruby-lang.org/issues/20907) (see also [socketry/async#424](https://github.com/socketry/async/issues/424)): under the fiber scheduler, a fiber interrupted while waiting on a `ConditionVariable` fails to re-acquire its mutex before unwinding, raising a spurious `ThreadError`. It appears whenever a pooled HTTP client is closed while its connection pool's background "gardener" task is idle — for example when a connection is evicted after a connection error, when the least recently used client is evicted because the pool is full, or when the processor shuts down.
|
|
631
|
+
|
|
632
|
+
The warning is harmless — connections are still closed correctly; only the log noise is wrong. The fix is to upgrade Ruby: the bug is fixed in Ruby 3.2.7+, 3.3.7+, and 3.4+.
|
|
633
|
+
|
|
521
634
|
## Configuration
|
|
522
635
|
|
|
523
636
|
```ruby
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.
|
|
1
|
+
1.3.0
|
data/lib/patient_http/client.rb
CHANGED
|
@@ -12,6 +12,7 @@ module PatientHttp
|
|
|
12
12
|
protocol: config.protocol
|
|
13
13
|
)
|
|
14
14
|
@response_reader = ResponseReader.new(@processor)
|
|
15
|
+
@request_preparer = RequestPreparer.new(config)
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
# Make an asynchronous HTTP request.
|
|
@@ -23,13 +24,16 @@ module PatientHttp
|
|
|
23
24
|
async_response = nil
|
|
24
25
|
|
|
25
26
|
begin
|
|
26
|
-
|
|
27
|
-
url =
|
|
27
|
+
outgoing = @request_preparer.prepare(request, request_id)
|
|
28
|
+
url = outgoing.url
|
|
29
|
+
headers = outgoing.headers.to_h
|
|
28
30
|
body = Protocol::HTTP::Body::Buffered.wrap([request.body.to_s]) if request.body
|
|
29
31
|
timeout = request.timeout || config.request_timeout
|
|
30
32
|
|
|
31
33
|
Async::Task.current.with_timeout(timeout) do
|
|
32
34
|
async_response = @client_pool.request(request.http_method, url, headers, body)
|
|
35
|
+
# Note: headers that appear multiple times (e.g. set-cookie) are
|
|
36
|
+
# flattened to a single joined string value.
|
|
33
37
|
headers_hash = async_response.headers.to_h.transform_values(&:to_s)
|
|
34
38
|
body = @response_reader.read_body(async_response, headers_hash)
|
|
35
39
|
|
|
@@ -66,18 +70,11 @@ module PatientHttp
|
|
|
66
70
|
def connection_error?(exception)
|
|
67
71
|
case exception
|
|
68
72
|
when Async::TimeoutError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE,
|
|
69
|
-
Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError, IOError
|
|
73
|
+
Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError, IOError
|
|
70
74
|
true
|
|
71
75
|
else
|
|
72
76
|
false
|
|
73
77
|
end
|
|
74
78
|
end
|
|
75
|
-
|
|
76
|
-
def request_headers(request, request_id)
|
|
77
|
-
headers = config.secret_manager.resolve_headers(request.headers.to_h)
|
|
78
|
-
headers["x-request-id"] = request_id
|
|
79
|
-
headers["user-agent"] ||= config.user_agent if config.user_agent
|
|
80
|
-
headers
|
|
81
|
-
end
|
|
82
79
|
end
|
|
83
80
|
end
|
|
@@ -94,6 +94,9 @@ module PatientHttp
|
|
|
94
94
|
@secrets = {}
|
|
95
95
|
@secret_manager = SecretManager.new
|
|
96
96
|
|
|
97
|
+
# Initialize preprocessor registry
|
|
98
|
+
@preprocessors = {}
|
|
99
|
+
|
|
97
100
|
@encryptor = nil
|
|
98
101
|
|
|
99
102
|
self.max_connections = max_connections
|
|
@@ -271,6 +274,43 @@ module PatientHttp
|
|
|
271
274
|
end
|
|
272
275
|
end
|
|
273
276
|
|
|
277
|
+
# Register a named preprocessor that can be attached to requests to modify them
|
|
278
|
+
# just before they are sent -- for example, to sign requests.
|
|
279
|
+
#
|
|
280
|
+
# The preprocessor can be provided as a callable or a block taking a single
|
|
281
|
+
# argument. When a request that references the preprocessor is sent, it is
|
|
282
|
+
# invoked with an {OutgoingRequest} after secret references have been resolved
|
|
283
|
+
# and the x-request-id and default user-agent headers have been set. It can
|
|
284
|
+
# change the request headers and append query parameters.
|
|
285
|
+
#
|
|
286
|
+
# Requests reference preprocessors by name only, so the callable (and any
|
|
287
|
+
# credentials it uses) stays on the processor side and is never serialized.
|
|
288
|
+
#
|
|
289
|
+
# @param name [String, Symbol] the preprocessor name
|
|
290
|
+
# @param callable [#call, nil] object invoked with the outgoing request (omit when providing a block)
|
|
291
|
+
# @yield [outgoing_request] a block invoked with the outgoing request (omit when providing a callable)
|
|
292
|
+
# @raise [ArgumentError] if neither or both of callable and block are provided, or
|
|
293
|
+
# if the preprocessor cannot be called with a single argument
|
|
294
|
+
# @return [void]
|
|
295
|
+
def register_preprocessor(name, callable = nil, &block)
|
|
296
|
+
preprocessor = resolve_callable(:preprocessor, callable, &block)
|
|
297
|
+
raise ArgumentError.new("register_preprocessor requires a callable or a block") if preprocessor.nil?
|
|
298
|
+
|
|
299
|
+
validate_preprocessor_parameters!(preprocessor)
|
|
300
|
+
|
|
301
|
+
@mutex.synchronize do
|
|
302
|
+
@preprocessors = @preprocessors.merge(name.to_s => preprocessor)
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# Get a registered preprocessor by name.
|
|
307
|
+
#
|
|
308
|
+
# @param name [String, Symbol] the preprocessor name
|
|
309
|
+
# @return [#call, nil] the preprocessor or nil if not registered
|
|
310
|
+
def preprocessor(name)
|
|
311
|
+
@preprocessors[name.to_s]
|
|
312
|
+
end
|
|
313
|
+
|
|
274
314
|
# Register a payload store for external storage of large payloads.
|
|
275
315
|
#
|
|
276
316
|
# The name is included in the serialized references to the stored data.
|
|
@@ -350,7 +390,8 @@ module PatientHttp
|
|
|
350
390
|
"protocol" => protocol,
|
|
351
391
|
"payload_stores" => payload_stores.keys,
|
|
352
392
|
"default_payload_store" => default_payload_store_name,
|
|
353
|
-
"secrets" => @mutex.synchronize { @secrets.keys }
|
|
393
|
+
"secrets" => @mutex.synchronize { @secrets.keys },
|
|
394
|
+
"preprocessors" => @mutex.synchronize { @preprocessors.keys }
|
|
354
395
|
}
|
|
355
396
|
end
|
|
356
397
|
|
|
@@ -368,6 +409,20 @@ module PatientHttp
|
|
|
368
409
|
callable || block
|
|
369
410
|
end
|
|
370
411
|
|
|
412
|
+
# Validate that a preprocessor can be invoked with a single positional argument.
|
|
413
|
+
def validate_preprocessor_parameters!(preprocessor)
|
|
414
|
+
method_obj = preprocessor.is_a?(Proc) ? preprocessor : preprocessor.method(:call)
|
|
415
|
+
parameters = method_obj.parameters
|
|
416
|
+
|
|
417
|
+
positional = parameters.count { |type, _| %i[req opt rest].include?(type) }
|
|
418
|
+
required = parameters.count { |type, _| type == :req }
|
|
419
|
+
required_keywords = parameters.count { |type, _| type == :keyreq }
|
|
420
|
+
|
|
421
|
+
if positional.zero? || required > 1 || required_keywords.positive?
|
|
422
|
+
raise ArgumentError.new("preprocessor must accept a single argument")
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
|
|
371
426
|
def validate_positive(attribute, value)
|
|
372
427
|
return if value.is_a?(Numeric) && value > 0
|
|
373
428
|
|
|
@@ -88,6 +88,13 @@ module PatientHttp
|
|
|
88
88
|
@headers.include?(name.to_s.downcase)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
+
# Ensure copies do not share the underlying storage so that mutating a
|
|
92
|
+
# copy (for example, via #merge) does not modify the original.
|
|
93
|
+
def initialize_copy(other)
|
|
94
|
+
super
|
|
95
|
+
@headers = @headers.dup
|
|
96
|
+
end
|
|
97
|
+
|
|
91
98
|
def eql?(other)
|
|
92
99
|
other.is_a?(HttpHeaders) && @headers.eql?(other.to_h)
|
|
93
100
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PatientHttp
|
|
4
|
+
# No-op task handler used for inline request execution.
|
|
5
|
+
#
|
|
6
|
+
# The {SynchronousExecutor} invokes the user callback directly, so the
|
|
7
|
+
# completion and error hooks here are never exercised in practice; they are
|
|
8
|
+
# defined as no-ops to satisfy the {TaskHandler} contract. Inline requests
|
|
9
|
+
# have no job queue, so retrying is not supported.
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
class InlineTaskHandler < TaskHandler
|
|
13
|
+
# @param response [Response] the HTTP response object
|
|
14
|
+
# @param callback [String] callback class name
|
|
15
|
+
# @return [void]
|
|
16
|
+
def on_complete(response, callback)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @param error [Error] the error object
|
|
20
|
+
# @param callback [String] callback class name
|
|
21
|
+
# @return [void]
|
|
22
|
+
def on_error(error, callback)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @raise [NotImplementedError] inline requests cannot be retried
|
|
26
|
+
def retry
|
|
27
|
+
raise NotImplementedError, "Inline requests cannot be retried"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -12,7 +12,7 @@ module PatientHttp
|
|
|
12
12
|
STATES = %i[stopped starting running draining stopping].freeze
|
|
13
13
|
|
|
14
14
|
# Polling interval during wait operations
|
|
15
|
-
POLL_INTERVAL = 0.
|
|
15
|
+
POLL_INTERVAL = 0.01
|
|
16
16
|
|
|
17
17
|
# Initialize the lifecycle manager.
|
|
18
18
|
#
|
|
@@ -66,12 +66,14 @@ module PatientHttp
|
|
|
66
66
|
state == :stopping
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
# Transition to starting state.
|
|
69
|
+
# Transition to starting state. The processor can only be started from
|
|
70
|
+
# the stopped state; in particular, starting a draining processor would
|
|
71
|
+
# spawn a second reactor alongside the one still finishing its drain.
|
|
70
72
|
#
|
|
71
73
|
# @return [Boolean] true if transition was successful
|
|
72
74
|
def start!
|
|
73
75
|
@lock.synchronize do
|
|
74
|
-
return false
|
|
76
|
+
return false unless stopped?
|
|
75
77
|
|
|
76
78
|
@state.set(:starting)
|
|
77
79
|
@shutdown_barrier.reset
|
|
@@ -83,9 +85,18 @@ module PatientHttp
|
|
|
83
85
|
|
|
84
86
|
# Transition to running state.
|
|
85
87
|
#
|
|
86
|
-
#
|
|
88
|
+
# The transition only occurs from the starting state so that a reactor
|
|
89
|
+
# that already failed and transitioned to stopped is not overwritten.
|
|
90
|
+
#
|
|
91
|
+
# @return [Boolean] true if transition was successful
|
|
87
92
|
def running!
|
|
88
|
-
@
|
|
93
|
+
@lock.synchronize do
|
|
94
|
+
return false unless starting?
|
|
95
|
+
|
|
96
|
+
@state.set(:running)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
true
|
|
89
100
|
end
|
|
90
101
|
|
|
91
102
|
# Transition to draining state.
|
|
@@ -136,9 +147,10 @@ module PatientHttp
|
|
|
136
147
|
|
|
137
148
|
# Wait for the reactor to be ready.
|
|
138
149
|
#
|
|
139
|
-
# @
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
# @param timeout [Numeric, nil] maximum time to wait in seconds (nil waits forever)
|
|
151
|
+
# @return [Boolean] true if the reactor is ready, false if the timeout was reached
|
|
152
|
+
def wait_for_reactor(timeout: nil)
|
|
153
|
+
@reactor_ready.wait(timeout)
|
|
142
154
|
end
|
|
143
155
|
|
|
144
156
|
# Check if shutdown has been signaled.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PatientHttp
|
|
4
|
+
# A mutable view of a request as it is about to be sent, after secret references
|
|
5
|
+
# have been resolved and the send-time headers (x-request-id and the default
|
|
6
|
+
# user-agent) have been set.
|
|
7
|
+
#
|
|
8
|
+
# Preprocessors attached to a request receive this object and can modify the
|
|
9
|
+
# headers or append query parameters before the request goes out -- for example,
|
|
10
|
+
# to sign the request. The HTTP method, URL, and body are read-only; headers can
|
|
11
|
+
# be changed in place and query parameters appended with {#add_param}.
|
|
12
|
+
#
|
|
13
|
+
# @see Configuration#register_preprocessor
|
|
14
|
+
class OutgoingRequest
|
|
15
|
+
# @return [Symbol] HTTP method (:get, :post, :put, :patch, :delete)
|
|
16
|
+
attr_reader :http_method
|
|
17
|
+
|
|
18
|
+
# @return [String] the request URL with any secret query params already resolved
|
|
19
|
+
attr_reader :url
|
|
20
|
+
|
|
21
|
+
# @return [String, nil] the request body
|
|
22
|
+
attr_reader :body
|
|
23
|
+
|
|
24
|
+
# @return [HttpHeaders] mutable, case-insensitive request headers
|
|
25
|
+
attr_reader :headers
|
|
26
|
+
|
|
27
|
+
# Initialize a new OutgoingRequest.
|
|
28
|
+
#
|
|
29
|
+
# @param http_method [Symbol] the HTTP method
|
|
30
|
+
# @param url [String] the resolved request URL
|
|
31
|
+
# @param headers [HttpHeaders] the resolved request headers
|
|
32
|
+
# @param body [String, nil] the request body
|
|
33
|
+
def initialize(http_method:, url:, headers:, body:)
|
|
34
|
+
@http_method = http_method
|
|
35
|
+
@url = url.to_s
|
|
36
|
+
@headers = headers
|
|
37
|
+
@body = body
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Append a query parameter to the request URL.
|
|
41
|
+
#
|
|
42
|
+
# @param name [String, Symbol] the parameter name
|
|
43
|
+
# @param value [Object] the parameter value
|
|
44
|
+
# @return [String] the updated URL
|
|
45
|
+
def add_param(name, value)
|
|
46
|
+
serialized_param = URI.encode_www_form([[name.to_s, value]])
|
|
47
|
+
uri = URI(@url)
|
|
48
|
+
uri.query = [uri.query, serialized_param].compact.reject(&:empty?).join("&")
|
|
49
|
+
@url = uri.to_s
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Inspect the outgoing request. Header values, the query string, and the body
|
|
53
|
+
# are not shown since they may contain resolved secrets.
|
|
54
|
+
#
|
|
55
|
+
# @return [String]
|
|
56
|
+
def inspect
|
|
57
|
+
"#<#{self.class.name} #{http_method.to_s.upcase} #{redacted_url} headers=#{headers.to_h.keys.inspect}>"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def redacted_url
|
|
63
|
+
uri = URI(@url)
|
|
64
|
+
uri.query = nil
|
|
65
|
+
uri.user = nil
|
|
66
|
+
uri.password = nil
|
|
67
|
+
uri.to_s
|
|
68
|
+
rescue URI::InvalidURIError
|
|
69
|
+
"<invalid url>"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
data/lib/patient_http/payload.rb
CHANGED
|
@@ -10,8 +10,11 @@ module PatientHttp
|
|
|
10
10
|
#
|
|
11
11
|
# Thread-safe: Redis clients handle their own thread safety.
|
|
12
12
|
#
|
|
13
|
+
# The client must respond to `set`, `get`, `del`, and `exists` (the
|
|
14
|
+
# interface provided by the `redis` gem).
|
|
15
|
+
#
|
|
13
16
|
# @example Configuration with direct Redis client
|
|
14
|
-
# redis =
|
|
17
|
+
# redis = Redis.new(url: ENV["REDIS_URL"])
|
|
15
18
|
# config.register_payload_store(:redis, adapter: :redis, redis: redis, ttl: 86400)
|
|
16
19
|
class RedisStore < Base
|
|
17
20
|
Base.register :redis, self
|