Skip to main content

ChanMgr

Struct ChanMgr 

Source
pub struct ChanMgr<R: Runtime> {
    pub(crate) mgr: AbstractChanMgr<CompoundFactory<ChanBuilder<R, DefaultTransport<R>>>>,
    pub(crate) bootstrap_status: ConnStatusEvents,
    pub(crate) runtime: R,
}
Expand description

An object that remembers a set of live channels, and launches new ones on request.

Use the ChanMgr::get_or_launch function to create a new Channel, or get one if it exists. (For a slightly lower-level API that does no caching, see ChannelFactory and its implementors.

Each channel is kept open as long as there is a reference to it, or something else (such as the relay or a network error) kills the channel.

After a ChanMgr launches a channel, it keeps a reference to it until that channel has been unused (that is, had no circuits attached to it) for a certain amount of time. (Currently this interval is chosen randomly from between 180-270 seconds, but this is an implementation detail that may change in the future.)

Fields§

§mgr: AbstractChanMgr<CompoundFactory<ChanBuilder<R, DefaultTransport<R>>>>

Internal channel manager object that does the actual work.

§How this is built

This internal manager is parameterized over an mgr::AbstractChannelFactory, which here is instantiated with a factory::CompoundFactory. The CompoundFactory itself holds:

§bootstrap_status: ConnStatusEvents

Stream of ConnStatus events.

§runtime: R

The runtime. Needed to possibly spawn tasks.

Implementations§

Source§

impl<R: Runtime> ChanMgr<R>

Source

pub fn new( runtime: R, config: ChanMgrConfig, dormancy: Dormancy, netparams: &NetParameters, memquota: ToplevelAccount, ) -> Result<Self>
where R: 'static,

Construct a new channel manager.

A new ChannelAccount will be made from memquota, for each Channel.

The ChannelAccount is used for data associated with this channel.

This does not (currently) include downstream outbound data (ie, data processed by the channel implementation here, awaiting TLS processing and actual transmission). In any case we try to keep those buffers small.

The ChannelAccount does track upstream outbound data (ie, data processed by a circuit, but not yet by the channel), even though that data relates to a specific circuit. TODO #1652 use CircuitAccount for circuit->channel queue.

§Usage note

For the manager to work properly, you will need to call ChanMgr::launch_background_tasks.

The keymgr is only needed for a relay which is used for authenticating its channel to other relays. Pass None for a client.

Source

pub fn launch_background_tasks( self: &Arc<Self>, runtime: &R, netdir: Arc<dyn NetDirProvider>, ) -> Result<Vec<TaskHandle>>

Launch the periodic daemon tasks required by the manager to function properly.

Returns a TaskHandle that can be used to manage those daemon tasks that poll periodically.

Source

pub async fn handle_incoming( &self, src: Sensitive<SocketAddr>, stream: <R as NetStreamProvider>::Stream, ) -> Result<Arc<Channel>>

Build a channel for an incoming stream.

The my_addrs are the IP address(es) that are advertised by the relay in the consensus. We need to pass them so they can be sent in the NETINFO cell.

The channel may or may not be authenticated. This method will wait until the channel is usable, and may return an error if we already have an existing channel to this peer, or if there are already too many open connections with this peer or subnet (as a dos defence).

Source

pub async fn get_or_launch<T: ChanTarget + ?Sized>( &self, target: &T, usage: ChannelUsage, ) -> Result<(Arc<Channel>, ChanProvenance)>

Try to get a suitable channel to the provided target, launching one if one does not exist.

This function does not guarantee that the returned channel satisfies all of the properties of target. For example if an existing channel is returned, it might not be connected to any of the addresses specified in target.

If there is already a channel launch attempt in progress, this function will wait until that launch is complete, and succeed or fail depending on its outcome.

Source

pub fn bootstrap_events(&self) -> ConnStatusEvents

Return a stream of ConnStatus events to tell us about changes in our ability to connect to the internet.

Note that this stream can be lossy: the caller will not necessarily observe every event on the stream

Source

pub fn expire_channels(&self) -> Duration

Expire all channels that have been unused for too long.

Return the duration from now until next channel expires.

Source

pub fn set_dormancy( &self, dormancy: Dormancy, netparams: Arc<dyn AsRef<NetParameters>>, ) -> StdResult<(), Bug>

Notifies the chanmgr to be dormant like dormancy

Source

pub fn reconfigure( &self, config: &ChannelConfig, how: Reconfigure, netparams: Arc<dyn AsRef<NetParameters>>, ) -> StdResult<(), ReconfigureError>

Reconfigure all channels

Source

pub fn set_pt_mgr(&self, ptmgr: Arc<dyn AbstractPtMgr + 'static>)

Replace the transport registry with one that may know about more transports.

Note that the ChannelFactory instances returned by ptmgr are required to time-out channels that take too long to build. You’ll get this behavior by default if the factories implement ChannelFactory using transport::proxied::ExternalProxyPlugin, which tor-ptmgr does.

Source

pub fn set_relay_auth_material( &self, auth_material: Arc<RelayChannelAuthMaterial>, ) -> Result<()>

Replace the relay auth material used for building new channels.

This rebuilds the internal channel builder with the provided auth_material, which includes a new TLS cert and key. Existing channels are not affected, only newly created channels will use the new keys.

Source

pub fn set_create_request_handler( &self, handler: Arc<CreateRequestHandler>, ) -> Result<()>

This will be used to handle CREATE* requests on channels.

This handler will only be used for new channels, not existing channels.

This will not be updated in any way by the channel manager, for example by a netdir update or when any keys change. The caller must handle this. The idea is that the channel manager shouldn’t need to deal with circuit-specific stuff.

It’s expected to only ever call this once. Ideally it would be an Option in the constructor, but we don’t want conditionally-compiled constructor arguments, and the CreateRequestHandler requires a [dyn ChannelProvider] which is typically this ChanMgr itself.

Source

pub async fn build_unmanaged_channel( &self, target: impl IntoOwnedChanTarget, memquota: ChannelAccount, ) -> Result<Arc<Channel>>

Try to create a new, unmanaged channel to target.

Unlike get_or_launch, this function always creates a new channel, never retries transient failure, and does not register this channel with the ChanMgr.

Generally you should not use this function; get_or_launch is usually a better choice. This function is the right choice if, for whatever reason, you need to manage the lifetime of the channel you create, and make sure that no other code with access to this ChanMgr will be able to use the channel.

Source

pub(crate) async fn continually_update_channels_config( self_: Weak<Self>, netdir: Arc<dyn NetDirProvider>, )

Watch for things that ought to change the configuration of all channels in the client

Currently this handles enabling and disabling channel padding.

This is a daemon task that runs indefinitely in the background, and exits when we find that chanmgr is dropped.

Source

pub(crate) async fn continually_expire_channels( sched: TaskSchedule<R>, chanmgr: Weak<Self>, )

Periodically expire any channels that have been unused beyond the maximum duration allowed.

Exist when we find that chanmgr is dropped

This is a daemon task that runs indefinitely in the background

Trait Implementations§

Source§

impl<R: Runtime> ChannelProvider for ChanMgr<R>

Source§

type BuildSpec = OwnedChanTarget

Type that explains how to build an outgoing channel.
Source§

fn get_or_launch( self: Arc<Self>, reactor_id: UniqId, target: Self::BuildSpec, tx: OutboundChanSender, ) -> Result<()>

Get a channel corresponding to the identities of target, for the circuit reactor with the specified reactor_id which should only be used for logging purposes. Read more

Auto Trait Implementations§

§

impl<R> !Freeze for ChanMgr<R>

§

impl<R> !RefUnwindSafe for ChanMgr<R>

§

impl<R> Send for ChanMgr<R>

§

impl<R> Sync for ChanMgr<R>

§

impl<R> Unpin for ChanMgr<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for ChanMgr<R>
where R: UnsafeUnpin,

§

impl<R> !UnwindSafe for ChanMgr<R>

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> PossiblyOption<T> for T

Source§

fn to_option(self) -> Option<T>

Convert this object into an Option<T>
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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