-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cached value #288
Cached value #288
Changes from 6 commits
2e5d7c2
21fed88
3ffad44
f64f44f
e4f54b2
26447a3
954c530
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ | |
conditions: | ||
- "req.method == 'POST'" | ||
variables: | ||
- user_id | ||
- user_id |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,19 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; | |
#[derive(Debug)] | ||
pub(crate) struct AtomicExpiringValue { | ||
value: AtomicI64, | ||
expiry: AtomicU64, // in microseconds | ||
expiry: AtomicExpiryTime, | ||
} | ||
|
||
impl AtomicExpiringValue { | ||
pub fn new(value: i64, expiry: SystemTime) -> Self { | ||
let expiry = Self::get_duration_micros(expiry); | ||
Self { | ||
value: AtomicI64::new(value), | ||
expiry: AtomicU64::new(expiry), | ||
expiry: AtomicExpiryTime::new(expiry), | ||
} | ||
} | ||
|
||
pub fn value_at(&self, when: SystemTime) -> i64 { | ||
let when = Self::get_duration_micros(when); | ||
let expiry = self.expiry.load(Ordering::SeqCst); | ||
if expiry <= when { | ||
if self.expiry.expired_at(when) { | ||
return 0; | ||
} | ||
self.value.load(Ordering::SeqCst) | ||
|
@@ -31,44 +28,95 @@ impl AtomicExpiringValue { | |
} | ||
|
||
pub fn update(&self, delta: i64, ttl: u64, when: SystemTime) -> i64 { | ||
let ttl_micros = ttl * 1_000_000; | ||
let when_micros = Self::get_duration_micros(when); | ||
|
||
let expiry = self.expiry.load(Ordering::SeqCst); | ||
if expiry <= when_micros { | ||
let new_expiry = when_micros + ttl_micros; | ||
if self | ||
.expiry | ||
.compare_exchange(expiry, new_expiry, Ordering::SeqCst, Ordering::SeqCst) | ||
.is_ok() | ||
{ | ||
self.value.store(delta, Ordering::SeqCst); | ||
} | ||
if self.expiry.update_if_expired(ttl, when) { | ||
self.value.store(delta, Ordering::SeqCst); | ||
return delta; | ||
} | ||
self.value.fetch_add(delta, Ordering::SeqCst) + delta | ||
} | ||
|
||
pub fn ttl(&self) -> Duration { | ||
self.expiry.duration() | ||
} | ||
|
||
#[allow(dead_code)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is annoying, it's dead code when compiling for wasm... I don't think we should fail the build when that happens tho. Dead code for that target is fine, it's all DCE'd anyways... |
||
pub fn set(&self, value: i64, ttl: Duration) { | ||
self.expiry.update(ttl); | ||
self.value.store(value, Ordering::SeqCst); | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AtomicExpiryTime { | ||
expiry: AtomicU64, // in microseconds | ||
} | ||
|
||
impl AtomicExpiryTime { | ||
pub fn new(when: SystemTime) -> Self { | ||
let expiry = Self::since_epoch(when); | ||
Self { | ||
expiry: AtomicU64::new(expiry), | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn from_now(ttl: Duration) -> Self { | ||
Self::new(SystemTime::now() + ttl) | ||
} | ||
|
||
fn since_epoch(when: SystemTime) -> u64 { | ||
when.duration_since(UNIX_EPOCH) | ||
.expect("SystemTime before UNIX EPOCH!") | ||
.as_micros() as u64 | ||
} | ||
|
||
pub fn duration(&self) -> Duration { | ||
let expiry = | ||
SystemTime::UNIX_EPOCH + Duration::from_micros(self.expiry.load(Ordering::SeqCst)); | ||
expiry | ||
.duration_since(SystemTime::now()) | ||
.unwrap_or(Duration::ZERO) | ||
} | ||
|
||
fn get_duration_micros(when: SystemTime) -> u64 { | ||
when.duration_since(UNIX_EPOCH) | ||
.expect("SystemTime before UNIX EPOCH!") | ||
.as_micros() as u64 | ||
pub fn expired_at(&self, when: SystemTime) -> bool { | ||
let when = Self::since_epoch(when); | ||
self.expiry.load(Ordering::SeqCst) <= when | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn update(&self, ttl: Duration) { | ||
self.expiry | ||
.store(Self::since_epoch(SystemTime::now() + ttl), Ordering::SeqCst); | ||
} | ||
|
||
pub fn update_if_expired(&self, ttl: u64, when: SystemTime) -> bool { | ||
let ttl_micros = ttl * 1_000_000; | ||
let when_micros = Self::since_epoch(when); | ||
let expiry = self.expiry.load(Ordering::SeqCst); | ||
if expiry <= when_micros { | ||
let new_expiry = when_micros + ttl_micros; | ||
return self | ||
.expiry | ||
.compare_exchange(expiry, new_expiry, Ordering::SeqCst, Ordering::SeqCst) | ||
.is_ok(); | ||
} | ||
false | ||
} | ||
} | ||
|
||
impl Clone for AtomicExpiryTime { | ||
fn clone(&self) -> Self { | ||
Self { | ||
expiry: AtomicU64::new(self.expiry.load(Ordering::SeqCst)), | ||
} | ||
} | ||
} | ||
|
||
impl Default for AtomicExpiringValue { | ||
fn default() -> Self { | ||
AtomicExpiringValue { | ||
value: AtomicI64::new(0), | ||
expiry: AtomicU64::new(0), | ||
expiry: AtomicExpiryTime::new(UNIX_EPOCH), | ||
} | ||
} | ||
} | ||
|
@@ -77,7 +125,7 @@ impl Clone for AtomicExpiringValue { | |
fn clone(&self) -> Self { | ||
AtomicExpiringValue { | ||
value: AtomicI64::new(self.value.load(Ordering::SeqCst)), | ||
expiry: AtomicU64::new(self.expiry.load(Ordering::SeqCst)), | ||
expiry: self.expiry.clone(), | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think encapsulating that behavior made sense, regardless of the change... but now that I needed reusing that very same thing... it was time ;)