Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Break: replace the Redis options with --redis-url
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jun 5, 2022
1 parent 35119d9 commit 7c534e0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 68 deletions.
6 changes: 3 additions & 3 deletions rusty-shared-opts/src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use tracing::{debug, info, instrument, warn};
pub struct Opts {
/// URL to which the microservice should post its heartbeat.
#[clap(long = "heartbeat-url", env = "RUSTY_HOME_HEARTBEAT_URL")]
url: Option<String>,
heartbeat_url: Option<String>,
}

impl Opts {
pub fn get_heartbeat(self) -> Result<Heartbeat> {
if self.url.is_none() {
if self.heartbeat_url.is_none() {
warn!("heartbeat URL is not specified, heartbeat is disabled");
}
self.url
self.heartbeat_url
.map(|url| Ok(Heartbeat::new(Some((Client::new(), url.parse()?)))))
.unwrap_or_else(|| Ok(Heartbeat::new(None)))
}
Expand Down
22 changes: 6 additions & 16 deletions rusty-shared-opts/src/redis.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
use std::net::SocketAddr;

use clap::Parser;

#[derive(Parser)]
pub struct Opts {
/// One address is treated as Redis Server address, multiple addresses – as Sentinel addresses.
#[clap(
long = "redis-server",
env = "RUSTY_HOME_REDIS_ADDRESSES",
default_value = "127.0.0.1:6379",
use_value_delimiter = true
)]
pub addresses: Vec<SocketAddr>,

/// Redis Sentinel master name.
/// Redis URL.
/// See: https://docs.rs/fred/5.0.0/fred/types/struct.RedisConfig.html#method.from_url.
#[clap(
long = "redis-service-name",
env = "RUSTY_HOME_REDIS_SERVICE_NAME",
default_value = "mymaster"
long = "redis-url",
env = "RUSTY_HOME_REDIS_URL",
default_value = "redis://localhost/0"
)]
pub service_name: String,
pub redis_url: String,
}
64 changes: 19 additions & 45 deletions rusty-shared-redis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::fmt::Debug;
use std::net::SocketAddr;
use std::time;

use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use async_std::future::timeout;
use fred::prelude::*;
use fred::types::{MultipleKeys, MultipleValues, PerformanceConfig, RedisKey};
use tracing::{debug, info, instrument};
use tracing::{debug, instrument};

pub struct Redis {
pub client: RedisClient,
Expand All @@ -25,17 +24,27 @@ impl Redis {
/// Thus, I put a short timeout on each `EVALSHA` call.
const EVALSHA_TIMEOUT: time::Duration = time::Duration::from_secs(5);

#[instrument(skip_all, fields(n_addresses = addresses.len()))]
pub async fn connect(addresses: &[SocketAddr], service_name: String) -> Result<Self> {
let client = RedisClient::new(new_configuration(addresses, service_name)?);
client.client_setname("fred").await?; // TODO: use bin crate name.
#[instrument(skip_all, fields(url))]
pub async fn connect(url: &str) -> Result<Self> {
let config = {
let mut config = RedisConfig::from_url(url)?;
config.blocking = Blocking::Error;
config.tracing = true;
config.performance = PerformanceConfig {
pipeline: false,
..Default::default()
};
config
};

let client = RedisClient::new(config);
connect(&client).await?;
let script_hashes = load_scripts(&client).await?;
let this = Self {

Ok(Self {
client,
script_hashes,
};
Ok(this)
})
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -102,41 +111,6 @@ impl Redis {
}
}

fn new_configuration(addresses: &[SocketAddr], service_name: String) -> Result<RedisConfig> {
let config = RedisConfig {
server: match addresses.len() {
0 => {
bail!("at least one address is required");
}
1 => {
info!("assuming centralized configuration");
ServerConfig::Centralized {
host: addresses[0].ip().to_string(),
port: addresses[0].port(),
}
}
_ => {
info!(service_name = ?service_name, "assuming Sentinel configuration");
ServerConfig::Sentinel {
service_name,
hosts: addresses
.iter()
.map(|address| (address.ip().to_string(), address.port()))
.collect(),
}
}
},
blocking: Blocking::Error,
tracing: true,
performance: PerformanceConfig {
pipeline: false,
..Default::default()
},
..Default::default()
};
Ok(config)
}

#[instrument(skip_all)]
async fn connect(client: &RedisClient) -> Result<()> {
client.connect(None);
Expand Down
3 changes: 1 addition & 2 deletions rusty-tractive-telegram-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ async fn run(
) -> Result<()> {
let bot_api = BotApi::new(service_opts.bot_token, std::time::Duration::from_secs(5))?;
let me = methods::GetMe.call(&bot_api).await?;
let redis =
rusty_shared_redis::Redis::connect(&redis_opts.addresses, redis_opts.service_name).await?;
let redis = rusty_shared_redis::Redis::connect(&redis_opts.redis_url).await?;

let tracker_id = service_opts.tracker_id.to_lowercase();
let bot = { Bot::new(redis.clone().await?, bot_api.clone(), me.id) };
Expand Down
3 changes: 1 addition & 2 deletions rusty-tractive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ async fn run(
heartbeat_opts: rusty_shared_opts::heartbeat::Opts,
service_opts: ServiceOpts,
) -> Result<()> {
let redis =
rusty_shared_redis::Redis::connect(&redis_opts.addresses, redis_opts.service_name).await?;
let redis = rusty_shared_redis::Redis::connect(&redis_opts.redis_url).await?;

let service = Service {
api: Api::new()?,
Expand Down

0 comments on commit 7c534e0

Please sign in to comment.