Create a handle bound to the captured client fingerprint.
(
&self,
auth_context: AuthContext,
captured_fingerprint: ClientFingerprint,
)
| 118 | |
| 119 | /// Create a handle bound to the captured client fingerprint. |
| 120 | pub fn create( |
| 121 | &self, |
| 122 | auth_context: AuthContext, |
| 123 | captured_fingerprint: ClientFingerprint, |
| 124 | ) -> String { |
| 125 | let now = now_secs(); |
| 126 | let cached = CachedSession { |
| 127 | auth_context, |
| 128 | captured_fingerprint, |
| 129 | expires_at: now + self.default_ttl_secs, |
| 130 | }; |
| 131 | |
| 132 | let mut sessions = self.sessions.write().unwrap_or_else(|p| p.into_inner()); |
| 133 | let handle = loop { |
| 134 | let candidate = generate_handle(); |
| 135 | if !sessions.contains_key(&candidate) { |
| 136 | break candidate; |
| 137 | } |
| 138 | }; |
| 139 | sessions.insert(handle.clone(), cached); |
| 140 | |
| 141 | // Lazy cleanup of expired handles (bounded per call). |
| 142 | let expired: Vec<String> = sessions |
| 143 | .iter() |
| 144 | .filter(|(_, s)| now >= s.expires_at) |
| 145 | .take(100) |
| 146 | .map(|(k, _)| k.clone()) |
| 147 | .collect(); |
| 148 | for key in expired { |
| 149 | sessions.remove(&key); |
| 150 | } |
| 151 | |
| 152 | handle |
| 153 | } |
| 154 | |
| 155 | /// Resolve a handle with full hygiene: per-connection rate limiting, |
| 156 | /// fingerprint binding, miss counting, spike detection. |