Skip to content

Commit

Permalink
Merge pull request #66 from keboola/kacurez-log-retry
Browse files Browse the repository at this point in the history
log when about to retry
  • Loading branch information
kacurez committed Jan 17, 2019
2 parents 220d209 + c3ee88a commit f068ff0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
15 changes: 9 additions & 6 deletions src/keboola/http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
(def fb-requests-count (atom 0))

; retry handler for IOExceptions
(defn- retry-handler [ex try-count http-context]
(Thread/sleep (* 1000 (Math/pow try-count 2)))
(if (> MAX_TRY_COUNT try-count) true false))
(defn retry-handler [ex try-count http-context]
(if (> MAX_TRY_COUNT try-count)
(do
(Thread/sleep (* 1000 (Math/pow try-count 2)))
(runtime/log-strings "retrying request[" (str try-count) "]")
true)
; else return false
false))

(defn check-fb-requests [url]
(if (str/starts-with? url "https://graph.facebook.com")
Expand All @@ -23,13 +28,11 @@
(check-fb-requests url)
(method url (assoc (apply hash-map rest) :retry-handler retry-handler)))


(defn GET [url & rest]
(let [response (apply make-request http/get url rest)]
(record-request response :get url rest)))
; (println "response" response)


(defn POST [url & rest]
(apply make-request http/post url rest))

13 changes: 5 additions & 8 deletions src/keboola/http/recording.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [clojure.walk :refer [postwalk postwalk-demo]]
[cheshire.core :refer [generate-string]]))


(def recording (atom '()))
(def do-recording? (atom false))

Expand All @@ -12,17 +11,16 @@
(defn turn-recording-off [] (reset! do-recording? false))

(def VALID-CHARS
(map char (concat (range 48 58) ; 0-9
(range 66 91) ; A-Z
(range 97 123)))) ; a-z
(map char (concat (range 48 58) ; 0-9
(range 66 91) ; A-Z
(range 97 123)))) ; a-z

(defn random-char []
(nth VALID-CHARS (rand (count VALID-CHARS))))
(nth VALID-CHARS (rand (count VALID-CHARS))))

(defn random-str [length]
(apply str (take length (repeatedly random-char))))


(def keywords-to-anonymize #{:name :story :caption :message :description :title :account_name :campaign_name})
(defn- anonymize-item [item]
(if (and
Expand Down Expand Up @@ -62,8 +60,7 @@
(apply str (mapcat (fn [r]
(let [request (postwalk #(replace-token % token) (:request r))
response (postwalk #(replace-token % token) (:response r))
shaved-response (select-keys response [:status :body])
]
shaved-response (select-keys response [:status :body])]
[(pprint request)
(str "(fn [req]" (pprint shaved-response) ")")]))
@recording)))
Expand Down
7 changes: 7 additions & 0 deletions test/keboola/http/client_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns keboola.http.client-test
(:require [keboola.http.client :as sut]
[clojure.test :refer :all]))

(deftest test-retry-handler
(is (sut/retry-handler nil 1 nil))
(is (not (sut/retry-handler nil 4 nil))))

0 comments on commit f068ff0

Please sign in to comment.