Skip to content

Commit a7e1452

Browse files
committed
improve logging & add tests
1 parent 7cd00d2 commit a7e1452

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/utils/requests.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ function safe_tryparse(args...)
6666
end
6767
end
6868

69+
# convert a numeric number of seconds to a canonicalized object for printing
70+
to_canon(val_seconds::Number) = Dates.canonicalize(Nanosecond(round(Int, val_seconds*1e9)))
71+
6972
"""
7073
github_retry_decision(method::String, resp::Union{HTTP.Response, Nothing}, ex::Union{Exception, Nothing}, exponential_delay::Float64; verbose::Bool=true) -> (should_retry::Bool, sleep_seconds::Float64)
7174
@@ -111,7 +114,7 @@ function github_retry_decision(method::String, resp::Union{HTTP.Response, Nothin
111114
if ex !== nothing
112115
# If there's an exception, check if it's recoverable and if the method is idempotent
113116
if HTTP.RetryRequest.isrecoverable(ex) && HTTP.RetryRequest.isidempotent(method)
114-
verbose && @info "GitHub API exception, retrying in $(round(exponential_delay, digits=1))s" method=method exception=ex
117+
verbose && @info "GitHub API exception, retrying in $(to_canon(exponential_delay))" method=method exception=ex delay_seconds=exponential_delay
115118
return (true, exponential_delay)
116119
end
117120
end
@@ -159,7 +162,7 @@ function github_retry_decision(method::String, resp::Union{HTTP.Response, Nothin
159162
delay_seconds = safe_tryparse(Float64, retry_after)
160163
if delay_seconds !== nothing
161164
delay_seconds = parse(Float64, retry_after)
162-
verbose && @info "$msg, retrying in $(round(delay_seconds, digits=1))s" method=method status=status limit=limit remaining=remaining used=used reset=reset_time resource=resource retry_after=retry_after
165+
verbose && @info "$msg, retrying in $(to_canon(delay_seconds))" method=method status=status limit=limit remaining=remaining used=used reset=reset_time resource=resource retry_after=retry_after delay_seconds=delay_seconds
163166
return (true, delay_seconds)
164167
end
165168

@@ -169,7 +172,7 @@ function github_retry_decision(method::String, resp::Union{HTTP.Response, Nothin
169172
current_time = time()
170173
if reset_timestamp > current_time
171174
delay_seconds = reset_timestamp - current_time + 1.0
172-
verbose && @info "$msg, retrying in $(round(delay_seconds, digits=1))s" method=method status=status limit=limit remaining=remaining used=used reset=reset_time resource=resource retry_after=retry_after
175+
verbose && @info "$msg, retrying in $(to_canon(delay_seconds))" method=method status=status limit=limit remaining=remaining used=used reset=reset_time resource=resource retry_after=retry_after delay_seconds=delay_seconds
173176
return (true, delay_seconds)
174177
end
175178
end
@@ -179,7 +182,8 @@ function github_retry_decision(method::String, resp::Union{HTTP.Response, Nothin
179182
# Fall back to exponential backoff
180183
delay_seconds = is_secondary_rate_limit ? max(60.0, exponential_delay) : exponential_delay
181184

182-
verbose && @info "$msg, retrying in $(round(delay_seconds, digits=1))s" method=method status=status
185+
# Fall back to exponential backoff
186+
verbose && @info "$msg, retrying in $(to_canon(delay_seconds))" method=method status=status delay_seconds=delay_seconds
183187

184188
return (true, delay_seconds)
185189
end

test/retries.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
using Test
22
using HTTP
33
using GitHub
4+
using Dates
45

56
primary_rate_limit_body = Vector{UInt8}("primary rate limit")
67
secondary_rate_limit_body = Vector{UInt8}("secondary rate limit")
78

9+
@testset "to_canon" begin
10+
@test GitHub.to_canon(1) == Second(1)
11+
@test GitHub.to_canon(60) == Minute(1)
12+
@test GitHub.to_canon(90) == Minute(1) + Second(30)
13+
@test GitHub.to_canon(3600) == Hour(1)
14+
@test GitHub.to_canon(1.5) == Second(1) + Millisecond(500)
15+
@test GitHub.to_canon(0) == Second(0)
16+
17+
# Test verbose logging with canonicalized time
18+
resp = HTTP.Response(429, ["retry-after" => "90"])
19+
@test_logs (:info, r"retrying in 1 minute, 30 seconds") GitHub.github_retry_decision("GET", resp, nothing, 2.0; verbose=true)
20+
end
21+
822
@testset "github_retry_decision" begin
923

1024
@testset "HTTP.jl recoverable exceptions" begin

0 commit comments

Comments
 (0)