Skip to content

Commit 61cf30d

Browse files
jordanhunt22Convex, Inc.
authored andcommitted
[Access Tokens] Add cache for valid access tokens (#27709)
Add 60 second cache for valid tokens, so we don't have to re-validate an access token on every request. GitOrigin-RevId: ac6ca92fc453679839f4019fa9dbf7ad28425352
1 parent 9ee6fc7 commit 61cf30d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

crates/common/src/http/fetch.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use std::collections::{
2-
BTreeMap,
3-
HashMap,
1+
use std::{
2+
collections::{
3+
BTreeMap,
4+
HashMap,
5+
},
6+
sync::atomic::{
7+
AtomicU64,
8+
Ordering,
9+
},
410
};
511

612
use async_trait::async_trait;
@@ -135,12 +141,14 @@ type HandlerFn = Box<
135141

136142
pub struct StaticFetchClient {
137143
router: BTreeMap<url::Url, HashMap<http::Method, HandlerFn>>,
144+
num_calls: AtomicU64,
138145
}
139146

140147
impl StaticFetchClient {
141148
pub fn new() -> Self {
142149
Self {
143150
router: BTreeMap::new(),
151+
num_calls: AtomicU64::new(0),
144152
}
145153
}
146154

@@ -156,11 +164,17 @@ impl StaticFetchClient {
156164
.or_default()
157165
.insert(method, Box::new(handler));
158166
}
167+
168+
/// Returns how many times a fetch client has been called
169+
pub fn num_calls(&self) -> u64 {
170+
self.num_calls.load(Ordering::Relaxed)
171+
}
159172
}
160173

161174
#[async_trait]
162175
impl FetchClient for StaticFetchClient {
163176
async fn fetch(&self, request: HttpRequestStream) -> anyhow::Result<HttpResponseStream> {
177+
self.num_calls.fetch_add(1, Ordering::Relaxed);
164178
let handler = self
165179
.router
166180
.get(&request.url)

crates/common/src/knobs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,3 +1058,10 @@ pub static REQUEST_TRACE_SAMPLE_CONFIG: LazyLock<SamplingConfig> =
10581058
/// the "backend_startup" domain keyed by db cluster name.
10591059
pub static STARTUP_RATE_LIMIT_ENABLED: LazyLock<bool> =
10601060
LazyLock::new(|| env_config("STARTUP_RATE_LIMIT_ENABLED", false));
1061+
1062+
/// Size of the cache for access token authentication
1063+
pub static AUTH_CACHE_SIZE: LazyLock<usize> = LazyLock::new(|| env_config("AUTH_CACHE_SIZE", 1000));
1064+
1065+
/// Length of time an entry to the access authentication cache is valid
1066+
pub static AUTH_CACHE_TTL_SECONDS: LazyLock<u64> =
1067+
LazyLock::new(|| env_config("AUTH_CACHE_TTL_SECONDS", 60));

0 commit comments

Comments
 (0)