Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(agent): extract config module
Browse files Browse the repository at this point in the history
Reisen committed Jun 26, 2024
1 parent 9db2592 commit 216ab59
Showing 2 changed files with 87 additions and 89 deletions.
91 changes: 2 additions & 89 deletions src/agent.rs
Original file line number Diff line number Diff line change
@@ -63,17 +63,18 @@ Note that there is an Oracle and Exporter for each network, but only one Local S
################################################################################################################################## */
use {
self::{
config::Config,
pyth::rpc,
solana::network,
},
anyhow::Result,
config::Config,
futures_util::future::join_all,
lazy_static::lazy_static,
std::sync::Arc,
tokio::sync::watch,
};

pub mod config;
pub mod legacy_schedule;
pub mod market_schedule;
pub mod metrics;
@@ -177,91 +178,3 @@ impl Agent {
Ok(())
}
}

pub mod config {
use {
super::{
metrics,
pyth,
services,
solana::network,
state,
},
anyhow::Result,
config as config_rs,
config_rs::{
Environment,
File,
},
serde::Deserialize,
std::path::Path,
};

/// Configuration for all components of the Agent
#[derive(Deserialize, Debug)]
pub struct Config {
#[serde(default)]
pub channel_capacities: ChannelCapacities,
pub primary_network: network::Config,
pub secondary_network: Option<network::Config>,
#[serde(default)]
#[serde(rename = "pythd_adapter")]
pub state: state::Config,
#[serde(default)]
pub pythd_api_server: pyth::rpc::Config,
#[serde(default)]
pub metrics_server: metrics::Config,
#[serde(default)]
pub remote_keypair_loader: services::keypairs::Config,
}

impl Config {
pub fn new(config_file: impl AsRef<Path>) -> Result<Self> {
// Build a new configuration object, allowing the default values to be
// overridden by those in the config_file or "AGENT_"-prefixed environment
// variables.
config_rs::Config::builder()
.add_source(File::from(config_file.as_ref()))
.add_source(Environment::with_prefix("agent"))
.build()?
.try_deserialize()
.map_err(|e| e.into())
}
}

/// Capacities of the channels top-level components use to communicate
#[derive(Deserialize, Debug)]
pub struct ChannelCapacities {
/// Capacity of the channel used to broadcast shutdown events to all components
pub shutdown: usize,
/// Capacity of the channel used to send updates from the primary Oracle to the Global Store
pub primary_oracle_updates: usize,
/// Capacity of the channel used to send updates from the secondary Oracle to the Global Store
pub secondary_oracle_updates: usize,
/// Capacity of the channel the Pythd API Adapter uses to send lookup requests to the Global Store
pub global_store_lookup: usize,
/// Capacity of the channel the Pythd API Adapter uses to communicate with the Local Store
pub local_store_lookup: usize,
/// Capacity of the channel on which the Local Store receives messages
pub local_store: usize,
/// Capacity of the channel on which the Pythd API Adapter receives messages
pub pythd_adapter: usize,
/// Capacity of the slog logging channel. Adjust this value if you see complaints about channel capacity from slog
pub logger_buffer: usize,
}

impl Default for ChannelCapacities {
fn default() -> Self {
Self {
shutdown: 10000,
primary_oracle_updates: 10000,
secondary_oracle_updates: 10000,
global_store_lookup: 10000,
local_store_lookup: 10000,
local_store: 10000,
pythd_adapter: 10000,
logger_buffer: 10000,
}
}
}
}
85 changes: 85 additions & 0 deletions src/agent/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use {
super::{
metrics,
pyth,
services,
solana::network,
state,
},
anyhow::Result,
config as config_rs,
config_rs::{
Environment,
File,
},
serde::Deserialize,
std::path::Path,
};

/// Configuration for all components of the Agent
#[derive(Deserialize, Debug)]
pub struct Config {
#[serde(default)]
pub channel_capacities: ChannelCapacities,
pub primary_network: network::Config,
pub secondary_network: Option<network::Config>,
#[serde(default)]
#[serde(rename = "pythd_adapter")]
pub state: state::Config,
#[serde(default)]
pub pythd_api_server: pyth::rpc::Config,
#[serde(default)]
pub metrics_server: metrics::Config,
#[serde(default)]
pub remote_keypair_loader: services::keypairs::Config,
}

impl Config {
pub fn new(config_file: impl AsRef<Path>) -> Result<Self> {
// Build a new configuration object, allowing the default values to be
// overridden by those in the config_file or "AGENT_"-prefixed environment
// variables.
config_rs::Config::builder()
.add_source(File::from(config_file.as_ref()))
.add_source(Environment::with_prefix("agent"))
.build()?
.try_deserialize()
.map_err(|e| e.into())
}
}

/// Capacities of the channels top-level components use to communicate
#[derive(Deserialize, Debug)]
pub struct ChannelCapacities {
/// Capacity of the channel used to broadcast shutdown events to all components
pub shutdown: usize,
/// Capacity of the channel used to send updates from the primary Oracle to the Global Store
pub primary_oracle_updates: usize,
/// Capacity of the channel used to send updates from the secondary Oracle to the Global Store
pub secondary_oracle_updates: usize,
/// Capacity of the channel the Pythd API Adapter uses to send lookup requests to the Global Store
pub global_store_lookup: usize,
/// Capacity of the channel the Pythd API Adapter uses to communicate with the Local Store
pub local_store_lookup: usize,
/// Capacity of the channel on which the Local Store receives messages
pub local_store: usize,
/// Capacity of the channel on which the Pythd API Adapter receives messages
pub pythd_adapter: usize,
/// Capacity of the slog logging channel. Adjust this value if you see complaints about channel capacity from slog
pub logger_buffer: usize,
}

impl Default for ChannelCapacities {
fn default() -> Self {
Self {
shutdown: 10000,
primary_oracle_updates: 10000,
secondary_oracle_updates: 10000,
global_store_lookup: 10000,
local_store_lookup: 10000,
local_store: 10000,
pythd_adapter: 10000,
logger_buffer: 10000,
}
}
}

0 comments on commit 216ab59

Please sign in to comment.