Skip to content

Commit f157393

Browse files
authored
Remove MAX_CONN (#316)
1 parent 3859e91 commit f157393

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

actix-server/src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::signals::{Signal, Signals};
1919
use crate::socket::{MioListener, StdSocketAddr, StdTcpListener, ToSocketAddrs};
2020
use crate::socket::{MioTcpListener, MioTcpSocket};
2121
use crate::waker_queue::{WakerInterest, WakerQueue};
22-
use crate::worker::{self, ServerWorker, ServerWorkerConfig, WorkerAvailability, WorkerHandle};
22+
use crate::worker::{ServerWorker, ServerWorkerConfig, WorkerAvailability, WorkerHandle};
2323
use crate::{join_all, Token};
2424

2525
/// Server builder
@@ -117,8 +117,8 @@ impl ServerBuilder {
117117
/// reached for each worker.
118118
///
119119
/// By default max connections is set to a 25k per worker.
120-
pub fn maxconn(self, num: usize) -> Self {
121-
worker::max_concurrent_connections(num);
120+
pub fn maxconn(mut self, num: usize) -> Self {
121+
self.worker_config.max_concurrent_connections(num);
122122
self
123123
}
124124

actix-server/src/worker.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
mem,
44
pin::Pin,
55
sync::{
6-
atomic::{AtomicBool, AtomicUsize, Ordering},
6+
atomic::{AtomicBool, Ordering},
77
Arc,
88
},
99
task::{Context, Poll},
@@ -43,27 +43,6 @@ pub(crate) struct Conn {
4343
pub token: Token,
4444
}
4545

46-
static MAX_CONNS: AtomicUsize = AtomicUsize::new(25600);
47-
48-
/// Sets the maximum per-worker number of concurrent connections.
49-
///
50-
/// All socket listeners will stop accepting connections when this limit is
51-
/// reached for each worker.
52-
///
53-
/// By default max connections is set to a 25k per worker.
54-
pub fn max_concurrent_connections(num: usize) {
55-
MAX_CONNS.store(num, Ordering::Relaxed);
56-
}
57-
58-
thread_local! {
59-
static MAX_CONNS_COUNTER: Counter =
60-
Counter::new(MAX_CONNS.load(Ordering::Relaxed));
61-
}
62-
63-
pub(crate) fn num_connections() -> usize {
64-
MAX_CONNS_COUNTER.with(|conns| conns.total())
65-
}
66-
6746
// a handle to worker that can send message to worker and share the availability of worker to other
6847
// thread.
6948
#[derive(Clone)]
@@ -173,6 +152,7 @@ enum WorkerServiceStatus {
173152
pub(crate) struct ServerWorkerConfig {
174153
shutdown_timeout: Duration,
175154
max_blocking_threads: usize,
155+
max_concurrent_connections: usize,
176156
}
177157

178158
impl Default for ServerWorkerConfig {
@@ -182,6 +162,7 @@ impl Default for ServerWorkerConfig {
182162
Self {
183163
shutdown_timeout: Duration::from_secs(30),
184164
max_blocking_threads,
165+
max_concurrent_connections: 25600,
185166
}
186167
}
187168
}
@@ -191,6 +172,10 @@ impl ServerWorkerConfig {
191172
self.max_blocking_threads = num;
192173
}
193174

175+
pub(crate) fn max_concurrent_connections(&mut self, num: usize) {
176+
self.max_concurrent_connections = num;
177+
}
178+
194179
pub(crate) fn shutdown_timeout(&mut self, dur: Duration) {
195180
self.shutdown_timeout = dur;
196181
}
@@ -218,16 +203,16 @@ impl ServerWorker {
218203
})
219204
.spawn(async move {
220205
availability.set(false);
221-
let mut wrk = MAX_CONNS_COUNTER.with(move |conns| ServerWorker {
206+
let mut wrk = ServerWorker {
222207
rx,
223208
rx2,
224209
services: Vec::new(),
225210
availability,
211+
conns: Counter::new(config.max_concurrent_connections),
226212
factories,
227213
state: Default::default(),
228214
shutdown_timeout: config.shutdown_timeout,
229-
conns: conns.clone(),
230-
});
215+
};
231216

232217
let fut = wrk
233218
.factories
@@ -383,7 +368,7 @@ impl Future for ServerWorker {
383368
Pin::new(&mut this.rx2).poll_recv(cx)
384369
{
385370
this.availability.set(false);
386-
let num = num_connections();
371+
let num = this.conns.total();
387372
if num == 0 {
388373
info!("Shutting down worker, 0 connections");
389374
let _ = tx.send(true);
@@ -450,7 +435,7 @@ impl Future for ServerWorker {
450435
// Wait for 1 second.
451436
ready!(shutdown.timer.as_mut().poll(cx));
452437

453-
if num_connections() == 0 {
438+
if this.conns.total() == 0 {
454439
// Graceful shutdown.
455440
if let WorkerState::Shutdown(shutdown) = mem::take(&mut this.state) {
456441
let _ = shutdown.tx.send(true);

0 commit comments

Comments
 (0)