Skip to content

Commit

Permalink
fix(cyclotron): make all config envconfig (#24538)
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner authored Aug 23, 2024
1 parent a7da933 commit e6ad4a9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
72 changes: 40 additions & 32 deletions rust/cyclotron-fetch/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,39 @@ pub struct Config {
#[envconfig(default = "false")]
pub allow_internal_ips: bool,

pub worker_id: Option<String>, // Default to a UUID
pub job_poll_interval_seconds: Option<u32>, // Defaults to 1
pub concurrent_requests_limit: Option<u32>, // Defaults to 1000
pub fetch_timeout_seconds: Option<u32>, // Defaults to 30
pub max_retry_attempts: Option<u32>, // Defaults to 10
pub queue_served: Option<String>, // Default to "fetch"
pub batch_size: Option<usize>, // Defaults to 1000
pub max_response_bytes: Option<usize>, // Defaults to 1MB
pub retry_backoff_base_ms: Option<u32>, // Defaults to 4000
#[envconfig(default = "default_worker_id")]
pub worker_id: String,

#[envconfig(default = "1")]
pub job_poll_interval_seconds: i64,

#[envconfig(default = "1000")]
pub concurrent_requests_limit: u32,

#[envconfig(default = "30")]
pub fetch_timeout_seconds: i64,

#[envconfig(default = "10")]
pub max_retry_attempts: u32,

#[envconfig(default = "fetch")]
pub queue_served: String,

#[envconfig(default = "1000")]
pub batch_size: usize,

#[envconfig(default = "1000000")]
pub max_response_bytes: usize,

#[envconfig(default = "4000")]
pub retry_backoff_base_ms: i64,
}

#[allow(dead_code)]
fn default_worker_id() -> String {
Uuid::now_v7().to_string()
}

// I do this instead of using envconfig's defaults because
// envconfig doesn't support defaults provided by functions,
// which is frustrating when I want to use UUIDs, and if I'm
// going to break out one field, I might as well break out
// everything into "AppConfig" and "PoolConfig"
#[derive(Debug, Clone)]
pub struct AppConfig {
pub host: String,
Expand All @@ -66,27 +83,18 @@ pub struct AppConfig {

impl Config {
pub fn to_components(self) -> (AppConfig, PoolConfig) {
let worker_id = self.worker_id.unwrap_or_else(|| Uuid::now_v7().to_string());
let job_poll_interval_seconds = self.job_poll_interval_seconds.unwrap_or(1);
let concurrent_requests_limit = self.concurrent_requests_limit.unwrap_or(1000);
let fetch_timeout_seconds = self.fetch_timeout_seconds.unwrap_or(30);
let max_retry_attempts = self.max_retry_attempts.unwrap_or(10);
let queue_served = self.queue_served.unwrap_or_else(|| "fetch".to_string());

let app_config = AppConfig {
host: self.host,
port: self.port,
worker_id,
job_poll_interval: Duration::seconds(job_poll_interval_seconds as i64),
concurrent_requests_limit,
fetch_timeout: Duration::seconds(fetch_timeout_seconds as i64),
max_retry_attempts,
queue_served,
batch_size: self.batch_size.unwrap_or(1000),
max_response_bytes: self.max_response_bytes.unwrap_or(1024 * 1024),
retry_backoff_base: Duration::milliseconds(
self.retry_backoff_base_ms.unwrap_or(4000) as i64
),
worker_id: self.worker_id,
job_poll_interval: Duration::seconds(self.job_poll_interval_seconds),
concurrent_requests_limit: self.concurrent_requests_limit,
fetch_timeout: Duration::seconds(self.fetch_timeout_seconds),
max_retry_attempts: self.max_retry_attempts,
queue_served: self.queue_served,
batch_size: self.batch_size,
max_response_bytes: self.max_response_bytes,
retry_backoff_base: Duration::milliseconds(self.retry_backoff_base_ms),
allow_internal_ips: self.allow_internal_ips,
};

Expand Down
13 changes: 8 additions & 5 deletions rust/cyclotron-janitor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub struct Config {

// Generally, this should be equivalent to a "shard id", as only one janitor should be running
// per shard
pub janitor_id: Option<String>,
#[envconfig(default = "default_janitor_id")]
pub janitor_id: String,

#[envconfig(default = "10")]
pub janitor_max_touches: i16,
Expand All @@ -44,6 +45,11 @@ pub struct Config {
pub janitor_stall_timeout_seconds: u16,
}

#[allow(dead_code)]
fn default_worker_id() -> String {
Uuid::now_v7().to_string()
}

impl Config {
pub fn get_janitor_config(&self) -> JanitorConfig {
let pool_config = PoolConfig {
Expand All @@ -58,10 +64,7 @@ impl Config {
let settings = JanitorSettings {
stall_timeout: Duration::seconds(self.janitor_stall_timeout_seconds as i64),
max_touches: self.janitor_max_touches,
id: self
.janitor_id
.clone()
.unwrap_or_else(|| Uuid::now_v7().to_string()),
id: self.janitor_id.clone(),
};

JanitorConfig {
Expand Down
2 changes: 1 addition & 1 deletion rust/feature-flags/src/v0_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn flags(
tracing::Span::current().record("method", method.as_str());
tracing::Span::current().record("path", path.as_str().trim_end_matches('/'));
tracing::Span::current().record("ip", ip.to_string());
tracing::Span::current().record("sent_at", &sent_at.to_string());
tracing::Span::current().record("sent_at", sent_at.to_string());

tracing::debug!("request headers: {:?}", headers);

Expand Down

0 comments on commit e6ad4a9

Please sign in to comment.