Skip to content

Commit e2f4d2a

Browse files
committed
fixes and improvements
1 parent 3f0a96f commit e2f4d2a

1 file changed

Lines changed: 40 additions & 41 deletions

File tree

src/practitest_firecracker/practitest.clj

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
;; utils
1717

1818
(def backoff-timeout "Backoff timeout in seconds" 20)
19+
(def num-attemps "Number of attempts to run" 10)
20+
(def timeout-between-attempts "Timeout in seconds between attempts" 1)
1921
(def run-batch-bucket-size 10)
2022

2123
(def custom-field-cache (atom {}))
@@ -37,53 +39,50 @@
3739
"both `query-params` and `form-params` can't be specified")
3840
(loop [results []
3941
uri uri
40-
attempts 10
42+
attempts num-attemps
4143
params (cond-> {:basic-auth credentials
4244
:throw-exceptions false
4345
:as :json}
4446
query-params (assoc :query-params (conj query-params {:source "firecracker" :firecracker-version fc-version}) )
4547
form-params (assoc :form-params (conj form-params {:source "firecracker" :firecracker-version fc-version}) :content-type :json))]
4648
(if (nil? uri)
47-
results
48-
(let [{:keys [status body]} (method uri params)]
49-
(case status
50-
504 (do
51-
;; load balancer freaking out, lets try again
52-
(Thread/sleep 1000)
53-
(recur results uri (dec attempts) params))
54-
502 (if (> attempts 0)
55-
(do
56-
;; 502 Bad Gateway Error, lets try again
57-
(log/warnf "%s responded with 502 - %s more attempts" uri attempts)
58-
(Thread/sleep 10)
59-
(recur results uri (dec attempts) params))
60-
(throw-api-exception ex-info status body uri))
61-
500 (if (> attempts 0)
62-
(do
63-
;; 500 Bad Gateway Error, lets try again
64-
(log/warnf "%s responded with 500 - %s more attempts" uri attempts)
65-
(Thread/sleep 10)
66-
(recur results uri (dec attempts) params))
67-
(throw-api-exception ex-info status body uri))
68-
503 (if (> attempts 0)
69-
(do
70-
;; 503 Service Unavailable, lets try again
71-
(log/warnf "%s responded with 503 - %s more attempts" uri attempts)
72-
(Thread/sleep 10))
73-
(recur results uri (dec attempts) params))
74-
429 (do
75-
(log/warnf "API rate limit reached, waiting for %s seconds" backoff-timeout)
76-
(Thread/sleep (* backoff-timeout 1000))
77-
(recur results (dec attempts) uri params))
78-
200 (let [data (:data body)]
79-
(recur (vec
80-
(if (sequential? data)
81-
(concat results data)
82-
(conj results data)))
83-
(get-in body [:links :next])
84-
(dec attempts)
85-
(dissoc params :query-params)))
86-
(throw-api-exception ex-info status body uri))))))
49+
results
50+
(let [{:keys [status body]} (method uri params)]
51+
(if (> attempts 0)
52+
(case status
53+
504 (do
54+
;; load balancer freaking out, lets try again
55+
(Thread/sleep (* timeout-between-attempts 1000))
56+
(recur results uri (dec attempts) params))
57+
502 (do
58+
;; 502 Bad Gateway Error, lets try again
59+
(log/warnf "%s responded with 502 - %s more attempts" uri attempts)
60+
(Thread/sleep (* timeout-between-attempts 1000))
61+
(recur results uri (dec attempts) params))
62+
500 (do
63+
;; 500 Bad Gateway Error, lets try again
64+
(log/warnf "%s responded with 500 - %s more attempts" uri attempts)
65+
(Thread/sleep (* timeout-between-attempts 1000))
66+
(recur results uri (dec attempts) params))
67+
503 (do
68+
;; 503 Service Unavailable, lets try again
69+
(log/warnf "%s responded with 503 - %s more attempts" uri attempts)
70+
(Thread/sleep (* timeout-between-attempts 1000))
71+
(recur results uri (dec attempts) params))
72+
429 (do
73+
(log/warnf "API rate limit reached, waiting for %s seconds" backoff-timeout)
74+
(Thread/sleep (* backoff-timeout 1000))
75+
(recur results (dec attempts) uri params))
76+
200 (let [data (:data body)]
77+
(recur (vec
78+
(if (sequential? data)
79+
(concat results data)
80+
(conj results data)))
81+
(get-in body [:links :next])
82+
attempts
83+
(dissoc params :query-params)))
84+
(throw-api-exception ex-info status body uri))
85+
(throw-api-exception ex-info status body uri))))))
8786

8887
;; ===========================================================================
8988
;; constants

0 commit comments

Comments
 (0)