Skip to content

Commit

Permalink
Lift Arc in EndpointsCache
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed May 31, 2021
1 parent b1939fd commit 93e4e2e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
3 changes: 2 additions & 1 deletion beacon_node/eth1/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use parking_lot::RwLock;
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::sync::Arc;
use types::ChainSpec;

#[derive(Default)]
Expand All @@ -29,7 +30,7 @@ impl DepositUpdater {
pub struct Inner {
pub block_cache: RwLock<BlockCache>,
pub deposit_cache: RwLock<DepositUpdater>,
pub endpoints_cache: RwLock<Option<EndpointsCache>>,
pub endpoints_cache: RwLock<Option<Arc<EndpointsCache>>>,
pub config: RwLock<Config>,
pub remote_head_block: RwLock<Option<Eth1Block>>,
pub spec: ChainSpec,
Expand Down
14 changes: 6 additions & 8 deletions beacon_node/eth1/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ pub enum EndpointError {

type EndpointState = Result<(), EndpointError>;

#[derive(Clone)]
pub struct EndpointWithState {
endpoint: SensitiveUrl,
state: Arc<TRwLock<Option<EndpointState>>>,
state: TRwLock<Option<EndpointState>>,
}

impl EndpointWithState {
pub fn new(endpoint: SensitiveUrl) -> Self {
Self {
endpoint,
state: Arc::new(TRwLock::new(None)),
state: TRwLock::new(None),
}
}
}
Expand All @@ -80,7 +79,6 @@ async fn get_state(endpoint: &EndpointWithState) -> Option<EndpointState> {
/// A cache structure to lazily check usability of endpoints. An endpoint is usable if it is
/// reachable and has the correct network id and chain id. Emits a `WARN` log if a checked endpoint
/// is not usable.
#[derive(Clone)]
pub struct EndpointsCache {
pub fallback: Fallback<EndpointWithState>,
pub config_network_id: Eth1Id,
Expand Down Expand Up @@ -639,24 +637,24 @@ impl Service {
}

/// Builds a new `EndpointsCache` with empty states.
pub fn init_endpoints(&self) -> EndpointsCache {
pub fn init_endpoints(&self) -> Arc<EndpointsCache> {
let endpoints = self.config().endpoints.clone();
let config_network_id = self.config().network_id.clone();
let config_chain_id = self.config().chain_id.clone();
let new_cache = EndpointsCache {
let new_cache = Arc::new(EndpointsCache {
fallback: Fallback::new(endpoints.into_iter().map(EndpointWithState::new).collect()),
config_network_id,
config_chain_id,
log: self.log.clone(),
};
});

let mut endpoints_cache = self.inner.endpoints_cache.write();
*endpoints_cache = Some(new_cache.clone());
new_cache
}

/// Returns the cached `EndpointsCache` if it exists or builds a new one.
pub fn get_endpoints(&self) -> EndpointsCache {
pub fn get_endpoints(&self) -> Arc<EndpointsCache> {
let endpoints_cache = self.inner.endpoints_cache.read();
if let Some(cache) = endpoints_cache.clone() {
cache
Expand Down

0 comments on commit 93e4e2e

Please sign in to comment.