Skip to content

Commit

Permalink
Send publisher permissions using a watch channel (#118)
Browse files Browse the repository at this point in the history
Fixes heap growth if Exporter::update_our_prices is not called as often as updates
  • Loading branch information
alexheretic authored Apr 25, 2024
1 parent dfebb4d commit 4985864
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 58 deletions.
11 changes: 5 additions & 6 deletions src/agent/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ pub mod network {

use {
super::{
super::{
store,
store::global,
super::store::{
self,
global,
},
exporter,
key_store::{
Expand All @@ -29,8 +29,8 @@ pub mod network {
std::time::Duration,
tokio::{
sync::{
mpsc,
mpsc::Sender,
watch,
},
task::JoinHandle,
},
Expand Down Expand Up @@ -86,8 +86,7 @@ pub mod network {
logger: Logger,
) -> Result<Vec<JoinHandle<()>>> {
// Publisher permissions updates between oracle and exporter
let (publisher_permissions_tx, publisher_permissions_rx) =
mpsc::channel(config.oracle.updates_channel_capacity);
let (publisher_permissions_tx, publisher_permissions_rx) = watch::channel(<_>::default());

// Spawn the Oracle
let mut jhs = oracle::spawn_oracle(
Expand Down
73 changes: 28 additions & 45 deletions src/agent/solana/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn spawn_exporter(
network: Network,
rpc_url: &str,
rpc_timeout: Duration,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_rx: watch::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
key_store: KeyStore,
local_store_tx: Sender<store::local::Message>,
global_store_tx: Sender<store::global::Lookup>,
Expand Down Expand Up @@ -260,7 +260,7 @@ pub struct Exporter {
inflight_transactions_tx: Sender<Signature>,

/// publisher => { permissioned_price => market hours } as read by the oracle module
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_rx: watch::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,

/// Currently known permissioned prices of this publisher along with their market hours
our_prices: HashMap<Pubkey, MarketSchedule>,
Expand All @@ -287,7 +287,7 @@ impl Exporter {
global_store_tx: Sender<store::global::Lookup>,
network_state_rx: watch::Receiver<NetworkState>,
inflight_transactions_tx: Sender<Signature>,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_rx: watch::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
keypair_request_tx: mpsc::Sender<KeypairRequest>,
logger: Logger,
) -> Self {
Expand Down Expand Up @@ -556,51 +556,34 @@ impl Exporter {
/// The loop ensures that we clear the channel and use
/// only the final, latest message; try_recv() is
/// non-blocking.
///
/// Note: This behavior is similar to
/// tokio::sync::watch::channel(), which was not appropriate here
/// because its internal RwLock would complain about not being
/// Send with the HashMap<HashSet<Pubkey>> inside.
/// TODO(2023-05-05): Debug the watch::channel() compilation errors
fn update_our_prices(&mut self, publish_pubkey: &Pubkey) {
loop {
match self.publisher_permissions_rx.try_recv() {
Ok(publisher_permissions) => {
self.our_prices = publisher_permissions.get(publish_pubkey) .cloned()
.unwrap_or_else( || {
warn!(
self.logger,
"Exporter: No permissioned prices were found for the publishing keypair on-chain. This is expected only on startup.";
"publish_pubkey" => publish_pubkey.to_string(),
);
HashMap::new()
});
trace!(
self.logger,
"Exporter: read permissioned price accounts from channel";
"new_value" => format!("{:?}", self.our_prices),
);
}
// Expected failures when channel is empty
Err(TryRecvError::Empty) => {
trace!(
self.logger,
"Exporter: No more permissioned price accounts in channel, using cached value";
);
break;
}
// Unexpected failures (channel closed, internal errors etc.)
Err(other) => {
warn!(
self.logger,
"Exporter: Updating permissioned price accounts failed unexpectedly, using cached value";
"cached_value" => format!("{:?}", self.our_prices),
"error" => other.to_string(),
);
break;
}
match self.publisher_permissions_rx.has_changed() {
Ok(true) => {}
Ok(false) => return,
Err(other) => {
warn!(
self.logger,
"Exporter: Updating permissioned price accounts failed unexpectedly, using cached value";
"cached_value" => format!("{:?}", self.our_prices),
"error" => other.to_string(),
);
return;
}
}

self.our_prices = self
.publisher_permissions_rx
.borrow_and_update()
.get(publish_pubkey)
.cloned()
.unwrap_or_else(|| {
warn!(
self.logger,
"Exporter: No permissioned prices were found for the publishing keypair on-chain. This is expected only on startup.";
"publish_pubkey" => publish_pubkey.to_string(),
);
HashMap::new()
});
}

async fn fetch_local_store_contents(&self) -> Result<HashMap<PriceIdentifier, PriceInfo>> {
Expand Down
15 changes: 8 additions & 7 deletions src/agent/solana/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ use {
time::Duration,
},
tokio::{
sync::mpsc,
sync::{
mpsc,
watch,
},
task::JoinHandle,
time::Interval,
},
Expand Down Expand Up @@ -204,7 +207,7 @@ pub fn spawn_oracle(
wss_url: &str,
rpc_timeout: Duration,
global_store_update_tx: mpsc::Sender<global::Update>,
publisher_permissions_tx: mpsc::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_tx: watch::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
key_store: KeyStore,
logger: Logger,
) -> Vec<JoinHandle<()>> {
Expand Down Expand Up @@ -419,7 +422,7 @@ struct Poller {
data_tx: mpsc::Sender<Data>,

/// Updates about permissioned price accounts from oracle to exporter
publisher_permissions_tx: mpsc::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_tx: watch::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,

/// The RPC client to use to poll data from the RPC node
rpc_client: RpcClient,
Expand All @@ -439,7 +442,7 @@ struct Poller {
impl Poller {
pub fn new(
data_tx: mpsc::Sender<Data>,
publisher_permissions_tx: mpsc::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
publisher_permissions_tx: watch::Sender<HashMap<Pubkey, HashMap<Pubkey, MarketSchedule>>>,
rpc_url: &str,
rpc_timeout: Duration,
commitment: CommitmentLevel,
Expand Down Expand Up @@ -481,9 +484,7 @@ impl Poller {
let fresh_data = self.poll().await?;

self.publisher_permissions_tx
.send(fresh_data.publisher_permissions.clone())
.await
.context("Updating permissioned price accounts for exporter")?;
.send_replace(fresh_data.publisher_permissions.clone());

self.data_tx
.send(fresh_data)
Expand Down

0 comments on commit 4985864

Please sign in to comment.