Skip to content

Commit

Permalink
caches aren't actualy optional on buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
BlinkyStitt committed Jul 21, 2023
1 parent 3334dc5 commit 7678cb7
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 34 deletions.
9 changes: 2 additions & 7 deletions web3_proxy/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use tokio::sync::{broadcast, mpsc, oneshot, watch, Semaphore};
use tokio::task::JoinHandle;
use tokio::time::{sleep, timeout};
use tracing::{error, info, trace, warn, Level};
use ulid::Ulid;

// TODO: make this customizable?
// TODO: include GIT_REF in here. i had trouble getting https://docs.rs/vergen/latest/vergen/ to work with a workspace. also .git is in .dockerignore
Expand Down Expand Up @@ -320,9 +319,6 @@ impl Web3ProxyApp {
.build()
.into();

// Generate the instance name
let instance_hash = Ulid::new().to_string();

// create a channel for receiving stats
// we do this in a channel so we don't slow down our response to the users
// stats can be saved in mysql, influxdb, both, or none
Expand All @@ -332,13 +328,12 @@ impl Web3ProxyApp {
60,
top_config.app.influxdb_bucket.clone(),
influxdb_client.clone(),
Some(rpc_secret_key_cache.clone()),
Some(user_balance_cache.clone()),
rpc_secret_key_cache.clone(),
user_balance_cache.clone(),
stat_buffer_shutdown_receiver,
1,
flush_stat_buffer_sender.clone(),
flush_stat_buffer_receiver,
instance_hash,
)? {
// since the database entries are used for accounting, we want to be sure everything is saved before exiting
important_background_handles.push(spawned_stat_buffer.background_handle);
Expand Down
20 changes: 7 additions & 13 deletions web3_proxy/src/stats/stat_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::time::Duration;
use tokio::sync::{broadcast, mpsc, oneshot};
use tokio::time::{interval, sleep};
use tracing::{error, info, trace, warn};
use ulid::Ulid;

#[derive(Debug, Default)]
pub struct BufferedRpcQueryStats {
Expand Down Expand Up @@ -68,13 +69,12 @@ impl StatBuffer {
db_save_interval_seconds: u32,
influxdb_bucket: Option<String>,
mut influxdb_client: Option<influxdb2::Client>,
rpc_secret_key_cache: Option<RpcSecretKeyCache>,
user_balance_cache: Option<UserBalanceCache>,
rpc_secret_key_cache: RpcSecretKeyCache,
user_balance_cache: UserBalanceCache,
shutdown_receiver: broadcast::Receiver<()>,
tsdb_save_interval_seconds: u32,
flush_sender: mpsc::Sender<oneshot::Sender<FlushedStats>>,
flush_receiver: mpsc::Receiver<oneshot::Sender<FlushedStats>>,
instance_hash: String,
) -> anyhow::Result<Option<SpawnedStatBuffer>> {
if influxdb_bucket.is_none() {
influxdb_client = None;
Expand All @@ -84,6 +84,8 @@ impl StatBuffer {

let timestamp_precision = TimestampPrecision::Seconds;

let instance_hash = Ulid::new().to_string();

let mut new = Self {
accounting_db_buffer: Default::default(),
billing_period_seconds,
Expand All @@ -94,10 +96,10 @@ impl StatBuffer {
influxdb_client,
instance_hash,
opt_in_timeseries_buffer: Default::default(),
rpc_secret_key_cache: rpc_secret_key_cache.unwrap(),
rpc_secret_key_cache,
timestamp_precision,
tsdb_save_interval_seconds,
user_balance_cache: user_balance_cache.unwrap(),
user_balance_cache,

_flush_sender: flush_sender,
};
Expand Down Expand Up @@ -437,11 +439,3 @@ impl StatBuffer {
count
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_something() {
panic!()
}
}
9 changes: 5 additions & 4 deletions web3_proxy/src/sub_commands/migrate_stats_to_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use migration::sea_orm::{
ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect, UpdateResult,
};
use migration::{Expr, Value};
use moka::future::Cache;
use parking_lot::Mutex;
use std::num::NonZeroU64;
use std::sync::Arc;
Expand Down Expand Up @@ -74,7 +75,8 @@ impl MigrateStatsToV2SubCommand {

let (flush_sender, flush_receiver) = mpsc::channel(1);

let instance_hash = Ulid::new().to_string();
let rpc_secret_key_cache = Cache::builder().build();
let user_balance_cache = Cache::builder().build().into();

// Spawn the stat-sender
let emitter_spawn = StatBuffer::try_spawn(
Expand All @@ -83,13 +85,12 @@ impl MigrateStatsToV2SubCommand {
30,
top_config.app.influxdb_bucket.clone(),
influxdb_client.clone(),
None,
None,
rpc_secret_key_cache,
user_balance_cache,
rpc_account_shutdown_recevier,
1,
flush_sender,
flush_receiver,
instance_hash,
)
.context("Error spawning stat buffer")?
.context("No stat buffer spawned. Maybe missing influx or db credentials?")?;
Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/admin_deposits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TestApp;
use super::TestApp;
use tracing::trace;
use web3_proxy::frontend::users::authentication::LoginPostResponse;

Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/admin_increases_balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TestApp;
use super::TestApp;
use ethers::prelude::{LocalWallet, Signer};
use migration::sea_orm::prelude::Decimal;
use tracing::info;
Expand Down
1 change: 1 addition & 0 deletions web3_proxy/tests/common/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct TestAnvil {
}

impl TestAnvil {
#[allow(unused)]
pub async fn spawn(chain_id: u64) -> Self {
info!(?chain_id);

Expand Down
1 change: 1 addition & 0 deletions web3_proxy/tests/common/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct TestApp {
}

impl TestApp {
#[allow(unused)]
pub async fn spawn(
anvil: &TestAnvil,
db: Option<&TestMysql>,
Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/create_admin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TestApp;
use super::TestApp;
use ethers::prelude::{LocalWallet, Signer};
use ethers::types::Signature;
use http::StatusCode;
Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/create_user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TestApp;
use super::TestApp;
use entities::{user, user_tier};
use ethers::prelude::{LocalWallet, Signer};
use ethers::types::Signature;
Expand Down
3 changes: 3 additions & 0 deletions web3_proxy/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub mod rpc_key;
pub mod stats_accounting;
pub mod user_balance;

pub use self::anvil::TestAnvil;
pub use self::app::TestApp;
pub use self::influx::TestInflux;
pub use self::mysql::TestMysql;
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/referral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// Includes
/// - get referral link
/// - getting code for referral (shared and used)
use crate::TestApp;
use super::TestApp;
use tracing::info;
use ulid::Ulid;
use web3_proxy::frontend::users::authentication::LoginPostResponse;
Expand Down
5 changes: 2 additions & 3 deletions web3_proxy/tests/common/rpc_key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::time::Duration;

use crate::TestApp;
use super::TestApp;
use serde::Deserialize;
use std::time::Duration;
use tracing::info;
use ulid::Ulid;
use web3_proxy::{
Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/stats_accounting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::TestApp;
use super::TestApp;
use serde_json::json;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{info, trace};
Expand Down
2 changes: 1 addition & 1 deletion web3_proxy/tests/common/user_balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::TestApp;
use super::TestApp;
use serde_json::json;
use tracing::{info, trace};
use web3_proxy::balance::Balance;
Expand Down

0 comments on commit 7678cb7

Please sign in to comment.