Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stats dymanic reload #87

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod sharding;
mod stats;

use config::{get_config, reload_config};
use pool::{get_pool, ClientServerMap, ConnectionPool};
use pool::{ClientServerMap, ConnectionPool};
use stats::{Collector, Reporter, REPORTER};

#[tokio::main(worker_threads = 4)]
Expand Down Expand Up @@ -120,25 +120,20 @@ async fn main() {
}
};

let pool = get_pool();

// Statistics collector task.
let collector_tx = tx.clone();

// Save these for reloading
let reload_client_server_map = client_server_map.clone();
let autoreload_client_server_map = client_server_map.clone();

let addresses = pool.databases();
tokio::task::spawn(async move {
let mut stats_collector = Collector::new(rx, collector_tx);
stats_collector.collect(addresses).await;
stats_collector.collect().await;
});

info!("Waiting for clients");

drop(pool);

// Client connection loop.
tokio::task::spawn(async move {
loop {
Expand Down
6 changes: 5 additions & 1 deletion src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use parking_lot::Mutex;
use std::collections::HashMap;
use tokio::sync::mpsc::{channel, Receiver, Sender};

use crate::pool::get_pool;

pub static REPORTER: Lazy<ArcSwap<Reporter>> =
Lazy::new(|| ArcSwap::from_pointee(Reporter::default()));

Expand Down Expand Up @@ -285,7 +287,7 @@ impl Collector {

/// The statistics collection handler. It will collect statistics
/// for `address_id`s starting at 0 up to `addresses`.
pub async fn collect(&mut self, addresses: usize) {
pub async fn collect(&mut self) {
info!("Events reporter started");

let stats_template = HashMap::from([
Expand Down Expand Up @@ -329,6 +331,7 @@ impl Collector {
tokio::time::interval(tokio::time::Duration::from_millis(STAT_PERIOD / 15));
loop {
interval.tick().await;
let addresses = get_pool().databases();
for address_id in 0..addresses {
let _ = tx.try_send(Event {
name: EventName::UpdateStats,
Expand All @@ -346,6 +349,7 @@ impl Collector {
tokio::time::interval(tokio::time::Duration::from_millis(STAT_PERIOD));
loop {
interval.tick().await;
let addresses = get_pool().databases();
for address_id in 0..addresses {
let _ = tx.try_send(Event {
name: EventName::UpdateAverages,
Expand Down