Skip to content

Commit

Permalink
Merge pull request #45 from hit-box/tokio-backend
Browse files Browse the repository at this point in the history
Tokio backend
  • Loading branch information
singulared authored Sep 2, 2022
2 parents 3fd47de + b06e7ad commit c1ceeff
Show file tree
Hide file tree
Showing 62 changed files with 1,649 additions and 603 deletions.
7 changes: 6 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

version: 2
updates:
- package-ecosystem: "cargo"
- package-ecosystem: "cargo" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
8 changes: 4 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [1.54.0, stable]
rust: [1.60.0, stable]
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
Expand All @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [1.54.0, stable, beta]
rust: [1.60.0, stable, beta]
redis-version: [6]
steps:
- uses: actions/checkout@main
Expand All @@ -49,14 +49,14 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --all-features
args: --workspace --all-features -- --test-threads=1

- name: Generate coverage file
if: >
matrix.rust == 'stable'
run: |
cargo install cargo-tarpaulin
cargo tarpaulin --out Xml --verbose --workspace --all-features --ignore-tests
cargo tarpaulin --out Xml --verbose --workspace --all-features --ignore-tests -- --test-threads=1
- name: Upload to Codecov
if: >
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

members = [
"hitbox",
"hitbox-tokio",
"hitbox-actix",
"hitbox-backend",
"hitbox-derive",
"hitbox-redis",
"hitbox-tower",
"examples",
]
17 changes: 12 additions & 5 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
name = "hitbox-examples"
version = "0.0.0"
publish = false
edition = "2018"
edition = "2021"

[features]
default = []

[dev-dependencies]
hitbox = { path = "../hitbox", features = ["derive"] }
hitbox = { path = "../hitbox", features = ["derive", "metrics"] }
hitbox-actix = { path = "../hitbox-actix", features = ["redis"]}
actix = "0.12"
actix = "0.13"
log = "0.4"
actix-rt = "2"
serde_json = "1"
serde_qs = { version = "0.8" }
serde_qs = { version = "0.10" }
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
thiserror = "1"
Expand All @@ -23,4 +23,11 @@ env_logger = "0.9"
actix_derive = "0.6"
actix-web = "4.0"
tracing = "0.1"
tracing-subscriber = "0.2"
tracing-subscriber = "0.3"
metrics-exporter-prometheus = "0.11"

hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["full"] }
tower-http = { version = "0.3", features = ["full"] }
http = "0.2"
6 changes: 5 additions & 1 deletion examples/examples/async_backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix::prelude::*;
/*use actix::prelude::*;
use hitbox::dev::{Backend, BackendError, Delete, DeleteStatus, Get, Lock, LockStatus, Set};
use hitbox_actix::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -94,4 +94,8 @@ async fn main() -> Result<(), CacheError> {
let _ = cache.send(msg.into_cache(&upstream)).await??;
Ok(())
}*/

fn main() {

}
6 changes: 5 additions & 1 deletion examples/examples/cacheable_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix::prelude::*;
/*use actix::prelude::*;
use actix_derive::{Message, MessageResponse};
use hitbox_actix::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -57,4 +57,8 @@ async fn main() -> Result<(), CacheError> {
let res = cache.send(msg.into_cache(&upstream)).await??;
println!("{:#?}", res);
Ok(())
}*/

fn main() {

}
35 changes: 30 additions & 5 deletions examples/examples/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ impl<T, E> CacheableResponse<T, E> for Result<T, E> {
struct Pong(i32);

#[derive(Debug)]
enum Error {}
struct PongError {}

#[derive(Message, Cacheable, Serialize)]
#[rtype(result = "Result<Pong, Error>")]
#[rtype(result = "Result<Pong, PongError>")]
struct Ping {
id: i32,
}
Expand All @@ -57,16 +57,41 @@ impl Handler<Ping> for UpstreamActor {
}
}

#[derive(Debug)]
enum Error {
Actix(actix::MailboxError),
Cache(hitbox::CacheError),
Msg(PongError),
}

impl From<actix::MailboxError> for Error {
fn from(err: actix::MailboxError) -> Error {
Error::Actix(err)
}
}

