fix(server): do not hold the RwLock guard before async calls

This commit is contained in:
Orhun Parmaksız 2022-05-16 12:34:57 +03:00
parent 59415cd31a
commit 657ca8c1d4
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 11 additions and 9 deletions

View file

@ -9,6 +9,7 @@ use std::fs::{self, File};
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, Write}; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str; use std::str;
use std::sync::RwLock;
use url::Url; use url::Url;
/// Type of the data to store. /// Type of the data to store.
@ -157,7 +158,7 @@ impl Paste {
&mut self, &mut self,
expiry_date: Option<u128>, expiry_date: Option<u128>,
client: &Client, client: &Client,
config: Config, config: &RwLock<Config>,
) -> Result<String, Error> { ) -> Result<String, Error> {
let data = str::from_utf8(&self.data).map_err(error::ErrorBadRequest)?; let data = str::from_utf8(&self.data).map_err(error::ErrorBadRequest)?;
let url = Url::parse(data).map_err(error::ErrorBadRequest)?; let url = Url::parse(data).map_err(error::ErrorBadRequest)?;
@ -172,6 +173,8 @@ impl Paste {
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
let payload_limit = config let payload_limit = config
.read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?
.server .server
.max_content_length .max_content_length
.get_bytes() .get_bytes()
@ -183,6 +186,9 @@ impl Paste {
.await .await
.map_err(error::ErrorInternalServerError)? .map_err(error::ErrorInternalServerError)?
.to_vec(); .to_vec();
let config = config
.read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?;
let bytes_checksum = util::sha256_digest(&*bytes)?; let bytes_checksum = util::sha256_digest(&*bytes)?;
self.data = bytes; self.data = bytes;
if !config.paste.duplicate_files.unwrap_or(true) && expiry_date.is_none() { if !config.paste.duplicate_files.unwrap_or(true) && expiry_date.is_none() {
@ -330,7 +336,7 @@ mod tests {
}; };
let client_data = Data::new(Client::default()); let client_data = Data::new(Client::default());
let file_name = paste let file_name = paste
.store_remote_file(None, &client_data, config.clone()) .store_remote_file(None, &client_data, &RwLock::new(config.clone()))
.await?; .await?;
let file_path = PasteType::RemoteFile let file_path = PasteType::RemoteFile
.get_path(&config.server.upload_path) .get_path(&config.server.upload_path)

View file

@ -165,12 +165,8 @@ async fn upload(
paste.store_file(content.get_file_name()?, expiry_date, &config)? paste.store_file(content.get_file_name()?, expiry_date, &config)?
} }
PasteType::RemoteFile => { PasteType::RemoteFile => {
{ paste
let config = config.read().map_err(|_| { .store_remote_file(expiry_date, &client, &config)
error::ErrorInternalServerError("cannot acquire config")
})?;
paste.store_remote_file(expiry_date, &client, config.clone())
}
.await? .await?
} }
PasteType::Url => { PasteType::Url => {