-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f6d37c
commit c990d07
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ members = [ | |
"hitbox-backend", | ||
"hitbox-derive", | ||
"hitbox-redis", | ||
"hitbox-tower", | ||
"examples", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
use hyper::{Body, Server}; | ||
|
||
use tower::make::Shared; | ||
use http::{Request, Response}; | ||
|
||
async fn handle(_: Request<Body>) -> Result<Response<Body>, Infallible> { | ||
Ok(Response::new("Hello, World!".into())) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let service = tower::ServiceBuilder::new() | ||
.layer(tower_http::trace::TraceLayer::new_for_http()) | ||
.service_fn(handle); | ||
|
||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
Server::bind(&addr) | ||
.serve(Shared::new(service)) | ||
.await | ||
.expect("server error"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
edition = "2021" | ||
name = "hitbox-tower" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
tower = "0.4" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
let result = 2 + 2; | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::{future::Future, marker::PhantomData}; | ||
|
||
use async_trait::async_trait; | ||
|
||
use hitbox::{ | ||
runtime::{AdapterResult, RuntimeAdapter}, | ||
CacheError, CacheState, CacheableResponse, Cacheable, | ||
}; | ||
use hitbox_backend::CacheBackend; | ||
use serde::de::DeserializeOwned; | ||
|
||
pub struct TowerAdapter<'b, Request, Response, Service, B> | ||
where | ||
Request: Cacheable, | ||
{ | ||
_response: PhantomData<Response>, | ||
backend: &'b B, | ||
upstream: Service, | ||
request: Option<Request>, | ||
cache_key: String, | ||
cache_ttl: u32, | ||
cache_stale_ttl: u32, | ||
} | ||
|
||
impl<'b, Request, Response, Service, B> FutureAdapter<'b, Request, Response, Service, B> | ||
where | ||
Request: Cacheable, | ||
{ | ||
pub fn new(upstream: U, request: Request, backend: &'b B) -> Result<Self, CacheError> { | ||
Ok(Self { | ||
cache_key: request.cache_key()?, | ||
cache_ttl: request.cache_ttl(), | ||
cache_stale_ttl: request.cache_stale_ttl(), | ||
request: Some(request), | ||
upstream, | ||
backend, | ||
_response: PhantomData::default(), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<Request, Response, Service, B, 'b> crate::runtime::RuntimeAdapter for FutureAdapter<'b, Request, Response, U, B> | ||
where | ||
Request: Cacheable + Send + Sync, | ||
{ | ||
type UpstreamResult = Response; | ||
async fn update_cache<'a>( | ||
&self, | ||
cached_value: &'a hitbox_backend::CachedValue<Self::UpstreamResult>, | ||
) -> crate::runtime::AdapterResult<()> { | ||
Ok(self | ||
.backend | ||
.set(self.cache_key.clone(), cached_value, Some(self.cache_ttl)) | ||
.await?) | ||
} | ||
|
||
async fn poll_cache(&self) -> crate::runtime::AdapterResult<CacheState<Self::UpstreamResult>> { | ||
Ok(self.backend.get(self.cache_key.clone()).await?.into()) | ||
} | ||
|
||
async fn poll_upstream(&mut self) -> crate::runtime::AdapterResult<Self::UpstreamResult> { | ||
let request = self.request.take(); | ||
let request = request.ok_or_else(|| { | ||
CacheError::CacheKeyGenerationError("Request already sent to upstream".to_owned()) | ||
})?; | ||
Ok(self.upstream.call(request).await) | ||
} | ||
|
||
fn eviction_settings(&self) -> hitbox_backend::EvictionPolicy { | ||
let ttl_settings = hitbox_backend::TtlSettings { | ||
ttl: self.cache_ttl, | ||
stale_ttl: self.cache_stale_ttl, | ||
}; | ||
hitbox_backend::EvictionPolicy::Ttl(ttl_settings) | ||
} | ||
} |