mirror of
https://github.com/aramperes/onetun.git
synced 2025-09-09 11:18:30 -04:00
Start move to Tokio
This commit is contained in:
parent
f2e6ec1e3f
commit
7693666855
7 changed files with 840 additions and 508 deletions
31
src/port_pool.rs
Normal file
31
src/port_pool.rs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue