pub struct Consul { /* private fields */ }Expand description
A key in Consul’s KV store, as a configuration source.
Not Clone: the session holds the current token, and two clones logging in
separately would double the login traffic. Wrap it in an Arc if two places
need one.
Implementations§
Source§impl Consul
impl Consul
Sourcepub fn new(address: impl Into<String>, key: impl Into<String>) -> Self
pub fn new(address: impl Into<String>, key: impl Into<String>) -> Self
The key key, served by the Consul agent at address.
The format is taken from the key’s extension — myapp/db.json is JSON.
A key without one needs with_format.
Sourcepub fn with_format(self, format: Format) -> Self
pub fn with_format(self, format: Format) -> Self
States the format, for a key whose name does not.
Sourcepub fn with_token(self, token: impl Into<String>) -> Self
pub fn with_token(self, token: impl Into<String>) -> Self
The ACL token to authenticate with.
Shorthand for with_auth(Auth::token(..)). A token that stops working
cannot be replaced, because there are no credentials here to log in
again with; Auth::kubernetes and Auth::jwt can.
Sourcepub fn with_auth(self, auth: Auth) -> Self
pub fn with_auth(self, auth: Auth) -> Self
How to obtain an ACL token.
// In Kubernetes, with no secret to distribute at all.
let consul = Consul::new("http://consul:8500", "myapp/db.json")
.with_auth(Auth::kubernetes("kubernetes"));
// Or whatever the operator put in the environment.
let consul = Consul::new("http://consul:8500", "myapp/db.json")
.with_auth(Auth::from_environment());Logging in is lazy: this reaches nothing, and the first read does it.
Sourcepub fn with_agent(self, agent: Agent) -> Self
pub fn with_agent(self, agent: Agent) -> Self
Uses an HTTP client the program already has.
For a caller with its own proxy settings, a private CA, a client
certificate, or a connection pool it would rather not have a second copy
of. The agent’s own timeout applies instead of
with_timeout — including for the long blocking
query watch issues, so an agent used for watching needs
a timeout above with_wait.
Sourcepub fn with_datacenter(self, datacenter: impl Into<String>) -> Self
pub fn with_datacenter(self, datacenter: impl Into<String>) -> Self
The datacenter to read from, when it is not the agent’s own.
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
How long to wait before giving up. Ten seconds by default.
Sourcepub fn with_wait(self, wait: Duration) -> Self
pub fn with_wait(self, wait: Duration) -> Self
How long a blocking query may hold the connection open, when
watch is used. One minute by default.
This is also how long a stopped watch can take to notice, so it trades one against the other: longer means fewer requests, and a slower exit. Consul’s own ceiling is ten minutes, so anything above it is clamped there — the agent would cap it silently anyway, and this way the client-side timeout stays sized to what the agent will actually do.
Sourcepub fn watch<F>(&self, watching: &Watching, on_change: F) -> Result<(), Error>
pub fn watch<F>(&self, watching: &Watching, on_change: F) -> Result<(), Error>
Calls on_change whenever the key’s value changes.
Uses Consul’s blocking queries: each request carries the index the last
one returned, and the agent holds it open until that index moves or
with_wait expires. So this is change-driven, not a
poll — the callback runs when the value actually moves.
The current value is not delivered at startup, for the same reason a file watcher does not report an edit when it starts. Fetch first if the starting value matters, which it usually does:
DbConfig::apply_remote(consul.fetch()?)?;
consul.watch(&watching, DbConfig::apply_remote)A failed query does not end the watch: the agent restarting, a network
blip, or a key that does not exist yet are all exactly what a watch is
supposed to survive. It pauses briefly and tries again, and gives up only
when watching says to. A document identical to the last one is not
reported — Consul bumps the index on every write, including one that
changed nothing.
§Errors
If on_change returns an error, which ends the watch — so a caller that
wants to survive a bad document should log it and return Ok. Transport
failures do not surface here; they are retried.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Consul
impl !RefUnwindSafe for Consul
impl !UnwindSafe for Consul
impl Send for Consul
impl Sync for Consul
impl Unpin for Consul
impl UnsafeUnpin for Consul
Blanket Implementations§
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> 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>
renamed to 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);