Skip to main content

Queue

Struct Queue 

Source
pub struct Queue<P: PeerId> { /* private fields */ }
Expand description

A policy that hands each decision to the application, through a bounded queue.

The application takes the requests with next and answers each Pending with admit or reject. Clones share one queue, so the node can hold the policy while the application holds a clone to answer from.

A request that finds the queue full is rejected at once with moq_net::Error::TooManyRequests, so a slow application does not pile up sessions whose peers wait. A node also gives up on a decision after its decide timeout, such as NodeConfig::decide_timeout in iroh-live-node, which is 10 seconds there by default. Raise it when a person answers.

§Examples

Admit the peers that present a known token:

use moq_ext_access::{
    Grant,
    policy::{Queue, Rejection},
};

let queue = Queue::<String>::new(16);
// Give `queue.clone()` to the node as its policy, then answer:
while let Some(pending) = queue.next().await {
    if pending.request().token() == Some("secret") {
        pending.admit(Grant::subscribe_only());
    } else {
        pending.reject(Rejection::new("unknown token"));
    }
}

Implementations§

Source§

impl<P: PeerId> Queue<P>

Source

pub fn new(capacity: usize) -> Self

Creates a queue that holds up to capacity requests.

A request that finds the queue full is rejected with moq_net::Error::TooManyRequests. A node treats that as no answer when it asks again about a live peer: the peer keeps its authorization, and the node asks again later.

§Panics

Panics if capacity is zero.

Source

pub async fn next(&self) -> Option<Pending<P>>

Waits for the next request to answer.

Returns None once the queue is closed and empty. Cancellation safe: dropping the future loses no request.

Source

pub fn close(&self)

Closes the queue for every clone.

Requests that arrive later are rejected. The ones already queued stay for next, which returns None after them.

Trait Implementations§

Source§

impl<P: Clone + PeerId> Clone for Queue<P>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: PeerId> Debug for Queue<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: PeerId> Policy<P> for Queue<P>

Source§

async fn decide(&self, request: &PeerRequest<P>) -> Decision

Decides whether the peer may hold a session, and with which rights. Read more
Source§

fn filter<F>(self, admit: F) -> Filter<Self, F>
where Self: Sized, F: Fn(&PeerRequest<P>) -> bool + Send + Sync + 'static,

Returns a policy that rejects the peers admit refuses, and asks this one otherwise. Read more

Auto Trait Implementations§

§

impl<P> !Unpin for Queue<P>

§

impl<P> !UnsafeUnpin for Queue<P>

§

impl<P> Freeze for Queue<P>

§

impl<P> RefUnwindSafe for Queue<P>

§

impl<P> Send for Queue<P>

§

impl<P> Sync for Queue<P>

§

impl<P> UnwindSafe for Queue<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<P, T> DynPolicy<P> for T
where P: PeerId, T: Policy<P>,

Source§

fn decide<'a>( &'a self, request: &'a PeerRequest<P>, ) -> Pin<Box<dyn Future<Output = Result<Authorization, Rejection>> + Send + 'a>>

Decides as Policy::decide does, with a boxed future.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more