mirror of https://github.com/fafhrd91/actix-web
add lazy data extractor
This commit is contained in:
parent
2504c2ecb0
commit
1e6b528e4b
|
@ -101,6 +101,7 @@ smallvec = "1.6"
|
|||
socket2 = "0.4.0"
|
||||
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||
url = "2.1"
|
||||
tokio = { version = "1.7.1", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||
|
|
81
src/data.rs
81
src/data.rs
|
@ -1,9 +1,11 @@
|
|||
use std::{any::type_name, ops::Deref, sync::Arc};
|
||||
use std::{any::type_name, cell::Cell, fmt, future::Future, ops::Deref, rc::Rc, sync::Arc};
|
||||
|
||||
use actix_http::Extensions;
|
||||
use actix_utils::future::{err, ok, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use futures_util::FutureExt;
|
||||
use serde::Serialize;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use crate::{
|
||||
dev::Payload, error::ErrorInternalServerError, extract::FromRequest, request::HttpRequest,
|
||||
|
@ -149,6 +151,83 @@ impl<T: ?Sized + 'static> DataFactory for Data<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// A lazy extractor for thread-local data.
|
||||
pub struct LazyData<T> {
|
||||
inner: Rc<LazyDataInner<T>>,
|
||||
}
|
||||
|
||||
pub struct LazyDataInner<T> {
|
||||
cell: OnceCell<T>,
|
||||
fut: Cell<Option<LocalBoxFuture<'static, T>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for LazyData<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for LazyData<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Lazy")
|
||||
.field("cell", &self.inner.cell)
|
||||
.field("fut", &"..")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> LazyData<T> {
|
||||
pub fn new<F, Fut>(init: F) -> LazyData<T>
|
||||
where
|
||||
F: FnOnce() -> Fut,
|
||||
Fut: Future<Output = T> + 'static,
|
||||
{
|
||||
Self {
|
||||
inner: Rc::new(LazyDataInner {
|
||||
cell: OnceCell::new(),
|
||||
fut: Cell::new(Some(init().boxed_local())),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self) -> &T {
|
||||
self.inner
|
||||
.cell
|
||||
.get_or_init(|| async move {
|
||||
match self.inner.fut.take() {
|
||||
Some(fut) => fut.await,
|
||||
None => panic!("LazyData instance has previously been poisoned"),
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> FromRequest for LazyData<T> {
|
||||
type Config = ();
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<Self, Error>>;
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
if let Some(lazy) = req.app_data::<LazyData<T>>() {
|
||||
ok(lazy.clone())
|
||||
} else {
|
||||
log::debug!(
|
||||
"Failed to construct App-level LazyData extractor. \
|
||||
Request path: {:?} (type: {})",
|
||||
req.path(),
|
||||
type_name::<T>(),
|
||||
);
|
||||
err(ErrorInternalServerError(
|
||||
"App data is not configured, to configure use App::data()",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -17,7 +17,7 @@ use crate::scope::Scope;
|
|||
use crate::service::WebService;
|
||||
|
||||
pub use crate::config::ServiceConfig;
|
||||
pub use crate::data::Data;
|
||||
pub use crate::data::{Data, LazyData};
|
||||
pub use crate::request::HttpRequest;
|
||||
pub use crate::request_data::ReqData;
|
||||
pub use crate::types::*;
|
||||
|
|
Loading…
Reference in New Issue