struct State {
config: Arc<BridgeDescDownloadConfig>,
subscribers: FlagPublisher<BridgeDescEvent>,
current: Arc<BridgeDescList>,
running: HashMap<BridgeConfig, RunningInfo>,
queued: VecDeque<QueuedEntry>,
dormancy: Dormancy,
refetch_schedule: BinaryHeap<RefetchEntry<SystemTime, ()>>,
retry_schedule: BinaryHeap<RefetchEntry<Instant, RetryDelay>>,
earliest_timeout: Sender<Option<Instant>>,
}
bridge-client
only.Expand description
State: our downloaded descriptors (cache), and records of what we’re doing
Various functions (both tasks and public entrypoints),
which generally start with a Manager
,
lock the mutex and modify this.
Generally, the flow is:
-
A public entrypoint, or task, obtains a
StateGuard
. It modifies the state to represent the callers’ new requirements, or things it has done, by updating the state, preserving the invariants but disturbing the “liveness” (see below). -
StateGuard::drop
callsState::process
. This restores the liveness properties.
§Possible states of a bridge:
A bridge can be in one of the following states,
represented by its presence in these particular data structures inside State
:
running
/queued
: newly added, no outcome yet.current
+running
/queued
: we are fetching (or going to)current = OK
+refetch_schedule
: fetched OK, will refetch before expirycurrent = Err
+retry_schedule
: failed, will retry at some point
§Invariants:
Can be disrupted in the middle of a principal function, but should be restored on return.
-
Tracked: Each bridge appears at most once in
running
,queued
,refetch_schedule
andretry_schedule
. We call such a bridge Tracked. -
Current Every bridge in
current
is Tracked. (But not every Tracked bridge is necessarily incurrent
, yet.) -
Schedules Every bridge in
refetch_schedule
orretry_schedule
is also incurrent
. -
Input: Exactly each bridge that was passed to the last call to
set_bridges()
is Tracked. (If we encountered spawn failures, we treat this as trying to shut down, so we cease attempts to get bridges, and discard the relevant state, violating this.) -
Limit:
running
is capped at the effective parallelism: zero if we are dormant, the configured parallelism otherwise.
§Liveness properties:
These can be disrupted by any function which holds a StateGuard
.
Will be restored by process()
,
which is called when StateGuard
is dropped.
Functions that take a StateGuard
may disturb these invariants
and rely on someone else to restore them.
-
Running: If
queued
is nonempty,running
is full. -
Timeout:
earliest_timeout
is the earliest timeout in eitherretry_schedule
orrefetch_schedule
. (Disturbances of this property which occur due to system time warps are not necessarily detected and remedied in a timely way, but will be remedied no later than aftermax_refetch
.)
Fields§
§config: Arc<BridgeDescDownloadConfig>
Our configuration
subscribers: FlagPublisher<BridgeDescEvent>
People who will be told when current
changes.
current: Arc<BridgeDescList>
Our current idea of our output, which we give out handles onto.
running: HashMap<BridgeConfig, RunningInfo>
Bridges whose descriptors we are currently downloading.
queued: VecDeque<QueuedEntry>
Bridges which we want to download,
but we’re waiting for running
to be less than effective_parallelism()
.
dormancy: Dormancy
Are we dormant?
refetch_schedule: BinaryHeap<RefetchEntry<SystemTime, ()>>
Bridges that we have a descriptor for, and when they should be refetched due to validity expiry.
This is indexed by SystemTime
because that helps avoids undesirable behaviors
when the system clock changes.
retry_schedule: BinaryHeap<RefetchEntry<Instant, RetryDelay>>
Bridges that failed earlier, and when they should be retried.
earliest_timeout: Sender<Option<Instant>>
Earliest time from either retry_schedule
or refetch_schedule
None
means “wait indefinitely”.
Implementations§
Source§impl State
impl State
Sourcefn process<R: Runtime, M: Mockable<R>>(&mut self, mgr: &Arc<Manager<R, M>>)
fn process<R: Runtime, M: Mockable<R>>(&mut self, mgr: &Arc<Manager<R, M>>)
Ensure progress is made, by restoring all the liveness invariants
This includes launching circuits as needed.
Sourcefn consider_launching<R: Runtime, M: Mockable<R>>(
&mut self,
mgr: &Arc<Manager<R, M>>,
)
fn consider_launching<R: Runtime, M: Mockable<R>>( &mut self, mgr: &Arc<Manager<R, M>>, )
Launch download attempts if we can
Specifically: if we have things in queued
, and running
is shorter than
effective_parallelism()
, we launch task(s) to attempt download(s).
Restores liveness invariant Running.
Idempotent. Forms part of process
.
Sourcefn modify_current<T, F: FnOnce(&mut BridgeDescList) -> T>(&mut self, f: F) -> T
fn modify_current<T, F: FnOnce(&mut BridgeDescList) -> T>(&mut self, f: F) -> T
Modify current
and notify subscribers
Helper function which modifies only current
, not any of the rest of the state.
it is the caller’s responsibility to ensure that the invariants are upheld.
The implementation actually involves cloning current
,
so it is best to amortize calls to this function.
Sourcefn set_current_and_notify<BDL: Into<Arc<BridgeDescList>>>(&mut self, new: BDL)
fn set_current_and_notify<BDL: Into<Arc<BridgeDescList>>>(&mut self, new: BDL)
Set current
to a value and notify
Helper function which modifies only current
, not any of the rest of the state.
it is the caller’s responsibility to ensure that the invariants are upheld.
Sourcefn effective_parallelism(&self) -> usize
fn effective_parallelism(&self) -> usize
Obtain the currently-desired level of parallelism
Helper function. The return value depends the mutable state and also the config
.
This is how we implement dormancy.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for State
impl !RefUnwindSafe for State
impl Send for State
impl Sync for State
impl Unpin for State
impl !UnwindSafe for State
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.