Skip to content

Commit

Permalink
feat: deserialize duration from milliseconds as a u64 in toml
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed May 20, 2022
1 parent 4d0371d commit 6fad310
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions book/src/sinks/gcp_pubsub.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ A sink that sends each event as a message to a PubSub topic. Each event is json-
type = "GcpPubSub"
credentials = "oura-test-347101-ff3f7b2d69cc.json"
topic = "test"

[sink.retry_policy]
max_retries = 30
backoff_unit = 5000
backoff_factor = 2
max_backoff = 100000
```

### Section: `sink`
Expand Down
8 changes: 6 additions & 2 deletions book/src/sinks/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ type = "Webhook"
url = "https://endpoint:5000/events"
authorization = "user:pass"
timeout = 30000
error_policy = "Retry"
error_policy = "Continue"

[sink.retry_policy]
max_retries = 30
backoff_delay = 5000
backoff_unit = 5000
backoff_factor = 2
max_backoff = 100000

[sink.headers]
extra_header_1 = "abc"
Expand Down
13 changes: 12 additions & 1 deletion src/utils/retry.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use std::{fmt::Debug, ops::Mul, time::Duration};

use serde::Deserialize;
use serde::{Deserialize, Deserializer};

#[derive(Debug, Deserialize, Default, Copy, Clone)]
pub struct Policy {
pub max_retries: u32,
#[serde(deserialize_with = "deserialize_duration")]
pub backoff_unit: Duration,
pub backoff_factor: u32,
#[serde(deserialize_with = "deserialize_duration")]
pub max_backoff: Duration,
}

fn deserialize_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let millis = u64::deserialize(deserializer)?;

Ok(Duration::from_millis(millis))
}

fn compute_backoff_delay(policy: &Policy, retry: u32) -> Duration {
let units = policy.backoff_factor.pow(retry);
let backoff = policy.backoff_unit.mul(units);
Expand Down

0 comments on commit 6fad310

Please sign in to comment.