diff --git a/configuration/base.yaml b/configuration/base.yaml index 0c6780b..d2ae484 100644 --- a/configuration/base.yaml +++ b/configuration/base.yaml @@ -11,4 +11,5 @@ database: email_client: base_url: "localhost" sender_email: "test@gmail.com" - authorization_token: "my-secret-token" \ No newline at end of file + authorization_token: "my-secret-token" + timeout_milliseconds: 10000 \ No newline at end of file diff --git a/src/configuration.rs b/src/configuration.rs index fb46c8d..450027e 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -54,12 +54,17 @@ pub struct EmailClientSettings { pub base_url: String, pub sender_email: String, pub authorization_token: String, + pub timeout_milliseconds: u64, } impl EmailClientSettings { pub fn sender(&self) -> Result { SubscriberEmail::parse(self.sender_email.clone()) } + + pub fn timeout(&self) -> std::time::Duration { + std::time::Duration::from_millis(self.timeout_milliseconds) + } } pub fn get_configuration() -> Result { diff --git a/src/email_client.rs b/src/email_client.rs index 1343f55..cad139a 100644 --- a/src/email_client.rs +++ b/src/email_client.rs @@ -9,11 +9,13 @@ pub struct EmailClient { } impl EmailClient { - pub fn new(base_url: String, sender: SubscriberEmail, authorization_token: String) -> Self { - let http_client = Client::builder() - .timeout(std::time::Duration::from_secs(10)) - .build() - .unwrap(); + pub fn new( + base_url: String, + sender: SubscriberEmail, + authorization_token: String, + timeout: std::time::Duration, + ) -> Self { + let http_client = Client::builder().timeout(timeout).build().unwrap(); Self { http_client, base_url, @@ -103,7 +105,12 @@ mod tests { /// Get a test instance of `EmailClient`. fn email_client(base_url: String) -> EmailClient { - EmailClient::new(base_url, email(), Faker.fake()) + EmailClient::new( + base_url, + email(), + Faker.fake(), + std::time::Duration::from_millis(200), + ) } #[tokio::test] diff --git a/src/startup.rs b/src/startup.rs index be72d21..f363313 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -24,10 +24,12 @@ impl Application { .email_client .sender() .expect("Invalid sender email address."); + let timeout = configuration.email_client.timeout(); let email_client = EmailClient::new( configuration.email_client.base_url, sender_email, configuration.email_client.authorization_token, + timeout, ); let address = format!(