1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 02:21:07 +01:00

Remove MAX_CONN (#316)

This commit is contained in:
fakeshadow 2021-04-04 15:00:12 -07:00 committed by GitHub
parent 3859e91799
commit f1573931dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 30 deletions

View File

@ -19,7 +19,7 @@ use crate::signals::{Signal, Signals};
use crate::socket::{MioListener, StdSocketAddr, StdTcpListener, ToSocketAddrs}; use crate::socket::{MioListener, StdSocketAddr, StdTcpListener, ToSocketAddrs};
use crate::socket::{MioTcpListener, MioTcpSocket}; use crate::socket::{MioTcpListener, MioTcpSocket};
use crate::waker_queue::{WakerInterest, WakerQueue}; use crate::waker_queue::{WakerInterest, WakerQueue};
use crate::worker::{self, ServerWorker, ServerWorkerConfig, WorkerAvailability, WorkerHandle}; use crate::worker::{ServerWorker, ServerWorkerConfig, WorkerAvailability, WorkerHandle};
use crate::{join_all, Token}; use crate::{join_all, Token};
/// Server builder /// Server builder
@ -117,8 +117,8 @@ impl ServerBuilder {
/// reached for each worker. /// reached for each worker.
/// ///
/// By default max connections is set to a 25k per worker. /// By default max connections is set to a 25k per worker.
pub fn maxconn(self, num: usize) -> Self { pub fn maxconn(mut self, num: usize) -> Self {
worker::max_concurrent_connections(num); self.worker_config.max_concurrent_connections(num);
self self
} }

View File

@ -3,7 +3,7 @@ use std::{
mem, mem,
pin::Pin, pin::Pin,
sync::{ sync::{
atomic::{AtomicBool, AtomicUsize, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
}, },
task::{Context, Poll}, task::{Context, Poll},
@ -43,27 +43,6 @@ pub(crate) struct Conn {
pub token: Token, pub token: Token,
} }
static MAX_CONNS: AtomicUsize = AtomicUsize::new(25600);
/// Sets the maximum per-worker number of concurrent connections.
///
/// All socket listeners will stop accepting connections when this limit is
/// reached for each worker.
///
/// By default max connections is set to a 25k per worker.
pub fn max_concurrent_connections(num: usize) {
MAX_CONNS.store(num, Ordering::Relaxed);
}
thread_local! {
static MAX_CONNS_COUNTER: Counter =
Counter::new(MAX_CONNS.load(Ordering::Relaxed));
}
pub(crate) fn num_connections() -> usize {
MAX_CONNS_COUNTER.with(|conns| conns.total())
}
// a handle to worker that can send message to worker and share the availability of worker to other // a handle to worker that can send message to worker and share the availability of worker to other
// thread. // thread.
#[derive(Clone)] #[derive(Clone)]
@ -173,6 +152,7 @@ enum WorkerServiceStatus {
pub(crate) struct ServerWorkerConfig { pub(crate) struct ServerWorkerConfig {
shutdown_timeout: Duration, shutdown_timeout: Duration,
max_blocking_threads: usize, max_blocking_threads: usize,
max_concurrent_connections: usize,
} }
impl Default for ServerWorkerConfig { impl Default for ServerWorkerConfig {
@ -182,6 +162,7 @@ impl Default for ServerWorkerConfig {
Self { Self {
shutdown_timeout: Duration::from_secs(30), shutdown_timeout: Duration::from_secs(30),
max_blocking_threads, max_blocking_threads,
max_concurrent_connections: 25600,
} }
} }
} }
@ -191,6 +172,10 @@ impl ServerWorkerConfig {
self.max_blocking_threads = num; self.max_blocking_threads = num;
} }
pub(crate) fn max_concurrent_connections(&mut self, num: usize) {
self.max_concurrent_connections = num;
}
pub(crate) fn shutdown_timeout(&mut self, dur: Duration) { pub(crate) fn shutdown_timeout(&mut self, dur: Duration) {
self.shutdown_timeout = dur; self.shutdown_timeout = dur;
} }
@ -218,16 +203,16 @@ impl ServerWorker {
}) })
.spawn(async move { .spawn(async move {
availability.set(false); availability.set(false);
let mut wrk = MAX_CONNS_COUNTER.with(move |conns| ServerWorker { let mut wrk = ServerWorker {
rx, rx,
rx2, rx2,
services: Vec::new(), services: Vec::new(),
availability, availability,
conns: Counter::new(config.max_concurrent_connections),
factories, factories,
state: Default::default(), state: Default::default(),
shutdown_timeout: config.shutdown_timeout, shutdown_timeout: config.shutdown_timeout,
conns: conns.clone(), };
});
let fut = wrk let fut = wrk
.factories .factories
@ -383,7 +368,7 @@ impl Future for ServerWorker {
Pin::new(&mut this.rx2).poll_recv(cx) Pin::new(&mut this.rx2).poll_recv(cx)
{ {
this.availability.set(false); this.availability.set(false);
let num = num_connections(); let num = this.conns.total();
if num == 0 { if num == 0 {
info!("Shutting down worker, 0 connections"); info!("Shutting down worker, 0 connections");
let _ = tx.send(true); let _ = tx.send(true);
@ -450,7 +435,7 @@ impl Future for ServerWorker {
// Wait for 1 second. // Wait for 1 second.
ready!(shutdown.timer.as_mut().poll(cx)); ready!(shutdown.timer.as_mut().poll(cx));
if num_connections() == 0 { if this.conns.total() == 0 {
// Graceful shutdown. // Graceful shutdown.
if let WorkerState::Shutdown(shutdown) = mem::take(&mut this.state) { if let WorkerState::Shutdown(shutdown) = mem::take(&mut this.state) {
let _ = shutdown.tx.send(true); let _ = shutdown.tx.send(true);