Skip to content

Commit

Permalink
feat(config): Intermediate result
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyErmilov committed Aug 14, 2023
1 parent 77a64bd commit d897c95
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 47 deletions.
41 changes: 20 additions & 21 deletions examples/examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,23 @@ async fn main() {
tracing::subscriber::set_global_default(subscriber).unwrap();

let backend = RedisBackend::new().unwrap();
let inmemory = StrettoBackend::builder(2 ^ 16)
.finalize()
.unwrap();
let inmemory = StrettoBackend::builder(2 ^ 16).finalize().unwrap();
//let request_predicate = predicate::RequestBuilder::new()
//.query("cache", "true")
//.build();
//.query("cache", "true")
//.build();
//let response_predicate = predicate::ResponseBuilder::new()
//.status_code(200)
//.body(operations::NE(operations::EmptyVec))
//.build();
//.status_code(200)
//.body(operations::NE(operations::EmptyVec))
//.build();
//let cache_key = CacheKeyBuilder::new()
//.path(Full)
//.method()
//.build();
//.path(Full)
//.method()
//.build();
//let endpoint_config = Config::builder()
//.request_predicate(response_predicate)
//.response_predicate(response_predicate)
//.cache_key(cache_key)
//.build();
//.request_predicate(response_predicate)
//.response_predicate(response_predicate)
//.cache_key(cache_key)
//.build();
let app = Router::new()
.route("/greet/:name/", get(handler_result))
.route("/", get(handler))
Expand All @@ -66,15 +64,16 @@ async fn main() {
ServiceBuilder::new()
.layer(
Cache::builder()
//.config(config)
.backend(inmemory)
.build()
//.config(config)
.backend(inmemory)
.build(),
)
.layer(
Cache::builder()
//.config(config)
.backend(backend)
.build()),
//.config(config)
.backend(backend)
.build(),
),
);