impl From<hitbox::CacheError> for Error {
fn from(err: hitbox::CacheError) -> Error {
Error::Cache(err)
}
}

impl From<PongError> for Error {
fn from(err: PongError) -> Error {
Error::Msg(err)
}
}

#[actix::main]
async fn main() -> Result<(), CacheError> {
async fn main() -> Result<(), Error> {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.filter_level(log::LevelFilter::Trace)
.init();

let cache = Cache::new().await?.start();
let upstream = UpstreamActor.start();

let msg = Ping { id: 42 };
let _ = cache.send(msg.into_cache(&upstream)).await??;
let _ = cache.send(msg.into_cache(&upstream)).await???;
Ok(())
}
66 changes: 66 additions & 0 deletions examples/examples/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use actix::prelude::*;
use actix_derive::MessageResponse;
use hitbox::{hitbox_serializer, CachePolicy, Cacheable, CacheableResponse};
use hitbox_actix::{Cache, CacheError, IntoCache};
use metrics_exporter_prometheus::PrometheusBuilder;

#[derive(Default)]
struct UpstreamActor;

impl Actor for UpstreamActor {
type Context = Context<Self>;
}

#[derive(Message, Cacheable, serde::Serialize)]
#[rtype(result = "Pong")]
struct Ping {
number: u8,
}

impl Ping {
fn new(number: u8) -> Self {
Self { number }
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize, MessageResponse, CacheableResponse)]
struct Pong {
number: u8,
}

impl Handler<Ping> for UpstreamActor {
type Result = MessageResult<Ping>;

fn handle(&mut self, msg: Ping, _: &mut Self::Context) -> Self::Result {
MessageResult(Pong { number: msg.number })
}
}

#[actix::main]
async fn main() {
let upstream = UpstreamActor::default().start();
let cache = Cache::new().await.unwrap().start();
let recorder = PrometheusBuilder::new()
.install_recorder()
.expect("failed to install recorder");

let _pong_0 = cache
.send(Ping::new(0).into_cache(&upstream))
.await
.expect("actix mailbox timeout/closed")
.expect("cache actor error");

let _again_pong_0 = cache
.send(Ping::new(0).into_cache(&upstream))
.await
.expect("actix mailbox timeout/closed")
.expect("cache actor error");

let _pong_1 = cache
.send(Ping::new(1).into_cache(&upstream))
.await
.expect("actix mailbox timeout/closed")
.expect("cache actor error");

println!("{}", recorder.render());
}
58 changes: 0 additions & 58 deletions examples/examples/metrics.rs.pass

This file was deleted.

6 changes: 5 additions & 1 deletion examples/examples/sync_backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix::prelude::*;
/*use actix::prelude::*;
use hitbox::dev::{Backend, BackendError, Delete, DeleteStatus, Get, Lock, LockStatus, Set};
use hitbox_actix::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -100,4 +100,8 @@ async fn main() -> Result<(), CacheError> {
let _ = cache.send(msg.into_cache(&upstream)).await??;
Ok(())
}*/

fn main() {

}
23 changes: 23 additions & 0 deletions examples/examples/tower.rs
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");
}

5 changes: 3 additions & 2 deletions hitbox-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "hitbox-actix"
version = "0.1.0"
authors = ["Belousow Makc <[email protected]>", "Andrey Ermilov <[email protected]>"]
license = "MIT"
edition = "2018"
edition = "2021"
description = "Asynchronous caching framework for Actix."
readme = "README.md"
repository = "https://github.com/hit-box/hitbox/"
Expand All @@ -16,10 +16,11 @@ keywords = ["cache", "actix", "async", "cache-backend", "hitbox"]
hitbox = { path = "../hitbox", version = "0.1.0" }
hitbox-backend = { path = "../hitbox-backend", version = "0.1.0" }
hitbox-redis = { path = "../hitbox-redis", version = "0.1.0", optional = true }
actix = { version = "0.12" }
actix = { version = "0.13" }
serde = { version = "1", features = ["derive"] }
tracing = "0.1"
serde_json = "1.0.64"
async-trait = "0.1.52"

[features]
default = ["redis", "derive"]
Expand Down
Loading

0 comments on commit c1ceeff

Please sign in to comment.