improvement: cache actual destination

This commit is contained in:
Timo Kösters 2020-12-06 11:05:51 +01:00
parent 45086b54b3
commit d62f17a91a
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
4 changed files with 74 additions and 35 deletions

View file

@ -1,20 +1,25 @@
use crate::{database::Config, utils, Error, Result};
use trust_dns_resolver::TokioAsyncResolver;
use std::collections::HashMap;
use log::error;
use ruma::ServerName;
use std::sync::Arc;
use std::sync::RwLock;
pub const COUNTER: &str = "c";
#[derive(Clone)]
pub struct Globals {
pub(super) globals: sled::Tree,
config: Config,
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
reqwest_client: reqwest::Client,
config: Config,
pub actual_destination_cache: Arc<RwLock<HashMap<Box<ServerName>, (String, Option<String>)>>>, // actual_destination, host
dns_resolver: TokioAsyncResolver,
}
impl Globals {
pub fn load(globals: sled::Tree, config: Config) -> Result<Self> {
pub async fn load(globals: sled::Tree, config: Config) -> Result<Self> {
let bytes = &*globals
.update_and_fetch("keypair", utils::generate_keypair)?
.expect("utils::generate_keypair always returns Some");
@ -51,9 +56,13 @@ impl Globals {
Ok(Self {
globals,
config,
keypair: Arc::new(keypair),
reqwest_client: reqwest::Client::new(),
config,
dns_resolver: TokioAsyncResolver::tokio_from_system_conf().await.map_err(|_| {
Error::bad_config("Failed to set up trust dns resolver with system config.")
})?,
actual_destination_cache: Arc::new(RwLock::new(HashMap::new())),
})
}
@ -103,4 +112,8 @@ impl Globals {
pub fn federation_enabled(&self) -> bool {
self.config.federation_enabled
}
pub fn dns_resolver(&self) -> &TokioAsyncResolver {
&self.dns_resolver
}
}