Struct apollo_router::Context
source ยท 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ยงunsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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>
Returns self
with the
fg()
set to
Color::BrightBlack
.
ยงExample
println!("{}", value.bright_black());
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>
Returns self
with the
fg()
set to
Color::BrightGreen
.
ยงExample
println!("{}", value.bright_green());
sourceยงfn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
ยงExample
println!("{}", value.bright_yellow());
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>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
ยงExample
println!("{}", value.bright_magenta());
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>
Returns self
with the
fg()
set to
Color::BrightWhite
.
ยงExample
println!("{}", value.bright_white());
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>
Returns self
with the
bg()
set to
Color::BrightBlack
.
ยงExample
println!("{}", value.on_bright_black());
sourceยงfn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightRed
.
ยงExample
println!("{}", value.on_bright_red());
sourceยงfn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
ยงExample
println!("{}", value.on_bright_green());
sourceยงfn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
ยงExample
println!("{}", value.on_bright_yellow());
sourceยงfn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
ยงExample
println!("{}", value.on_bright_blue());
sourceยงfn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
ยงExample
println!("{}", value.on_bright_magenta());
sourceยงfn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
ยงExample
println!("{}", value.on_bright_cyan());
sourceยงfn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
ยงExample
println!("{}", value.on_bright_white());
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 underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
ยงExample
println!("{}", value.underline());
sourceยงfn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
ยงExample
println!("{}", value.rapid_blink());
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> Serializable for Twhere
T: Serialize,
impl<T> Serializable for Twhere
T: Serialize,
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>
.