Use std RwLock instead of the tokio one

tokio recommends to use the std synchronization primitives if the locked
value is just data instead of a shared resource.
This commit is contained in:
Klemens Schölhorn 2023-04-06 22:06:29 +02:00
parent a7ade951aa
commit 6206d0ea58

View File

@ -1,4 +1,4 @@
use std::{net::SocketAddr, path::{Path, PathBuf}, ffi::{OsStr, OsString}, fs::File, io::{BufReader, Seek, Cursor, BufRead}, env::args_os, os::unix::prelude::OsStrExt, cmp::Reverse, sync::Arc, collections::HashMap}; use std::{net::SocketAddr, path::{Path, PathBuf}, ffi::{OsStr, OsString}, fs::File, io::{BufReader, Seek, Cursor, BufRead}, env::args_os, os::unix::prelude::OsStrExt, cmp::Reverse, sync::{Arc, RwLock}, collections::HashMap};
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result, anyhow};
use axum::{Router, routing::{get, post}, response::{IntoResponse, Redirect}, http::{StatusCode, header}, extract::{self, State}, Form, handler::Handler}; use axum::{Router, routing::{get, post}, response::{IntoResponse, Redirect}, http::{StatusCode, header}, extract::{self, State}, Form, handler::Handler};
@ -10,7 +10,7 @@ use error::AppError;
use exif::Exif; use exif::Exif;
use image::{imageops::FilterType, DynamicImage}; use image::{imageops::FilterType, DynamicImage};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use tokio::{task, sync::RwLock}; use tokio::task;
use tower_http::{trace::{self, TraceLayer}, compression::CompressionLayer}; use tower_http::{trace::{self, TraceLayer}, compression::CompressionLayer};
use tracing::Level; use tracing::Level;
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
@ -195,7 +195,7 @@ async fn converted_image(
.ok(); .ok();
let cached_buffer = if let Some(image_modified) = image_modified { let cached_buffer = if let Some(image_modified) = image_modified {
image_cache image_cache
.read().await .read().unwrap()
.get(image_name) .get(image_name)
.filter(|(cache_modified, _)| *cache_modified == image_modified) .filter(|(cache_modified, _)| *cache_modified == image_modified)
.map(|(_, image_buffer)| image_buffer) .map(|(_, image_buffer)| image_buffer)
@ -216,7 +216,7 @@ async fn converted_image(
if let Some(image_modified) = image_modified { if let Some(image_modified) = image_modified {
tracing::debug!("Add {:?} ({}k) to cache", image_name, image_buffer.len() / 1024); tracing::debug!("Add {:?} ({}k) to cache", image_name, image_buffer.len() / 1024);
image_cache image_cache
.write().await .write().unwrap()
.insert(image_name.to_owned(), (image_modified, image_buffer.clone())); .insert(image_name.to_owned(), (image_modified, image_buffer.clone()));
} }