summaryrefslogtreecommitdiff
path: root/haunt/jakob/dynamic/rate-limiter.scm
diff options
context:
space:
mode:
Diffstat (limited to 'haunt/jakob/dynamic/rate-limiter.scm')
-rw-r--r--haunt/jakob/dynamic/rate-limiter.scm22
1 files changed, 7 insertions, 15 deletions
diff --git a/haunt/jakob/dynamic/rate-limiter.scm b/haunt/jakob/dynamic/rate-limiter.scm
index bd39d07..d6ef28c 100644
--- a/haunt/jakob/dynamic/rate-limiter.scm
+++ b/haunt/jakob/dynamic/rate-limiter.scm
@@ -15,6 +15,7 @@
;;; <http://www.gnu.org/licenses/>.
(define-module (jakob dynamic rate-limiter)
+ #:use-module (jakob dynamic errors)
#:use-module (jakob dynamic util)
#:use-module (json)
#:use-module (rnrs conditions)
@@ -34,11 +35,7 @@
(define active-rate-limits (make-hash-table))
-(define (rate-limit-handler-stub request body)
- (values (build-response #:code 429)
- (scm->json-string
- `((success . #f)
- (error . "Your IP address is currently being rate-limited.")))))
+(define (rate-limit-for-endpoint name) 32)
(define (increment-key! hash-table key)
(let ((new-value (if (hash-ref hash-table key)
@@ -46,12 +43,10 @@
1)))
(hash-set! hash-table key new-value)))
-(define (rate-limit-for-endpoint name) 32)
-
(define (rate-limit-wrap proc)
(lambda (request body)
(unless (assoc-ref (request-headers request) 'x-forwarded-for)
- (raise (condition (make-message-condition "X-Forwarded-For header not provided"))))
+ (panic "X-Forwarded-For header not provided"))
(let ((endpoint-name (procedure-name proc))
(requester (chain (assoc-ref (request-headers request) 'x-forwarded-for)
(string-split _ #\,)
@@ -68,10 +63,7 @@
(hash-set! active-rate-limits
requester
(make-requester-state (current-time) (make-hash-table))))
- (if (and (> (hash-ref (requester-state-bins (hash-ref active-rate-limits requester)) endpoint-name)
- (rate-limit-for-endpoint endpoint-name)))
- (values (build-response #:code 429)
- (scm->json-string
- `((success . #f)
- (error . "Your IP address is currently being rate-limited."))))
- (proc request body)))))
+ (when (and (> (hash-ref (requester-state-bins (hash-ref active-rate-limits requester)) endpoint-name)
+ (rate-limit-for-endpoint endpoint-name)))
+ (panic "Your IP address is currently being rate-limited." #:code 429))
+ (proc request body))))