Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: services comm using redis #123

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ diesel.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
futures.workspace = true
futures-util.workspace = true
serde.workspace = true
deadpool-redis = "0.15.1"
redis = {version = "0.25.0", features = ["streams"]}

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
1 change: 1 addition & 0 deletions chain/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
. ../.env
export TENDERMINT_URL
export DATABASE_URL
export QUEUE_URL
cargo run -- --initial-query-retry-time 5
21 changes: 19 additions & 2 deletions chain/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::env;

use anyhow::Context;
use deadpool_diesel::postgres::{Object, Pool as DbPool};
use deadpool_redis::{Config, Pool as RedisPool, Runtime};

#[derive(Clone)]
pub struct AppState {
db: DbPool,
redis: RedisPool,
}

impl AppState {
pub fn new(db_url: String) -> anyhow::Result<Self> {
pub fn new(db_url: String, redis_url: String) -> anyhow::Result<Self> {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 8.to_string())
.parse::<usize>()
Expand All @@ -23,7 +25,13 @@ impl AppState {
.build()
.context("Failed to build Postgres db pool")?;

Ok(Self { db: pool })
let cfg = Config::from_url(redis_url);
let redis_pool = cfg.create_pool(Some(Runtime::Tokio1)).unwrap();

Ok(Self {
db: pool,
redis: redis_pool,
})
}

pub async fn get_db_connection(&self) -> anyhow::Result<Object> {
Expand All @@ -32,4 +40,13 @@ impl AppState {
.await
.context("Failed to get db connection handle from deadpool")
}

pub async fn get_redis_connection(
&self,
) -> anyhow::Result<deadpool_redis::Connection> {
self.redis
.get()
.await
.context("Failed to get redis connection handle from deadpool")
}
}
5 changes: 4 additions & 1 deletion chain/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ impl Display for CargoEnv {
}
}

#[derive(clap::Parser)]
#[derive(clap::Parser, Clone)]
pub struct AppConfig {
#[clap(long, env)]
pub tendermint_url: String,

#[clap(long, env)]
pub database_url: String,

#[clap(long, env)]
pub queue_url: String,

#[clap(long, env)]
pub initial_query_retry_time: u64,

Expand Down
Loading