teloxide-fork-core 0.16.0

Core part of the `teloxide` library - telegram bot API client
Documentation
use tokio::sync::oneshot;

use crate::RequestError;

use super::FreezeUntil;

pub(super) fn channel() -> (RequestLock, RequestWaiter) {
    let (tx, rx) = oneshot::channel();
    (RequestLock(tx), rx)
}

pub(super) struct RequestLock(oneshot::Sender<Result<Permit, RequestError>>);

type RequestWaiter = oneshot::Receiver<Result<Permit, RequestError>>;

pub(super) struct Permit {
    pub(super) retry: bool,
    pub(super) finished: oneshot::Sender<(FreezeUntil, Option<RequestLock>)>,
}

impl RequestLock {
    pub(super) fn unlock(self, permit: Permit) -> bool {
        self.0.send(Ok(permit)).is_ok()
    }

    pub(super) fn reject(self) {
        let _ = self.0.send(Err(RequestError::ThrottleQueueFull));
    }

    pub(super) fn is_cancelled(&self) -> bool {
        self.0.is_closed()
    }
}