Start move to Tokio

This commit is contained in:
Aram 🍐 2021-10-13 19:39:17 -04:00
parent f2e6ec1e3f
commit 7693666855
7 changed files with 840 additions and 508 deletions

31
src/port_pool.rs Normal file
View file

@ -0,0 +1,31 @@
use std::ops::Range;
use anyhow::Context;
const MIN_PORT: u16 = 32768;
const MAX_PORT: u16 = 60999;
const PORT_RANGE: Range<u16> = MIN_PORT..MAX_PORT;
pub struct PortPool {
inner: lockfree::queue::Queue<u16>,
}
impl PortPool {
pub fn new() -> Self {
let inner = lockfree::queue::Queue::default();
PORT_RANGE.for_each(|p| inner.push(p) as ());
Self {
inner,
}
}
pub fn next(&self) -> anyhow::Result<u16> {
self.inner
.pop()
.with_context(|| "Virtual port pool is exhausted")
}
pub fn release(&self, port: u16) {
self.inner.push(port);
}
}