Skip to content

Commit f6d8342

Browse files
committed
Auto merge of #2785 - jtgeibel:double-nginx-worker-connections, r=pietroalbini
Double nginx worker connections This should double our burst capacity during large influxes of download traffic and reduce the occurance of H13 errors due to hitting the worker connection limit. With more queued connections we could potentially bump into limits on the number of open file handles. It looks like Heroku dynos have a ulimit on open files of: * 10,000 for Standard-1X dynos * 1,048,576 for Performance-M dynos Based on these numbers, we have a margin of 1,808 file handles for database connections, serving static files, etc. even on a Standard-1X dyno. r? `@pietroalbini`
2 parents a595001 + f372d6f commit f6d8342

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

config/nginx.conf.erb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
2323
events {
2424
use epoll;
2525
accept_mutex on;
26-
worker_connections 1024;
26+
worker_connections 2048;
2727
}
2828

2929
http {
@@ -33,32 +33,48 @@ http {
3333
real_ip_recursive on;
3434

3535
# CloudFront IP addresses from http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips
36-
# Last updated: 2020-05-22
36+
# Last updated: 2020-09-12
37+
set_real_ip_from 3.10.17.128/25;
38+
set_real_ip_from 3.11.53.0/24;
39+
set_real_ip_from 3.128.93.0/24;
40+
set_real_ip_from 3.134.215.0/24;
3741
set_real_ip_from 3.231.2.0/25;
3842
set_real_ip_from 3.234.232.224/27;
43+
set_real_ip_from 3.236.48.0/23;
44+
set_real_ip_from 3.236.169.192/26;
3945
set_real_ip_from 13.32.0.0/15;
4046
set_real_ip_from 13.35.0.0/16;
47+
set_real_ip_from 13.48.32.0/24;
4148
set_real_ip_from 13.54.63.128/26;
4249
set_real_ip_from 13.59.250.0/26;
50+
set_real_ip_from 13.113.196.64/26;
4351
set_real_ip_from 13.113.203.0/24;
4452
set_real_ip_from 13.124.199.0/24;
4553
set_real_ip_from 13.210.67.128/26;
4654
set_real_ip_from 13.224.0.0/14;
4755
set_real_ip_from 13.228.69.0/24;
56+
set_real_ip_from 13.233.177.192/26;
4857
set_real_ip_from 13.249.0.0/16;
58+
set_real_ip_from 15.188.184.0/24;
59+
set_real_ip_from 15.207.13.128/25;
60+
set_real_ip_from 15.207.213.128/25;
61+
set_real_ip_from 18.192.142.0/23;
4962
set_real_ip_from 18.200.212.0/23;
5063
set_real_ip_from 18.216.170.128/25;
64+
set_real_ip_from 18.229.220.192/26;
5165
set_real_ip_from 34.195.252.0/24;
5266
set_real_ip_from 34.216.51.0/25;
5367
set_real_ip_from 34.223.12.224/27;
5468
set_real_ip_from 34.223.80.192/26;
5569
set_real_ip_from 34.226.14.0/24;
56-
set_real_ip_from 34.232.163.208/29;
5770
set_real_ip_from 35.158.136.0/24;
5871
set_real_ip_from 35.162.63.192/26;
5972
set_real_ip_from 35.167.191.128/26;
6073
set_real_ip_from 36.103.232.0/25;
6174
set_real_ip_from 36.103.232.128/26;
75+
set_real_ip_from 44.227.178.0/24;
76+
set_real_ip_from 44.234.90.252/30;
77+
set_real_ip_from 44.234.108.128/25;
6278
set_real_ip_from 52.15.127.128/26;
6379
set_real_ip_from 52.46.0.0/18;
6480
set_real_ip_from 52.47.139.0/24;

src/middleware/balance_capacity.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ impl AroundMiddleware for BalanceCapacity {
7979

8080
impl Handler for BalanceCapacity {
8181
fn call(&self, request: &mut dyn RequestExt) -> AfterResult {
82+
// The _drop_on_exit ensures the counter is decremented for all exit paths (including panics)
8283
let (_drop_on_exit1, in_flight_total) = RequestCounter::add_one(&self.in_flight_total);
84+
85+
// Begin logging total request count so early stages of load increase can be located
8386
if in_flight_total >= self.log_total_at_count {
8487
super::log_request::add_custom_metadata(request, "in_flight_total", in_flight_total);
8588
}
8689

87-
// Download requests are always accepted and do not affect the request count
90+
// Download requests are always accepted and do not affect the capacity tracking
8891
if request.path().starts_with("/api/v1/crates/") && request.path().ends_with("/download") {
8992
return self.handle(request);
9093
}
@@ -93,7 +96,7 @@ impl Handler for BalanceCapacity {
9396
let (_drop_on_exit2, count) = RequestCounter::add_one(&self.in_flight_non_dl_requests);
9497
let load = 100 * count / self.db_capacity;
9598

96-
// Begin logging request count so early stages of load increase can be located
99+
// Begin logging non-download request count so early stages of non-download load increase can be located
97100
if load >= self.log_at_percentage {
98101
super::log_request::add_custom_metadata(request, "in_flight_non_dl_requests", count);
99102
}

src/middleware/require_user_agent.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
//! Middleware that blocks requests with no user-agent header
2+
//!
3+
//! By default the middleware will treat "" and "Amazon CloudFront" as a missing user-agent. To
4+
//! change the 2nd value, set `WEB_CDN_USER_AGENT` to the appropriate string. To disable the CDN
5+
//! check, set `WEB_CDN_USER_AGENT` to the empty string.
6+
//!
7+
//! Requests to the download endpoint are always allowed, to support versions of cargo older than
8+
//! 0.17 (released alongside rustc 1.17).
29
310
use super::prelude::*;
11+
use std::env;
412

513
use crate::util::request_header;
614

715
// Can't derive debug because of Handler.
816
#[allow(missing_debug_implementations)]
917
#[derive(Default)]
1018
pub struct RequireUserAgent {
19+
cdn_user_agent: String,
1120
handler: Option<Box<dyn Handler>>,
1221
}
1322

1423
impl AroundMiddleware for RequireUserAgent {
1524
fn with_handler(&mut self, handler: Box<dyn Handler>) {
25+
self.cdn_user_agent =
26+
env::var("WEB_CDN_USER_AGENT").unwrap_or_else(|_| "Amazon CloudFront".into());
1627
self.handler = Some(handler);
1728
}
1829
}
1930

2031
impl Handler for RequireUserAgent {
2132
fn call(&self, req: &mut dyn RequestExt) -> AfterResult {
22-
let has_user_agent = request_header(req, header::USER_AGENT) != "";
33+
let agent = request_header(req, header::USER_AGENT);
34+
let has_user_agent = agent != "" && agent != self.cdn_user_agent;
2335
let is_download = req.path().ends_with("download");
2436
if !has_user_agent && !is_download {
2537
super::log_request::add_custom_metadata(req, "cause", "no user agent");

0 commit comments

Comments
 (0)