forked from MIrrors/bin
Move to actix-web
This commit is contained in:
41
src/io.rs
41
src/io.rs
@@ -1,43 +1,31 @@
|
||||
extern crate gpw;
|
||||
extern crate linked_hash_map;
|
||||
extern crate owning_ref;
|
||||
extern crate rand;
|
||||
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::{thread_rng, Rng};
|
||||
use rand::{thread_rng, Rng, distributions::Alphanumeric};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
|
||||
use owning_ref::OwningRef;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::env;
|
||||
use std::sync::{PoisonError};
|
||||
|
||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||
|
||||
type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>;
|
||||
|
||||
pub type PasteStore = RwLock<LinkedHashMap<String, String>>;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE")
|
||||
.map(|f| f
|
||||
.parse::<usize>()
|
||||
.expect("Failed to parse value of BIN_BUFFER_SIZE"))
|
||||
.unwrap_or(1000usize);
|
||||
static ref BUFFER_SIZE: usize = argh::from_env::<crate::BinArgs>().buffer_size;
|
||||
}
|
||||
|
||||
/// Ensures `ENTRIES` is less than the size of `BIN_BUFFER_SIZE`. If it isn't then
|
||||
/// `ENTRIES.len() - BIN_BUFFER_SIZE` elements will be popped off the front of the map.
|
||||
///
|
||||
/// During the purge, `ENTRIES` is locked and the current thread will block.
|
||||
async fn purge_old() {
|
||||
let entries_len = ENTRIES.read().await.len();
|
||||
async fn purge_old(entries: &PasteStore) {
|
||||
let entries_len = entries.read().await.len();
|
||||
|
||||
if entries_len > *BUFFER_SIZE {
|
||||
let to_remove = entries_len - *BUFFER_SIZE;
|
||||
|
||||
let mut entries = ENTRIES.write().await;
|
||||
let mut entries = entries.write().await;
|
||||
|
||||
for _ in 0..to_remove {
|
||||
entries.pop_front();
|
||||
@@ -58,11 +46,10 @@ pub fn generate_id() -> String {
|
||||
}
|
||||
|
||||
/// Stores a paste under the given id
|
||||
pub async fn store_paste(id: String, content: String) {
|
||||
purge_old().await;
|
||||
pub async fn store_paste(entries: &PasteStore, id: String, content: String) {
|
||||
purge_old(&entries).await;
|
||||
|
||||
ENTRIES
|
||||
.write()
|
||||
entries.write()
|
||||
.await
|
||||
.insert(id, content);
|
||||
}
|
||||
@@ -70,9 +57,9 @@ pub async fn store_paste(id: String, content: String) {
|
||||
/// Get a paste by id.
|
||||
///
|
||||
/// Returns `None` if the paste doesn't exist.
|
||||
pub async fn get_paste(id: &str) -> Option<RwLockReadGuardRef<'_, LinkedHashMap<String, String>, String>> {
|
||||
pub async fn get_paste<'a>(entries: &'a PasteStore, id: &str) -> Option<RwLockReadGuardRef<'a, LinkedHashMap<String, String>, String>> {
|
||||
// need to box the guard until owning_ref understands Pin is a stable address
|
||||
let or = RwLockReadGuardRef::new(Box::new(ENTRIES.read().await));
|
||||
let or = RwLockReadGuardRef::new(Box::new(entries.read().await));
|
||||
|
||||
if or.contains_key(id) {
|
||||
Some(or.map(|x| x.get(id).unwrap()))
|
||||
|
||||
Reference in New Issue
Block a user