mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-30 19:54:36 +01:00
reduce branch in Accept::accept method (#300)
This commit is contained in:
parent
0ee8d032b6
commit
26a5af70cb
@ -208,17 +208,7 @@ impl Accept {
|
|||||||
}
|
}
|
||||||
Some(WakerInterest::Pause) => {
|
Some(WakerInterest::Pause) => {
|
||||||
drop(guard);
|
drop(guard);
|
||||||
sockets.iter_mut().for_each(|(_, info)| {
|
self.deregister_all(&mut sockets);
|
||||||
match self.deregister(info) {
|
|
||||||
Ok(_) => info!(
|
|
||||||
"Paused accepting connections on {}",
|
|
||||||
info.addr
|
|
||||||
),
|
|
||||||
Err(e) => {
|
|
||||||
error!("Can not deregister server socket {}", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Some(WakerInterest::Resume) => {
|
Some(WakerInterest::Resume) => {
|
||||||
drop(guard);
|
drop(guard);
|
||||||
@ -295,10 +285,18 @@ impl Accept {
|
|||||||
self.poll.registry().deregister(&mut info.lst)
|
self.poll.registry().deregister(&mut info.lst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deregister_logged(&self, info: &mut ServerSocketInfo) {
|
||||||
|
match self.deregister(info) {
|
||||||
|
Ok(_) => info!("Paused accepting connections on {}", info.addr),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Can not deregister server socket {}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn deregister_all(&self, sockets: &mut Slab<ServerSocketInfo>) {
|
fn deregister_all(&self, sockets: &mut Slab<ServerSocketInfo>) {
|
||||||
sockets.iter_mut().for_each(|(_, info)| {
|
sockets.iter_mut().for_each(|(_, info)| {
|
||||||
info!("Accepting connections on {} has been paused", info.addr);
|
self.deregister_logged(info);
|
||||||
let _ = self.deregister(info);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,43 +386,42 @@ impl Accept {
|
|||||||
|
|
||||||
fn accept(&mut self, sockets: &mut Slab<ServerSocketInfo>, token: usize) {
|
fn accept(&mut self, sockets: &mut Slab<ServerSocketInfo>, token: usize) {
|
||||||
loop {
|
loop {
|
||||||
let msg = if let Some(info) = sockets.get_mut(token) {
|
let info = sockets
|
||||||
match info.lst.accept() {
|
.get_mut(token)
|
||||||
Ok(Some((io, addr))) => Conn {
|
.expect("ServerSocketInfo is removed from Slab");
|
||||||
|
|
||||||
|
match info.lst.accept() {
|
||||||
|
Ok((io, addr)) => {
|
||||||
|
let msg = Conn {
|
||||||
io,
|
io,
|
||||||
token: info.token,
|
token: info.token,
|
||||||
peer: Some(addr),
|
peer: Some(addr),
|
||||||
},
|
};
|
||||||
Ok(None) => return,
|
self.accept_one(sockets, msg);
|
||||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => return,
|
|
||||||
Err(ref e) if connection_error(e) => continue,
|
|
||||||
Err(e) => {
|
|
||||||
// deregister listener temporary
|
|
||||||
error!("Error accepting connection: {}", e);
|
|
||||||
if let Err(err) = self.deregister(info) {
|
|
||||||
error!("Can not deregister server socket {}", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sleep after error. write the timeout to socket info as later
|
|
||||||
// the poll would need it mark which socket and when it's
|
|
||||||
// listener should be registered
|
|
||||||
info.timeout = Some(Instant::now() + Duration::from_millis(500));
|
|
||||||
|
|
||||||
// after the sleep a Timer interest is sent to Accept Poll
|
|
||||||
let waker = self.waker.clone();
|
|
||||||
System::current().arbiter().spawn(async move {
|
|
||||||
sleep(Duration::from_millis(510)).await;
|
|
||||||
waker.wake(WakerInterest::Timer);
|
|
||||||
});
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => return,
|
||||||
return;
|
Err(ref e) if connection_error(e) => continue,
|
||||||
};
|
Err(e) => {
|
||||||
|
error!("Error accepting connection: {}", e);
|
||||||
|
|
||||||
self.accept_one(sockets, msg);
|
// deregister listener temporary
|
||||||
|
self.deregister_logged(info);
|
||||||
|
|
||||||
|
// sleep after error. write the timeout to socket info as later
|
||||||
|
// the poll would need it mark which socket and when it's
|
||||||
|
// listener should be registered
|
||||||
|
info.timeout = Some(Instant::now() + Duration::from_millis(500));
|
||||||
|
|
||||||
|
// after the sleep a Timer interest is sent to Accept Poll
|
||||||
|
let waker = self.waker.clone();
|
||||||
|
System::current().arbiter().spawn(async move {
|
||||||
|
sleep(Duration::from_millis(510)).await;
|
||||||
|
waker.wake(WakerInterest::Timer);
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,15 +40,15 @@ impl MioListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn accept(&self) -> io::Result<Option<(MioStream, SocketAddr)>> {
|
pub(crate) fn accept(&self) -> io::Result<(MioStream, SocketAddr)> {
|
||||||
match *self {
|
match *self {
|
||||||
MioListener::Tcp(ref lst) => lst
|
MioListener::Tcp(ref lst) => lst
|
||||||
.accept()
|
.accept()
|
||||||
.map(|(stream, addr)| Some((MioStream::Tcp(stream), SocketAddr::Tcp(addr)))),
|
.map(|(stream, addr)| (MioStream::Tcp(stream), SocketAddr::Tcp(addr))),
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
MioListener::Uds(ref lst) => lst
|
MioListener::Uds(ref lst) => lst
|
||||||
.accept()
|
.accept()
|
||||||
.map(|(stream, addr)| Some((MioStream::Uds(stream), SocketAddr::Uds(addr)))),
|
.map(|(stream, addr)| (MioStream::Uds(stream), SocketAddr::Uds(addr))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user