Skip to content

Commit

Permalink
fix: do not create api client on every lookup (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot authored Dec 6, 2024
1 parent 0c27503 commit 56df9ef
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions coreclient/src/clients/api_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::collections::hash_map::Entry;
use std::{collections::HashMap, sync::Mutex};

use phnxtypes::identifiers::Fqdn;
Expand Down Expand Up @@ -40,10 +41,14 @@ impl ApiClients {
.clients
.lock()
.map_err(|_| ApiClientsError::MutexPoisonError)?;
let client = clients
.entry(lookup_domain.clone())
.or_insert(ApiClient::initialize(lookup_domain)?);
Ok(client.clone())
match clients.entry(lookup_domain.clone()) {
Entry::Occupied(entry) => Ok(entry.get().clone()),
Entry::Vacant(entry) => {
let client = ApiClient::initialize(lookup_domain.clone())?;
entry.insert(client.clone());
Ok(client)
}
}
}

pub(super) fn default_client(&self) -> Result<ApiClient, ApiClientsError> {
Expand Down

0 comments on commit 56df9ef

Please sign in to comment.