faraday_dynamic_timeout 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e0d5c3fe787914d19d470b70182719f5c4863106a5d9fc4854df38e219f1a5a
4
- data.tar.gz: 485e82cfdd0b9098801cffd08fad0426a763ec4a51c746b97a3a553c12281d2d
3
+ metadata.gz: a0f2d0bde754191015cbd03f7cf425170a36d65215a843e881dfb0b27b44482b
4
+ data.tar.gz: 88d59a4fc7dc1ea3addbf44c3043ae4fedea257a8ecf6bfe3acdceba60641fcf
5
5
  SHA512:
6
- metadata.gz: a5025c609b11ae25188e685a15dc44ae55499fea41f16bad4c1cf925b3465942cf42ea77d734eeff8af417f6df7610c8c4dbe5032169dac22100004d1348bda0
7
- data.tar.gz: 3443e7adc22b8bcc9c8b3adc2a25d24187b2f6b284e03429fcaabee8ff80299e976f8fe019cce1ffb3ff55a2020eb6b2be7b31cfe39c8773922327bb86fdc4de
6
+ metadata.gz: 4ab05d2fda3b8dc85a2d54b18abe9b7570e5452de94a5bfbabf373350a54be4046c7fa9c9693f925d518fb0d09a6766b24069e2bd7c726b0020e6fc0960ad7a1
7
+ data.tar.gz: b3e984c48e93af3aae37fac84a60efb18bd61eb8cf4f719b75e2b08187e7ec37bde20f3da87bb6950f3e11ce2175427bf0deaf326c88855b1cb4383771924f88
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ 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.1.0
8
+
9
+ ### Added
10
+
11
+ - Add `before_request` option on middleware to allow making custom changes to the request based on the timeout value being used.
12
+
7
13
  ## 1.0.0
8
14
 
9
15
  ### Added
data/README.md CHANGED
@@ -50,6 +50,8 @@ In this example, the timeout will be set to 8 seconds if there are 5 or fewer re
50
50
 
51
51
  - `: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.
52
52
 
53
+ - `: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.
54
+
53
55
  - `: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.
54
56
 
55
57
  ### Capacity Strategy
@@ -80,6 +82,14 @@ For this example, we will configure the `opensearch` gem with this middleware al
80
82
  # Set up a redis connection to coordinate counting concurrent requests.
81
83
  redis = Redis.new(url: ENV.fetch("REDIS_URL"))
82
84
 
85
+ # Set the query timeout to match the request timeout so the search nodes will stop
86
+ # processing if the request times out.
87
+ set_query_timeout = ->(env, timeout) do
88
+ query_params = (Faraday::Utils.parse_query(env.url.query) || {})
89
+ query_params["timeout"] = "#{timeout}s"
90
+ env.url.query = Faraday::Utils.build_query(query_params)
91
+ end
92
+
83
93
  # Set up a statsd client to report metrics with the DataDog extensions.
84
94
  statsd = Statsd.new(ENV.fetch("STATSD_HOST"), ENV.fetch("STATSD_PORT"))
85
95
 
@@ -102,6 +112,7 @@ client = OpenSearch::Client.new(host: 'localhost', port: '9200') do |faraday|
102
112
  name: "opensearch",
103
113
  redis: redis,
104
114
  filter: ->(env) { env.url.path.end_with?("/_search") },
115
+ before_request: set_payload_timeout,
105
116
  callback: metrics_callback
106
117
  end
107
118
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -27,7 +27,7 @@ module FaradayDynamicTimeout
27
27
  count_request(env.url, redis, buckets, callback) do |request_count|
28
28
  execute_with_timeout(env.url, buckets, request_count, redis) do |timeout|
29
29
  bucket_timeout = timeout
30
- set_timeout(env.request, timeout) if timeout
30
+ set_timeout(env, timeout) if timeout
31
31
 
32
32
  # Resetting the start time to more accurately reflect the time spent in the request.
33
33
  start_time = monotonic_time if callback
@@ -103,11 +103,14 @@ module FaradayDynamicTimeout
103
103
  retval
104
104
  end
105
105
 
106
- def set_timeout(request, timeout)
106
+ def set_timeout(env, timeout)
107
+ request = env.request
107
108
  request.timeout = timeout
108
109
  request.open_timeout = nil
109
110
  request.write_timeout = nil
110
111
  request.read_timeout = nil
112
+
113
+ option(:before_request)&.call(env, timeout)
111
114
  end
112
115
 
113
116
  # Track how many requests are currently being executed only if a callback has been configured.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_dynamic_timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-29 00:00:00.000000000 Z
11
+ date: 2024-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.4.12
96
+ rubygems_version: 3.4.10
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Faraday middleware to dynamically set a request timeout based on the number