// run it with hyper on localhost:3000
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/tower.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hitbox_stretto::StrettoBackend;
use hitbox_redis::RedisBackend;
use hitbox_stretto::builder::StrettoBackendBuilder;
use hitbox_stretto::StrettoBackend;
use hitbox_tower::Cache;
use hyper::{Body, Server};
use std::{convert::Infallible, net::SocketAddr};
Expand Down
37 changes: 31 additions & 6 deletions hitbox-tower/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
#[derive(Clone)]
pub struct Config {
pub query: (String, String),
use hitbox_http::CacheableHttpRequest;
use hitbox::predicates::Predicate;
use hitbox_http::predicates::{NeutralPredicate, query::QueryPredicate};

pub enum RequestPredicate {
Query { key: String, value: String },
}

pub struct EndpointConfig {
pub request_predicates: Vec<RequestPredicate>,
}

impl Config {
pub fn new() -> Self {
Self { query: (String::from("key"), String::from("value")) }
impl EndpointConfig {
pub fn create<ReqBody>(&self) -> Box<dyn Predicate<Subject = CacheableHttpRequest<ReqBody>>>
where
ReqBody: Send,
{
let acc_predicate = Box::new(NeutralPredicate::new());
self
.request_predicates
.iter()
.rfold(acc_predicate, |inner, predicate| match predicate {
RequestPredicate::Query { key, value } => Box::new(inner.query(key, value))

Check failure on line 23 in hitbox-tower/src/config.rs

View workflow job for this annotation

GitHub Actions / check (stable)

the method `query` exists for struct `Box<dyn Predicate<Subject = CacheableHttpRequest<ReqBody>>>`, but its trait bounds were not satisfied
//PredicateType::Header { key, value } => Box::new(inner.header(key, value)),
//PredicateType::Body => Box::new(inner.body()),
})
}
}

impl Default for EndpointConfig {
fn default() -> Self {
Self {
request_predicates: Vec::new(),
}
}
}
26 changes: 16 additions & 10 deletions hitbox-tower/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
use crate::config::EndpointConfig;
use std::sync::Arc;

use hitbox::{backend::CacheBackend, predicates::Predicate, Extractor};
use hitbox_http::{
extractors::NeutralExtractor,
extractors::{method::MethodExtractor, path::PathExtractor},
predicates::{query::QueryPredicate, NeutralPredicate, NeutralResponsePredicate},
CacheableHttpRequest, CacheableHttpResponse, FromBytes,
};
use http::{Request, Response};
use hitbox::backend::CacheBackend;
use tower::Layer;

use crate::{dummy::DummyBackend, service::CacheService};

#[derive(Clone)]
pub struct Cache<B> {
pub backend: Arc<B>,
pub endpoint_config: Arc<EndpointConfig>,
}

impl<B> Cache<B> {
pub fn new(backend: B) -> Cache<B> {
Cache {
backend: Arc::new(backend),
endpoint_config: Arc::new(Default::default()),
}
}
}
Expand All @@ -29,7 +25,11 @@ impl<S, B> Layer<S> for Cache<B> {
type Service = CacheService<S, B>;

fn layer(&self, upstream: S) -> Self::Service {
CacheService::new(upstream, Arc::clone(&self.backend), crate::config::Config::new())
CacheService::new(
upstream,
Arc::clone(&self.backend),
Arc::new(Default::default()),
)
}
}

Expand All @@ -41,6 +41,7 @@ impl Cache<DummyBackend> {

pub struct CacheBuilder<B> {
backend: Option<B>,
endpoint_config: Option<EndpointConfig>,
}

impl<B> CacheBuilder<B>
Expand All @@ -50,18 +51,23 @@ where
pub fn backend<NB: CacheBackend>(self, backend: NB) -> CacheBuilder<NB> {
CacheBuilder {
backend: Some(backend),
endpoint_config: self.endpoint_config,
}
}

pub fn build(self) -> Cache<B> {
Cache {
backend: Arc::new(self.backend.expect("Please add some cache backend")),
endpoint_config: Arc::new(self.endpoint_config.unwrap_or_default()),
}
}
}

impl<B> Default for CacheBuilder<B> {
fn default() -> Self {
Self { backend: None }
Self {
backend: None,
endpoint_config: Default::default(),
}
}
}
2 changes: 1 addition & 1 deletion hitbox-tower/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod config;
pub mod dummy;
pub mod future;
pub mod layer;
pub mod service;
pub mod config;

pub use layer::Cache;
22 changes: 15 additions & 7 deletions hitbox-tower/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::Config;
use crate::config::EndpointConfig;
use std::{fmt::Debug, sync::Arc};

use hitbox::{backend::CacheBackend, fsm::CacheFuture};
Expand All @@ -17,12 +17,16 @@ use crate::future::Transformer;
pub struct CacheService<S, B> {
upstream: S,
backend: Arc<B>,
config: Config,
endpoint_config: Arc<EndpointConfig>,
}

impl<S, B> CacheService<S, B> {
pub fn new(upstream: S, backend: Arc<B>, config: Config) -> Self {
CacheService { upstream, backend, config }
pub fn new(upstream: S, backend: Arc<B>, endpoint_config: Arc<EndpointConfig>) -> Self {
CacheService {
upstream,
backend,
endpoint_config,
}
}
}

Expand All @@ -35,7 +39,7 @@ where
Self {
upstream: self.upstream.clone(),
backend: Arc::clone(&self.backend),
config: self.config.clone(),
endpoint_config: Arc::clone(&self.endpoint_config),
}
}
}
Expand Down Expand Up @@ -73,8 +77,12 @@ where
dbg!(&req);

let transformer = Transformer::new(self.upstream.clone());
let config = self.config.clone();
let request_predicate = NeutralPredicate::new().query(config.query.0, config.query.1);
let config = Arc::into_inner(self.endpoint_config.clone()).unwrap();
let request_predicate = NeutralPredicate::new();
//let request_predicat = match config.query {
//Some(value) => request_predicate.query(value.key, value.name),
//None => request_predicate,
//};
let response_predicate = NeutralResponsePredicate::new();
let extractor = NeutralExtractor::new().method().path("/{path}*");
CacheFuture::new(
Expand Down
2 changes: 1 addition & 1 deletion hitbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ pub mod fsm;
pub mod metrics;
pub mod predicates;
pub use cache::Cacheable;
pub use cache::Extractor;
pub use error::CacheError;
pub use hitbox_backend::{CachePolicy, CacheState, CacheableResponse, CachedValue};
pub use cache::Extractor;

/// The `hitbox` prelude.
pub mod prelude {
Expand Down

0 comments on commit d897c95

Please sign in to comment.