Skip to content

Commit a2dc9e5

Browse files
committed
fix: Retry in POST method
1 parent 55c3bdc commit a2dc9e5

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

lib/trino/client/statement_client.rb

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,46 @@ def init_request(req)
6666

6767
def post_query_request!
6868
uri = "/v1/statement"
69-
response = @faraday.post do |req|
70-
req.url uri
7169

72-
req.body = @query
73-
init_request(req)
74-
end
70+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
71+
attempts = 0
7572

76-
# TODO error handling
77-
if response.status != 200
78-
exception! TrinoHttpError.new(response.status, "Failed to start query: #{response.body} (#{response.status})")
73+
loop do
74+
begin
75+
response = @faraday.post do |req|
76+
req.url uri
77+
req.body = @query
78+
init_request(req)
79+
end
80+
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
81+
# temporally error to retry
82+
response = nil
83+
rescue => e
84+
exception! e
85+
end
86+
87+
if response
88+
if response.status == 200 && !response.body.to_s.empty?
89+
@results_headers = response.headers
90+
@results = decode_model(uri, parse_body(response), @models::QueryResults)
91+
return
92+
end
93+
# retry if 502, 503, 504 according to the trino protocol
94+
unless [502, 503, 504].include?(response.status)
95+
# deterministic error
96+
exception! TrinoHttpError.new(response.status, "Trino API error at #{uri} returned #{response.status}: #{response.body}")
97+
end
98+
end
99+
100+
raise_if_timeout!
101+
102+
attempts += 1
103+
sleep attempts * 0.1
104+
105+
break unless (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) < @retry_timeout && !client_aborted?
79106
end
80107

81-
@results_headers = response.headers
82-
@results = decode_model(uri, parse_body(response), @models::QueryResults)
108+
exception! TrinoHttpError.new(408, "Trino API error due to timeout")
83109
end
84110

85111
private :post_query_request!

0 commit comments

Comments
 (0)