pub struct Context { /* private fields */ }
Expand description
A map of arbitrary JSON values, for use by plugins.
Context makes use of DashMap
under the hood which tries to handle concurrency
by allowing concurrency across threads without requiring locking. This is great
for usability but could lead to surprises when updates are highly contested.
Within the router, contention is likely to be highest within plugins which
provide crate::services::SubgraphRequest
or
crate::services::SubgraphResponse
processing. At such times,
plugins should restrict themselves to the Context::get
and Context::upsert
functions to minimise the possibility of mis-sequenced updates.
Implementations§
Source§impl Context
impl Context
Sourcepub fn extensions(&self) -> &ExtensionsMutex
pub fn extensions(&self) -> &ExtensionsMutex
Returns extensions of the context.
You can use Extensions
to pass data between plugins that is not serializable. Such data is not accessible from Rhai or co-processoers.
It is CRITICAL to avoid holding on to the mutex guard for too long, particularly across async calls. Doing so may cause performance degradation or even deadlocks.
See related clippy lint for examples: https://rust-lang.github.io/rust-clippy/master/index.html#/await_holding_lock
Sourcepub fn contains_key<K>(&self, key: K) -> bool
pub fn contains_key<K>(&self, key: K) -> bool
Returns true if the context contains a value for the specified key.
Sourcepub fn get<K, V>(&self, key: K) -> Result<Option<V>, BoxError>
pub fn get<K, V>(&self, key: K) -> Result<Option<V>, BoxError>
Get a value from the context using the provided key.
Semantics:
- If the operation fails, that’s because we can’t deserialize the value.
- If the operation succeeds, the value is an
Option
.
Sourcepub fn insert<K, V>(&self, key: K, value: V) -> Result<Option<V>, BoxError>
pub fn insert<K, V>(&self, key: K, value: V) -> Result<Option<V>, BoxError>
Insert a value int the context using the provided key and value.
Semantics:
- If the operation fails, then the pair has not been inserted.
- If the operation succeeds, the result is the old value as an
Option
.
Sourcepub fn insert_json_value<K>(&self, key: K, value: Value) -> Option<Value>
pub fn insert_json_value<K>(&self, key: K, value: Value) -> Option<Value>
Insert a value in the context using the provided key and value.
Semantics: the result is the old value as an Option
.
Sourcepub fn get_json_value<K>(&self, key: K) -> Option<Value>
pub fn get_json_value<K>(&self, key: K) -> Option<Value>
Get a json value from the context using the provided key.
Sourcepub fn upsert<K, V>(
&self,
key: K,
upsert: impl FnOnce(V) -> V,
) -> Result<(), BoxError>
pub fn upsert<K, V>( &self, key: K, upsert: impl FnOnce(V) -> V, ) -> Result<(), BoxError>
Upsert a value in the context using the provided key and resolving function.
The resolving function must yield a value to be used in the context. It is provided with the current value to use in evaluating which value to yield.
Semantics:
- If the operation fails, then the pair has not been inserted (or a current value updated).
- If the operation succeeds, the pair have either updated an existing value or been inserted.
Sourcepub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Value>> + '_
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Value>> + '_
Iterate over the entries.
Sourcepub fn iter_mut(
&self,
) -> impl Iterator<Item = RefMutMulti<'_, String, Value>> + '_
pub fn iter_mut( &self, ) -> impl Iterator<Item = RefMutMulti<'_, String, Value>> + '_
Iterate mutably over the entries.
Sourcepub fn enter_active_request(&self) -> BusyTimerGuard
pub fn enter_active_request(&self) -> BusyTimerGuard
Notify the busy timer that we’re waiting on a network request
When a plugin makes a network call that would block request handling, this indicates to the processing time counter that it should stop measuring while we wait for the call to finish. When the value returned by this method is dropped, the router will start measuring again, unless we are still covered by another active request (ex: parallel subgraph calls)
Sourcepub fn busy_time(&self) -> Duration
pub fn busy_time(&self) -> Duration
Time actually spent working on this request
This is the request duration without the time spent waiting for external calls
(coprocessor and subgraph requests). This metric is an approximation of
the time spent, because in the case of parallel subgraph calls, some
router processing time could happen during a network call (and so would
not be accounted for) and make another task late.
This is reported under the apollo_router_processing_time
metric
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
Source§impl<T> Variant for T
impl<T> Variant for T
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Variant
trait object to &mut dyn Any
.Source§fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
Variant
trait object to Box<dyn Any>
.