|
| 1 | +use std::num::NonZeroUsize; |
1 | 2 | use std::sync::Arc;
|
2 | 3 |
|
3 | 4 | // Import the base64 crate Engine trait anonymously so we can
|
@@ -25,8 +26,10 @@ impl AuthTimersGroupKey {
|
25 | 26 | }
|
26 | 27 | }
|
27 | 28 |
|
| 29 | +const AUTH_LRU_CACHE_SIZE: NonZeroUsize = NonZeroUsize::new(1000).unwrap(); |
| 30 | + |
28 | 31 | // Within a group, we can hold the lock for longer to verify the auth with upstream
|
29 |
| -type AuthTimersGroup = std::collections::HashMap<Handle, std::time::Instant>; |
| 32 | +type AuthTimersGroup = lru::LruCache<Handle, std::time::Instant>; |
30 | 33 | type AuthTimers =
|
31 | 34 | std::collections::HashMap<AuthTimersGroupKey, Arc<tokio::sync::Mutex<AuthTimersGroup>>>;
|
32 | 35 |
|
@@ -129,13 +132,15 @@ pub async fn check_http_auth(url: &str, auth: &Handle, required: bool) -> josh::
|
129 | 132 |
|
130 | 133 | let group_key = AuthTimersGroupKey::new(url, &auth);
|
131 | 134 | let auth_timers = AUTH_TIMERS
|
132 |
| - .lock() |
133 |
| - .unwrap() |
| 135 | + .lock()? |
134 | 136 | .entry(group_key.clone())
|
135 |
| - .or_default() |
| 137 | + .or_insert_with(|| { |
| 138 | + let cache = lru::LruCache::new(AUTH_LRU_CACHE_SIZE); |
| 139 | + Arc::new(tokio::sync::Mutex::new(cache)) |
| 140 | + }) |
136 | 141 | .clone();
|
137 | 142 |
|
138 |
| - let auth_header = AUTH.lock().unwrap().get(auth).cloned().unwrap_or_default(); |
| 143 | + let auth_header = AUTH.lock()?.get(auth).cloned().unwrap_or_default(); |
139 | 144 |
|
140 | 145 | let refs_url = format!("{}/info/refs?service=git-upload-pack", url);
|
141 | 146 | let do_request = || {
|
@@ -195,7 +200,7 @@ pub async fn check_http_auth(url: &str, auth: &Handle, required: bool) -> josh::
|
195 | 200 |
|
196 | 201 | let resp = do_request().await?;
|
197 | 202 | if resp.status().is_success() {
|
198 |
| - auth_timers.insert(auth.clone(), std::time::Instant::now()); |
| 203 | + auth_timers.put(auth.clone(), std::time::Instant::now()); |
199 | 204 | }
|
200 | 205 |
|
201 | 206 | resp
|
|
0 commit comments