Skip to content

Commit

Permalink
refactor: replace lazy_static with LazyLock (#1064)
Browse files Browse the repository at this point in the history
Update to Rust 1.82.0: `std::sync::LazyLock` was introduced in 1.80.0 (2024-07-25).
Since the version is being bumped, we bump to latest, i.e., 1.82.0 (2024-10-17).
  • Loading branch information
allan2 authored Oct 22, 2024
1 parent 193630f commit a4b432a
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 256 deletions.
406 changes: 196 additions & 210 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion crates/mock-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false
[dependencies]
tokio = { version = "1.38", features = ["io-util", "sync"] }
loom = { version = "0.7", features = ["futures", "checkpoint"] }
lazy_static = "1.5"

[dev-dependencies]
tokio = { version = "1.38", features = ["rt"] }
8 changes: 4 additions & 4 deletions crates/mock-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
use std::pin::Pin;
use std::sync::LazyLock;
use tokio::io::{AsyncRead, AsyncWrite, DuplexStream};
use tokio::sync::{mpsc, Mutex, Notify};

lazy_static::lazy_static! {
static ref LISTENERS: Mutex<HashMap<SocketAddr, mpsc::Sender<DuplexStream>>> = Mutex::new(HashMap::new());
static ref NEW_LISTENER: Notify = Notify::new();
}
static LISTENERS: LazyLock<Mutex<HashMap<SocketAddr, mpsc::Sender<DuplexStream>>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
static NEW_LISTENER: LazyLock<Notify> = LazyLock::new(|| Notify::new());

#[derive(Debug)]
pub struct TcpListener(
Expand Down
1 change: 0 additions & 1 deletion devolutions-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ camino = { version = "1.1", features = ["serde1"] }
smol_str = { version = "0.2", features = ["serde"] }
nonempty = "0.10"
tap = "1.0"
lazy_static = "1.5" # FIXME: replace lazy_static by std’s OnceLock
bytes = "1.6"
cfg-if = "1.0"
url = { version = "2.5", features = ["serde"] }
Expand Down
8 changes: 3 additions & 5 deletions devolutions-gateway/src/api/webapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,18 +503,16 @@ fn ensure_enabled(conf: &crate::config::Conf) -> Result<(), HttpError> {
mod login_rate_limit {
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::LazyLock;
use std::time::Duration;

use lazy_static::lazy_static;
use parking_lot::Mutex;
use std::time::Instant;

type LoginAttempts = Mutex<HashMap<(String, IpAddr), u8>>;

lazy_static! {
static ref LOGIN_ATTEMPTS: LoginAttempts = Mutex::new(HashMap::new());
static ref LAST_RESET: Mutex<Instant> = Mutex::new(Instant::now());
}
static LOGIN_ATTEMPTS: LazyLock<LoginAttempts> = LazyLock::new(|| Mutex::new(HashMap::new()));
static LAST_RESET: LazyLock<Mutex<Instant>> = LazyLock::new(|| Mutex::new(Instant::now()));

const PERIOD: Duration = Duration::from_secs(60);

Expand Down
10 changes: 5 additions & 5 deletions devolutions-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use utoipa as _;
use {
argon2 as _, async_trait as _, axum as _, axum_extra as _, backoff as _, bytes as _, camino as _,
devolutions_agent_shared as _, dlopen as _, dlopen_derive as _, etherparse as _, hostname as _, hyper as _,
hyper_util as _, ironrdp_pdu as _, ironrdp_rdcleanpath as _, jmux_proxy as _, lazy_static as _, multibase as _,
network_scanner as _, ngrok as _, nonempty as _, pcap_file as _, picky as _, picky_krb as _, pin_project_lite as _,
portpicker as _, reqwest as _, serde as _, serde_urlencoded as _, smol_str as _, sysinfo as _, thiserror as _,
time as _, tokio_rustls as _, tower as _, tower_http as _, transport as _, tungstenite as _, typed_builder as _,
url as _, uuid as _, zeroize as _,
hyper_util as _, ironrdp_pdu as _, ironrdp_rdcleanpath as _, jmux_proxy as _, multibase as _, network_scanner as _,
ngrok as _, nonempty as _, pcap_file as _, picky as _, picky_krb as _, pin_project_lite as _, portpicker as _,
reqwest as _, serde as _, serde_urlencoded as _, smol_str as _, sysinfo as _, thiserror as _, time as _,
tokio_rustls as _, tower as _, tower_http as _, transport as _, tungstenite as _, typed_builder as _, url as _,
uuid as _, zeroize as _,
};

// Used by tests.
Expand Down
8 changes: 3 additions & 5 deletions devolutions-gateway/src/plugin_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ pub use recording::Recorder;
use anyhow::Context as _;
use camino::Utf8Path;
use dlopen::symbor::Library;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use plugin_info::{PluginCapabilities, PluginInformation};
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use crate::config::Conf;

Expand All @@ -25,9 +24,8 @@ pub struct PluginManager {
libs: Vec<Plugin>,
}

lazy_static! {
pub static ref PLUGIN_MANAGER: Mutex<PluginManager> = Mutex::new(PluginManager { libs: Vec::new() });
}
pub static PLUGIN_MANAGER: LazyLock<Mutex<PluginManager>> =
LazyLock::new(|| Mutex::new(PluginManager { libs: Vec::new() }));

impl PluginManager {
pub fn get_recording_plugin(&self) -> Option<Recorder> {
Expand Down
46 changes: 22 additions & 24 deletions devolutions-gateway/src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use anyhow::Context as _;
use tokio::net::TcpStream;
Expand All @@ -8,31 +8,29 @@ use tokio_rustls::rustls::{self, pki_types};

static DEFAULT_CIPHER_SUITES: &[rustls::SupportedCipherSuite] = rustls::crypto::ring::DEFAULT_CIPHER_SUITES;

lazy_static::lazy_static! {
// rustls doc says:
// rustls doc says:
//
// > Making one of these can be expensive, and should be once per process rather than once per connection.
//
// source: https://docs.rs/rustls/0.21.1/rustls/client/struct.ClientConfig.html
//
// We’ll reuse the same TLS client config for all proxy-based TLS connections.
// (TlsConnector is just a wrapper around the config providing the `connect` method.)
static TLS_CONNECTOR: LazyLock<tokio_rustls::TlsConnector> = LazyLock::new(|| {
let mut config = rustls::client::ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(Arc::new(danger::NoCertificateVerification))
.with_no_client_auth();

// Disable TLS resumption because it’s not supported by some services such as CredSSP.
//
// > Making one of these can be expensive, and should be once per process rather than once per connection.
// > The CredSSP Protocol does not extend the TLS wire protocol. TLS session resumption is not supported.
//
// source: https://docs.rs/rustls/0.21.1/rustls/client/struct.ClientConfig.html
//
// We’ll reuse the same TLS client config for all proxy-based TLS connections.
// (TlsConnector is just a wrapper around the config providing the `connect` method.)
static ref TLS_CONNECTOR: tokio_rustls::TlsConnector = {
let mut config = rustls::client::ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(Arc::new(danger::NoCertificateVerification))
.with_no_client_auth();

// Disable TLS resumption because it’s not supported by some services such as CredSSP.
//
// > The CredSSP Protocol does not extend the TLS wire protocol. TLS session resumption is not supported.
//
// source: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/385a7489-d46b-464c-b224-f7340e308a5c
config.resumption = rustls::client::Resumption::disabled();

tokio_rustls::TlsConnector::from(Arc::new(config))
};
}
// source: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/385a7489-d46b-464c-b224-f7340e308a5c
config.resumption = rustls::client::Resumption::disabled();

tokio_rustls::TlsConnector::from(Arc::new(config))
});

pub async fn connect(dns_name: &str, stream: TcpStream) -> io::Result<TlsStream<TcpStream>> {
use tokio::io::AsyncWriteExt as _;
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.79.0"
channel = "1.82.0"
components = ["rustfmt", "clippy"]

0 comments on commit a4b432a

Please sign in to comment.