File tree Expand file tree Collapse file tree 1 file changed +16
-4
lines changed
Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use worker::*;
44#[ durable_object]
55pub struct RateLimiter {
66 state : State ,
7+ #[ allow( unused) ]
78 block_until : DateTime < Utc > ,
89}
910
@@ -18,21 +19,32 @@ impl DurableObject for RateLimiter {
1819
1920 async fn fetch ( & mut self , _req : Request ) -> Result < Response > {
2021 let now = Utc :: now ( ) ;
22+ let block_until = self
23+ . state
24+ . storage ( )
25+ . get ( "block_until" )
26+ . await
27+ . map ( |v| DateTime :: < Utc > :: from_timestamp ( v, 0 ) . unwrap_or_default ( ) )
28+ . unwrap_or ( Utc :: now ( ) ) ;
2129 console_log ! (
2230 "Rate limiter invoked: now={:?}, block_until={:?}, may_sign={:?}" ,
2331 now,
24- self . block_until,
25- self . block_until <= now
32+ block_until,
33+ block_until <= now
2634 ) ;
27- if self . block_until <= now {
35+ if block_until <= now {
2836 // This Durable Object will be deleted after the alarm is triggered
2937 self . state
3038 . storage ( )
3139 . set_alarm ( std:: time:: Duration :: from_secs (
3240 crate :: constants:: RATE_LIMIT_SECONDS as u64 + 1 ,
3341 ) )
3442 . await ?;
35- self . block_until = now + Duration :: seconds ( crate :: constants:: RATE_LIMIT_SECONDS ) ;
43+ let block_until = now + Duration :: seconds ( crate :: constants:: RATE_LIMIT_SECONDS ) ;
44+ self . state
45+ . storage ( )
46+ . put ( "block_until" , block_until. timestamp ( ) )
47+ . await ?;
3648
3749 Response :: from_json ( & true )
3850 } else {
You can’t perform that action at this time.
0 commit comments