Skip to content

Commit d99f64c

Browse files
committed
Avoid cloning Config struct
This struct contains a lot of owned data and cloning the entire struct is unnecessary.
1 parent 333dac0 commit d99f64c

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/admin/render_readmes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
schema::{crates, readme_renderings, versions},
66
Config,
77
};
8-
use std::{io::Read, path::Path, thread};
8+
use std::{io::Read, path::Path, sync::Arc, thread};
99

1010
use chrono::{TimeZone, Utc};
1111
use clap::Clap;
@@ -38,7 +38,7 @@ pub struct Opts {
3838
}
3939

4040
pub fn run(opts: Opts) {
41-
let config = Config::default();
41+
let config = Arc::new(Config::default());
4242
let conn = db::connect_now().unwrap();
4343

4444
let start_time = Utc::now();

src/bin/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3535
tracing_subscriber::fmt::init();
3636

3737
let config = cargo_registry::Config::default();
38+
let env = config.env;
3839
let client = Client::new();
39-
let app = Arc::new(App::new(config.clone(), Some(client)));
40+
let app = Arc::new(App::new(config, Some(client)));
4041

4142
// Start the background thread periodically persisting download counts to the database.
4243
downloads_counter_thread(app.clone());

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::publish_rate_limit::PublishRateLimit;
22
use crate::{env, uploaders::Uploader, Env, Replica};
33

4-
#[derive(Clone, Debug)]
4+
#[derive(Debug)]
55
pub struct Config {
66
pub uploader: Uploader,
77
pub session_key: String,
@@ -24,7 +24,7 @@ pub struct Config {
2424
pub metrics_authorization_token: Option<String>,
2525
}
2626

27-
#[derive(Clone, Debug)]
27+
#[derive(Debug)]
2828
pub struct DbPoolConfig {
2929
pub url: String,
3030
pub read_only_mode: bool,

src/middleware.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use crate::{App, Env};
3939

4040
pub fn build_middleware(app: Arc<App>, endpoints: RouteBuilder) -> MiddlewareBuilder {
4141
let mut m = MiddlewareBuilder::new(endpoints);
42-
let config = app.config.clone();
43-
let env = config.env;
42+
let env = app.config.env;
43+
let blocked_traffic = app.config.blocked_traffic.clone();
4444

4545
if env != Env::Test {
4646
m.add(ensure_well_formed_500::EnsureWellFormed500);
@@ -110,7 +110,7 @@ pub fn build_middleware(app: Arc<App>, endpoints: RouteBuilder) -> MiddlewareBui
110110

111111
m.around(Head::default());
112112

113-
for (header, blocked_values) in config.blocked_traffic {
113+
for (header, blocked_values) in blocked_traffic {
114114
m.around(block_traffic::BlockTraffic::new(header, blocked_values));
115115
}
116116

0 commit comments

Comments
 (0)