Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
add delayed start to heartbeats (#387)
Browse files Browse the repository at this point in the history
Adds a random initial jitter the size of the heartbeat periodicity to prevent heartbeats storming the service when we launch 3000 nodes roughly at the same time.

Fixes #386
  • Loading branch information
bmc-msft authored Jan 4, 2021
1 parent d038cca commit 3441790
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/agent/onefuzz/src/heartbeat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::utils::CheckNotify;
use crate::{jitter::random_delay, utils::CheckNotify};
use anyhow::Result;
use futures::Future;
use reqwest::Url;
Expand Down Expand Up @@ -83,6 +83,7 @@ where
TContext: Send + Sync + 'static,
{
let heartbeat_period = heartbeat_period.unwrap_or(DEFAULT_HEARTBEAT_PERIOD);

let context = Arc::new(HeartbeatContext {
state: context,
queue_client: QueueClient::new(queue_url),
Expand All @@ -92,6 +93,7 @@ where

let flush_context = context.clone();
let heartbeat_process = task::spawn(async move {
random_delay(heartbeat_period).await;
flush(flush_context.clone()).await;
while !flush_context.cancelled.is_notified(heartbeat_period).await {
flush(flush_context.clone()).await;
Expand Down
6 changes: 6 additions & 0 deletions src/agent/onefuzz/src/jitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ pub fn jitter(value: Duration) -> Duration {
pub async fn delay_with_jitter(value: Duration) {
delay_for(jitter(value)).await
}

pub async fn random_delay(value: Duration) {
let random: u64 = thread_rng().gen_range(0, value.as_secs());
let delay = Duration::new(random, 0);
delay_for(delay).await
}

0 comments on commit 3441790

Please sign in to comment.