@@ -119,6 +119,7 @@ def initialize(...)
119
119
super
120
120
121
121
@timeout = @connection_options . delete ( :timeout )
122
+ @read_timeout = @connection_options . delete ( :read_timeout )
122
123
123
124
if clients = @connection_options . delete ( :clients )
124
125
@clients = clients . call ( **@connection_options , &@config_block )
@@ -127,6 +128,12 @@ def initialize(...)
127
128
end
128
129
end
129
130
131
+ # @attribute [Numeric | Nil] The maximum time to send a request and wait for a response.
132
+ attr :timeout
133
+
134
+ # @attribute [Numeric | Nil] The maximum time to wait for an individual IO operation.
135
+ attr :read_timeout
136
+
130
137
# Close all clients.
131
138
def close
132
139
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
@@ -181,7 +188,7 @@ def perform_request(env)
181
188
182
189
request = ::Protocol ::HTTP ::Request . new ( endpoint . scheme , endpoint . authority , method , endpoint . path , nil , headers , body )
183
190
184
- with_timeout do
191
+ with_timeout ( env . request . timeout || @timeout ) do
185
192
if env . stream_response?
186
193
response = env . stream_response do |&on_data |
187
194
response = client . call ( request )
@@ -213,7 +220,7 @@ def perform_request(env)
213
220
214
221
def with_client ( env )
215
222
Sync do
216
- endpoint = Endpoint . new ( env . url )
223
+ endpoint = Endpoint . new ( env . url , timeout : @read_timeout )
217
224
218
225
if proxy = env . request . proxy
219
226
proxy_endpoint = Endpoint . new ( proxy . uri )
@@ -229,9 +236,9 @@ def with_client(env)
229
236
end
230
237
end
231
238
232
- def with_timeout ( task : Async ::Task . current )
233
- if @ timeout
234
- task . with_timeout ( @ timeout, ::Faraday ::TimeoutError ) do
239
+ def with_timeout ( timeout = @timeout , task : Async ::Task . current )
240
+ if timeout
241
+ task . with_timeout ( timeout , ::Faraday ::TimeoutError ) do
235
242
yield
236
243
end
237
244
else
0 commit comments