README.md in faraday_dynamic_timeout-1.0.0 vs README.md in faraday_dynamic_timeout-1.1.0
- old
+ new
@@ -48,10 +48,12 @@
- `:redis` - The redis connection to use. This should be a `Redis` object or a `Proc` that yields a `Redis` object. If not provided, a default `Redis` connection will be used (configured using environment variables). If the value is explicitly set to nil, then the middleware will pass through all requests without doing anything.
- `:name` - An optional name for the resource. By default the hostname and port of the request URL will be used to identify the resource. Each resource will report a separate count of concurrent requests and processes. You can group multiple resources from different hosts together with the `:name` option.
+- `:before_request` - An optional callback that will be called before each request is made. The callback can be a `Proc` or any object that responds to `call`. It will be called before the request is made with the `Faraday::Env` object and the timeout being used. You can use this to make changes to the request based on the timeout being used. This can be used, for example, to add the timeout to the request payload.
+
- `:callback` - An optional callback that will be called after each request. The callback can be a `Proc` or any object that responds to `call`. It will be called with a `FaradayDyamicTimeout::RequestInfo` argument. You can use this to log the number of concurrent requests or to report metrics to a monitoring system. This can be very useful for tuning the bucket settings.
### Capacity Strategy
You can use the `FaraadyDynamicTimeout::CapacityStrategy` class to build a bucket configuration based on the current capacity of your application rather than hard coding bucket limits. This can be useful if you have a system that can scale up and down based on load. It works by estimating the total number of threads available in your application and uses that value to calculate bucket limits based on a percentage provided by the `:capacity` option.
@@ -78,10 +80,18 @@
```ruby
# Set up a redis connection to coordinate counting concurrent requests.
redis = Redis.new(url: ENV.fetch("REDIS_URL"))
+# Set the query timeout to match the request timeout so the search nodes will stop
+# processing if the request times out.
+set_query_timeout = ->(env, timeout) do
+ query_params = (Faraday::Utils.parse_query(env.url.query) || {})
+ query_params["timeout"] = "#{timeout}s"
+ env.url.query = Faraday::Utils.build_query(query_params)
+end
+
# Set up a statsd client to report metrics with the DataDog extensions.
statsd = Statsd.new(ENV.fetch("STATSD_HOST"), ENV.fetch("STATSD_PORT"))
metrics_callback = ->(request_info) do
batch = Statsd::Batch.new(statsd)
@@ -100,9 +110,10 @@
{timeout: 0.5, max_requests: 20}
],
name: "opensearch",
redis: redis,
filter: ->(env) { env.url.path.end_with?("/_search") },
+ before_request: set_payload_timeout,
callback: metrics_callback
end
```
## Installation