Skip to content

Commit

Permalink
retry with random delay after "too-many-requests" error
Browse files Browse the repository at this point in the history
  • Loading branch information
vale981 committed Jul 23, 2023
1 parent 07be5aa commit 6970b19
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config-example.lisp
Expand Up @@ -19,6 +19,7 @@
:whitelist (#?|127\.0\.0\.1| #?|141\.30\.[0-9]{1,3}\.[0-9]{1,3}|)
:ip-header "x-real-ip"
:root "https://klausurnoug.at"
:max-retry-extra-delay 1
:discourse (:url "https://physik.protagon.space"
:key "<KEY>"
:username "hiro98"
Expand Down
10 changes: 10 additions & 0 deletions src/main.lisp
Expand Up @@ -39,6 +39,7 @@

(setf (who:html-mode) :html5)
(log:config (or :info (get-config :log-level)))
(setf *random-state* (make-random-state t))

(defvar *static-directory* (merge-pathnames-as-directory
(get-config :application-root)
Expand Down Expand Up @@ -98,6 +99,15 @@
(~> (dexador.error:response-body c)
(json:decode-json-from-string)
(aget :errors)))))
(dexador.error:http-request-too-many-requests (c)
(declare (ignore c))
(handle-discourse
(lambda ()
(progn
(log:warn "Discourse says too many requests, slowing down...")
(sleep (+ (parse-integer (href-default 1 (dexador.error:response-headers c) "retry-after"))
(* (get-config :max-retry-extra-delay) (/ (random 100) 100))))
(funcall call)))))
(error (c)
(discourse-500 (format nil "~A" c)))))

Expand Down

0 comments on commit 6970b19

Please sign in to comment.