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

feat: add tokio-retry crate to retry failed requests #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "slog-loggly"
version = "0.5.0"
version = "0.5.1"
edition = "2018"
authors = ["Angelcam, Inc."]
license = "MIT/Apache-2.0"
Expand Down Expand Up @@ -28,6 +28,7 @@ hyper-tls = "0.5"
serde = "1.0"
serde_json = "1.0"
slog = "2.5"
tokio-retry = "0.3"

[dependencies.hyper]
version = "0.14"
Expand Down
8 changes: 6 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use hyper::{
};
use hyper_tls::HttpsConnector;

use crate::{batch::BatchStream, channel::Message, error::Error};
use crate::{batch::BatchStream, channel::Message, error::Error, retry::with_retry};

/// Default request timeout in seconds.
const DEFAULT_REQUEST_TIMEOUT: u64 = 5;
Expand Down Expand Up @@ -138,7 +138,11 @@ impl LogglyClient {

/// Try to send a given log message.
pub async fn try_send(&self, msg: Bytes) -> Result<(), Error> {
let send = tokio::time::timeout(self.timeout, self.try_send_inner(msg));
let action = || {
tokio::time::timeout(self.timeout, self.try_send_inner(msg.clone()))
};

let send = with_retry(None, None, action);

let res = match send.await {
Ok(Ok(())) => Ok(()),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ mod channel;
mod client;
mod error;
mod serializer;
mod retry;

use std::{str, sync::Mutex, time::Duration};

Expand Down
25 changes: 25 additions & 0 deletions src/retry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use tokio_retry::strategy::{FibonacciBackoff, jitter};
use std::future::Future;
use std::{
iter::{Take, Map},
time::Duration
};

fn retry_strategy(ms: u64, attempts: usize) -> Take<Map<FibonacciBackoff, fn(Duration) -> Duration>>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn retry_strategy(ms: u64, attempts: usize) -> Take<Map<FibonacciBackoff, fn(Duration) -> Duration>>{
fn retry_strategy(base: Duration, attempts: usize) -> impl Iterator<Item = Duration> {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function itself probably isn't necessary. It's very simple and it's used only once. It only bloats the code. I'd put the contents directly into with_retry.

FibonacciBackoff::from_millis(ms)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FibonacciBackoff::from_millis(ms)
FibonacciBackoff::from_millis(base.as_millis() as u64)

.map(jitter as fn(Duration) -> Duration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(jitter as fn(Duration) -> Duration)
.map(jitter)

.take(attempts)
}


pub async fn with_retry<A, F, R, E>(ms: Option<u64>, attempts: Option<usize>, action: A) -> Result<R, E>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub async fn with_retry<A, F, R, E>(ms: Option<u64>, attempts: Option<usize>, action: A) -> Result<R, E>
pub async fn with_retry<A, F, R, E>(base_delay: Duration, attempts: usize, action: A) -> Result<R, E>

Let's put the defaults into the client module as constants and make them configurable using LogglyClientBuilder.

where
A: FnMut() -> F,
E: std::fmt::Display,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bound probably isn't necessary. The tokio-retry crate does not require the error type to implement Display and the function itself does not format/display the error.

F: Future<Output=Result<R, E>>
{
let attempts = if let Some(attempts) = attempts { attempts } else { 10 };
let ms = if let Some(ms) = ms { ms } else { 1000 /*1 sec */};

tokio_retry::Retry::spawn(retry_strategy(ms, attempts), action).await
}