use std::sync::Arc;
use std::time::Duration;
use crate::pool::ExitPool;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Availability {
Available,
Resting(Option<Duration>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitStatus {
Ok,
Cooled,
Dead,
}
pub struct Lease {
url: Option<String>,
code: String,
pool: Arc<ExitPool>,
returned: bool,
}
impl Lease {
pub(crate) fn new(url: Option<String>, code: String, pool: Arc<ExitPool>) -> Lease {
Lease {
url,
code,
pool,
returned: false,
}
}
pub fn key(&self) -> String {
self.url.clone().unwrap_or_default()
}
pub fn code(&self) -> &str {
&self.code
}
pub fn release(mut self, status: ExitStatus) {
self.pool.return_lease(&self.code, status);
self.returned = true;
}
}
impl Drop for Lease {
fn drop(&mut self) {
if !self.returned {
self.pool.return_lease(&self.code, ExitStatus::Ok);
}
}
}