1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

Merge pull request #118 from messense/feature/sockets-vec

Use Vec instead of HashMap to store sockets in HttpServer
This commit is contained in:
Nikolay Kim 2018-03-11 16:38:23 -07:00 committed by GitHub
commit 208117ca6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,6 @@ use std::{io, net, thread};
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, mpsc as sync_mpsc}; use std::sync::{Arc, mpsc as sync_mpsc};
use std::time::Duration; use std::time::Duration;
use std::collections::HashMap;
use actix::prelude::*; use actix::prelude::*;
use actix::actors::signal; use actix::actors::signal;
@ -37,7 +36,7 @@ pub struct HttpServer<H> where H: IntoHttpHandler + 'static
factory: Arc<Fn() -> Vec<H> + Send + Sync>, factory: Arc<Fn() -> Vec<H> + Send + Sync>,
#[cfg_attr(feature="cargo-clippy", allow(type_complexity))] #[cfg_attr(feature="cargo-clippy", allow(type_complexity))]
workers: Vec<(usize, Addr<Syn, Worker<H::Handler>>)>, workers: Vec<(usize, Addr<Syn, Worker<H::Handler>>)>,
sockets: HashMap<net::SocketAddr, net::TcpListener>, sockets: Vec<(net::SocketAddr, net::TcpListener)>,
accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>, accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>,
exit: bool, exit: bool,
shutdown_timeout: u16, shutdown_timeout: u16,
@ -85,7 +84,7 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
keep_alive: KeepAlive::Os, keep_alive: KeepAlive::Os,
factory: Arc::new(f), factory: Arc::new(f),
workers: Vec::new(), workers: Vec::new(),
sockets: HashMap::new(), sockets: Vec::new(),
accept: Vec::new(), accept: Vec::new(),
exit: false, exit: false,
shutdown_timeout: 30, shutdown_timeout: 30,
@ -173,7 +172,7 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
/// Get addresses of bound sockets. /// Get addresses of bound sockets.
pub fn addrs(&self) -> Vec<net::SocketAddr> { pub fn addrs(&self) -> Vec<net::SocketAddr> {
self.sockets.keys().cloned().collect() self.sockets.iter().map(|s| s.0.clone()).collect()
} }
/// Use listener for accepting incoming connection requests /// Use listener for accepting incoming connection requests
@ -181,7 +180,7 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
/// HttpServer does not change any configuration for TcpListener, /// HttpServer does not change any configuration for TcpListener,
/// it needs to be configured before passing it to listen() method. /// it needs to be configured before passing it to listen() method.
pub fn listen(mut self, lst: net::TcpListener) -> Self { pub fn listen(mut self, lst: net::TcpListener) -> Self {
self.sockets.insert(lst.local_addr().unwrap(), lst); self.sockets.push((lst.local_addr().unwrap(), lst));
self self
} }
@ -195,7 +194,7 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
match create_tcp_listener(addr, self.backlog) { match create_tcp_listener(addr, self.backlog) {
Ok(lst) => { Ok(lst) => {
succ = true; succ = true;
self.sockets.insert(lst.local_addr().unwrap(), lst); self.sockets.push((lst.local_addr().unwrap(), lst));
}, },
Err(e) => err = Some(e), Err(e) => err = Some(e),
} }
@ -288,7 +287,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
} else { } else {
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
let addrs: Vec<(net::SocketAddr, net::TcpListener)> = let addrs: Vec<(net::SocketAddr, net::TcpListener)> =
self.sockets.drain().collect(); self.sockets.drain(..).collect();
let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal); let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
let info = Info{addr: addrs[0].0, handler: StreamHandlerType::Normal}; let info = Info{addr: addrs[0].0, handler: StreamHandlerType::Normal};
@ -357,7 +356,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
Err(io::Error::new(io::ErrorKind::Other, "No socket addresses are bound")) Err(io::Error::new(io::ErrorKind::Other, "No socket addresses are bound"))
} else { } else {
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
let addrs: Vec<(net::SocketAddr, net::TcpListener)> = self.sockets.drain().collect(); let addrs: Vec<(net::SocketAddr, net::TcpListener)> = self.sockets.drain(..).collect();
let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false);
let workers = self.start_workers( let workers = self.start_workers(
&settings, &StreamHandlerType::Tls(acceptor.clone())); &settings, &StreamHandlerType::Tls(acceptor.clone()));
@ -409,7 +408,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
let acceptor = builder.build(); let acceptor = builder.build();
let addrs: Vec<(net::SocketAddr, net::TcpListener)> = self.sockets.drain().collect(); let addrs: Vec<(net::SocketAddr, net::TcpListener)> = self.sockets.drain(..).collect();
let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false);
let workers = self.start_workers( let workers = self.start_workers(
&settings, &StreamHandlerType::Alpn(acceptor.clone())); &settings, &StreamHandlerType::Alpn(acceptor.clone()));
@ -451,7 +450,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
if !self.sockets.is_empty() { if !self.sockets.is_empty() {
let addrs: Vec<(net::SocketAddr, net::TcpListener)> = let addrs: Vec<(net::SocketAddr, net::TcpListener)> =
self.sockets.drain().collect(); self.sockets.drain(..).collect();
let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal); let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
let info = Info{addr: addrs[0].0, handler: StreamHandlerType::Normal}; let info = Info{addr: addrs[0].0, handler: StreamHandlerType::Normal};