Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/token_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::token::{TokenOrRequest, TokenProvider};
use crate::{error::Error, token::RequestReason, Token};

use std::hash::Hasher;
use std::sync::Mutex;
use std::sync::RwLock;

type Hash = u64;

Expand All @@ -13,7 +13,7 @@ struct Entry {

/// An in-memory cache for caching tokens.
pub struct TokenCache {
cache: Mutex<Vec<Entry>>,
cache: RwLock<Vec<Entry>>,
}

pub enum TokenOrRequestReason {
Expand All @@ -24,14 +24,14 @@ pub enum TokenOrRequestReason {
impl TokenCache {
pub fn new() -> Self {
Self {
cache: Mutex::new(Vec::new()),
cache: RwLock::new(Vec::new()),
}
}

/// Get a token from the cache that matches the hash
pub fn get(&self, hash: Hash) -> Result<TokenOrRequestReason, Error> {
let reason = {
let cache = self.cache.lock().map_err(|_e| Error::Poisoned)?;
let cache = self.cache.read().map_err(|_e| Error::Poisoned)?;
match cache.binary_search_by(|i| i.hash.cmp(&hash)) {
Ok(i) => {
let token = &cache[i].token;
Expand All @@ -52,7 +52,7 @@ impl TokenCache {
/// Insert a token into the cache
pub fn insert(&self, token: Token, hash: Hash) -> Result<(), Error> {
// Last token wins, which...should?...be fine
let mut cache = self.cache.lock().map_err(|_e| Error::Poisoned)?;
let mut cache = self.cache.write().map_err(|_e| Error::Poisoned)?;
match cache.binary_search_by(|i| i.hash.cmp(&hash)) {
Ok(i) => cache[i].token = token,
Err(i) => {
Expand Down