@@ -14,10 +14,16 @@ defmodule Ch.Connection do
14
14
def connect ( opts ) do
15
15
with { :ok , conn } <- do_connect ( opts ) do
16
16
handshake = Query . build ( "select 1" )
17
- params = DBConnection.Query . encode ( handshake , _params = [ ] , _opts = [ ] )
18
17
19
- case handle_execute ( handshake , params , _opts = [ ] , conn ) do
20
- { :ok , handshake , responses , conn } ->
18
+ { query_params , extra_headers , body } =
19
+ DBConnection.Query . encode ( handshake , _params = [ ] , _opts = [ ] )
20
+
21
+ path = path ( conn , query_params , opts )
22
+ headers = headers ( conn , extra_headers , opts )
23
+ timeout = HTTP . get_private ( conn , :timeout ) || :timer . seconds ( 15 )
24
+
25
+ case request ( conn , "POST" , path , headers , body , timeout ) do
26
+ { :ok , conn , responses } ->
21
27
case DBConnection.Query . decode ( handshake , responses , _opts = [ ] ) do
22
28
% Result { rows: [ [ 1 ] ] } ->
23
29
{ :ok , conn }
@@ -44,8 +50,9 @@ defmodule Ch.Connection do
44
50
def ping ( conn ) do
45
51
conn = maybe_reconnect ( conn )
46
52
headers = [ { "user-agent" , @ user_agent } ]
53
+ timeout = HTTP . get_private ( conn , :timeout ) || :timer . seconds ( 5 )
47
54
48
- case request ( conn , "GET" , "/ping" , headers , _body = "" , _opts = [ ] ) do
55
+ case request ( conn , "GET" , "/ping" , headers , _body = [ ] , timeout ) do
49
56
{ :ok , conn , _response } -> { :ok , conn }
50
57
{ :error , error , conn } -> { :disconnect , error , conn }
51
58
{ :disconnect , _error , _conn } = disconnect -> disconnect
@@ -88,7 +95,7 @@ defmodule Ch.Connection do
88
95
headers = headers ( conn , extra_headers , opts )
89
96
90
97
with { :ok , conn , _ref } <- send_request ( conn , "POST" , path , headers , body ) ,
91
- { :ok , conn } <- eat_ok_status_and_headers ( conn , timeout ( conn , opts ) ) do
98
+ { :ok , conn } <- eat_ok_status_and_headers ( conn , :infinity ) do
92
99
{ :ok , query , % Result { command: command } , conn }
93
100
end
94
101
end
@@ -148,8 +155,8 @@ defmodule Ch.Connection do
148
155
end
149
156
end
150
157
151
- def handle_fetch ( _query , result , opts , conn ) do
152
- case HTTP . recv ( conn , 0 , timeout ( conn , opts ) ) do
158
+ def handle_fetch ( _query , result , _opts , conn ) do
159
+ case HTTP . recv ( conn , 0 , :infinity ) do
153
160
{ :ok , conn , responses } ->
154
161
{ halt_or_cont ( responses ) , % Result { result | data: extract_data ( responses ) } , conn }
155
162
@@ -194,12 +201,12 @@ defmodule Ch.Connection do
194
201
end
195
202
end
196
203
197
- def handle_execute ( % Query { } = query , { :stream , ref , body } , opts , conn ) do
204
+ def handle_execute ( % Query { } = query , { :stream , ref , body } , _opts , conn ) do
198
205
case HTTP . stream_request_body ( conn , ref , body ) do
199
206
{ :ok , conn } ->
200
207
case body do
201
208
:eof ->
202
- with { :ok , conn , responses } <- receive_full_response ( conn , timeout ( conn , opts ) ) do
209
+ with { :ok , conn , responses } <- receive_full_response ( conn , :infinity ) do
203
210
{ :ok , query , responses , conn }
204
211
end
205
212
@@ -219,7 +226,7 @@ defmodule Ch.Connection do
219
226
path = path ( conn , query_params , opts )
220
227
headers = headers ( conn , extra_headers , opts )
221
228
222
- with { :ok , conn , responses } <- request ( conn , "POST" , path , headers , body , opts ) do
229
+ with { :ok , conn , responses } <- request ( conn , "POST" , path , headers , body , :infinity ) do
223
230
{ :ok , query , responses , conn }
224
231
end
225
232
end
@@ -232,13 +239,13 @@ defmodule Ch.Connection do
232
239
233
240
@ typep response :: Mint.Types . status ( ) | Mint.Types . headers ( ) | binary
234
241
235
- @ spec request ( conn , binary , binary , Mint.Types . headers ( ) , iodata , [ Ch . query_option ( ) ] ) ::
242
+ @ spec request ( conn , binary , binary , Mint.Types . headers ( ) , iodata , timeout ) ::
236
243
{ :ok , conn , [ response ] }
237
244
| { :error , Error . t ( ) , conn }
238
245
| { :disconnect , Mint.Types . error ( ) , conn }
239
- defp request ( conn , method , path , headers , body , opts ) do
246
+ defp request ( conn , method , path , headers , body , timeout ) do
240
247
with { :ok , conn , _ref } <- send_request ( conn , method , path , headers , body ) do
241
- receive_full_response ( conn , timeout ( conn , opts ) )
248
+ receive_full_response ( conn , timeout )
242
249
end
243
250
end
244
251
@@ -275,7 +282,7 @@ defmodule Ch.Connection do
275
282
end
276
283
end
277
284
278
- @ spec recv_all ( conn , [ response ] , timeout ( ) ) ::
285
+ @ spec recv_all ( conn , [ response ] , timeout ) ::
279
286
{ :ok , conn , [ response ] } | { :disconnect , Mint.Types . error ( ) , conn }
280
287
defp recv_all ( conn , acc , timeout ) do
281
288
case HTTP . recv ( conn , 0 , timeout ) do
@@ -302,9 +309,6 @@ defmodule Ch.Connection do
302
309
defp maybe_put_private ( conn , _k , nil ) , do: conn
303
310
defp maybe_put_private ( conn , k , v ) , do: HTTP . put_private ( conn , k , v )
304
311
305
- defp timeout ( conn ) , do: HTTP . get_private ( conn , :timeout )
306
- defp timeout ( conn , opts ) , do: Keyword . get ( opts , :timeout ) || timeout ( conn )
307
-
308
312
defp settings ( conn , opts ) do
309
313
default_settings = HTTP . get_private ( conn , :settings , [ ] )
310
314
opts_settings = Keyword . get ( opts , :settings , [ ] )
@@ -375,7 +379,7 @@ defmodule Ch.Connection do
375
379
with { :ok , conn } <- HTTP . connect ( scheme , address , port , mint_opts ) do
376
380
conn =
377
381
conn
378
- |> HTTP . put_private ( :timeout , opts [ :timeout ] || :timer . seconds ( 15 ) )
382
+ |> maybe_put_private ( :timeout , opts [ :timeout ] )
379
383
|> maybe_put_private ( :database , opts [ :database ] )
380
384
|> maybe_put_private ( :username , opts [ :username ] )
381
385
|> maybe_put_private ( :password , opts [ :password ] )
0 commit comments