Skip to content
Closed
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
16 changes: 8 additions & 8 deletions packages/common/service-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use std::{future::Future, sync::Arc, time::Duration};

use rand::Rng;
use reqwest::Client;
use rivet_api::models::{ProvisionDatacentersGetServersResponse, ProvisionServer};
use tokio::{
sync::{Mutex, RwLock},
task::JoinHandle,
};
use url::Url;
use rivet_api::models::{ProvisionServer, ProvisionDatacentersGetServersResponse};

pub struct ServiceDiscovery {
fetch_endpoint: Url,
last: RwLock<Vec<ProvisionServer>>,
handle: Mutex<Option<JoinHandle<()>>>,
client: Client,
}

impl ServiceDiscovery {
Expand All @@ -21,6 +22,7 @@ impl ServiceDiscovery {
fetch_endpoint,
last: RwLock::new(Vec::new()),
handle: Mutex::new(None),
client: Client::new(),
})
}

Expand All @@ -36,10 +38,8 @@ impl ServiceDiscovery {

let self2 = self.clone();
*guard = Some(tokio::task::spawn(async move {
let client = Client::new();

loop {
let res = match self2.fetch_inner(&client).await {
let res = match self2.fetch_inner().await {
Ok(res) => res,
Err(err) => {
tracing::error!(?err, "fetch service discovery failed");
Expand Down Expand Up @@ -70,12 +70,12 @@ impl ServiceDiscovery {

/// Manually fetches the endpoint.
pub async fn fetch(&self) -> Result<Vec<ProvisionServer>, reqwest::Error> {
let client = Client::new();
Ok(self.fetch_inner(&client).await?.servers)
Ok(self.fetch_inner().await?.servers)
}

async fn fetch_inner(&self, client: &Client) -> Result<ProvisionDatacentersGetServersResponse, reqwest::Error> {
Ok(client
async fn fetch_inner(&self) -> Result<ProvisionDatacentersGetServersResponse, reqwest::Error> {
Ok(self
.client
.get(self.fetch_endpoint.clone())
Copy link

Choose a reason for hiding this comment

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

logic: clone() on Url for each request is unnecessary since Client already handles concurrent requests safely. Use &self.fetch_endpoint

Suggested change
.get(self.fetch_endpoint.clone())
.get(&self.fetch_endpoint)

.send()
.await?
Expand Down
Loading