Module zksync_concurrency::ctx
source · Expand description
Implementation of the golang Context (https://pkg.go.dev/context). You can think of context as a runtime equivalent of the rust lifetime: As soon as its context is canceled the function should return ASAP, without doing any further blocking calls.
It is NOT possible to extend the context provided by the caller, who defines how long it allows the function call to execute.
Context is essentially a cancellation token which should be passed down the call stack, so that it can be awaited together with every blocking call: Instead of “awaiting for new data on the channel”, you “await for new data on the channel OR for context to get canceled”. This way you can implement graceful shutdown in a very uniform way.
Modules§
- Implementation of a context-aware bounded and unbounded mpsc async channel. Build on top of
tokio::sync::mpsc::(un)bounded_channel
. Note that channel disconnection is not observable by default:
Structs§
- Clock which is running
speedup
times faster than the realtime. (ifspeedup
is <1, then it is actually slower). - Error returned when the blocking operation was interrupted due to context getting canceled.
- Contexts are composed into a tree via
_parent
link. We maintain an invariant_parent.deadline <= deadline
. If a parent gets canceled, the child also gets canceled immediately afterwards, although not atomically. If deadline passes the context also gets canceled. - Context-aware future, that an async task can await and sync task can block on.
- Fake clock which supports manually advancing the time.
- No-copy wrapper allowing to carry a
Copy
type into a closure or anasync
block. - Realtime clock.
Enums§
- An abstract clock. We use a concrete enum rather than a trait to avoid abstract method call in runtime.
- anyhow::Error + “canceled” variant. Useful for working with concurrent code which doesn’t need structured errors, but needs to handle cancelation explicitly.
Functions§
- Equivalent to Ok::<_, ctx::Error>(value).
- Constructs a top-level context. Should be called only at the start of the
main()
function of the binary. - Returns a root context with the given
clock
and a deterministic RNG provider. - Returns a child context with the given clock. Useful for simulating entities with independent clocks.
Type Aliases§
- Wraps result with ErrCancel as an error.
- Alias for Result with
ctx::Error
.