pub struct Client { /* private fields */ }Expand description
A client for interacting with a skiff cluster.
Client maintains a single gRPC connection to one cluster node. All
write requests are automatically forwarded to the current leader by the
node, so the client does not need to track leadership itself.
§Example
use skiff_rs::Client;
let mut client = Client::new(vec!["127.0.0.1".parse()?]);
client.connect().await?;
client.insert("key", "value").await?;
let val: Option<String> = client.get("key").await?;Implementations§
Source§impl Client
impl Client
Sourcepub fn new(cluster: Vec<Ipv4Addr>) -> Self
pub fn new(cluster: Vec<Ipv4Addr>) -> Self
Create a new client that will try each address in cluster in order
when connecting.
Sourcepub fn with_port(self, port: u16) -> Self
pub fn with_port(self, port: u16) -> Self
Override the port used to connect to cluster nodes. Defaults to 9400.
Sourcepub async fn connect(&mut self) -> Result<(), Error>
pub async fn connect(&mut self) -> Result<(), Error>
Establish a gRPC connection to the first reachable node in the cluster.
This is called automatically by the other methods, but can be called explicitly to verify connectivity before performing operations.
§Errors
Returns Error::ClientConnectFailed if no node in the cluster list
can be reached.
Sourcepub async fn get<T: DeserializeOwned>(
&mut self,
key: &str,
) -> Result<Option<T>, Error>
pub async fn get<T: DeserializeOwned>( &mut self, key: &str, ) -> Result<Option<T>, Error>
Retrieve the value stored at key, deserializing it as T.
Returns Ok(None) if the key does not exist.
§Errors
Returns an error if the RPC fails or the stored bytes cannot be
deserialized into T.
Sourcepub async fn insert<T: Serialize>(
&mut self,
key: &str,
value: T,
) -> Result<(), Error>
pub async fn insert<T: Serialize>( &mut self, key: &str, value: T, ) -> Result<(), Error>
Insert or overwrite key with value.
The call blocks until the entry has been committed to a majority of cluster nodes.
§Errors
Returns an error if serialization fails, the RPC fails, or the cluster does not commit the entry within the timeout.
Sourcepub async fn remove(&mut self, key: &str) -> Result<(), Error>
pub async fn remove(&mut self, key: &str) -> Result<(), Error>
Remove key from the store.
The call blocks until the deletion has been committed to a majority of
cluster nodes. Returns Ok(()) even if the key did not exist.
§Errors
Returns an error if the RPC fails or the cluster does not commit the deletion within the timeout.
Sourcepub async fn get_prefixes(&mut self) -> Result<Vec<String>, Error>
pub async fn get_prefixes(&mut self) -> Result<Vec<String>, Error>
Return all key prefixes (namespace segments) currently in the store.
Keys are organised hierarchically using / as a separator. This
method returns the distinct prefix segments, e.g. ["users", "posts"]
for a store containing "users/alice" and "posts/hello".
§Errors
Returns an error if the RPC fails.
Sourcepub async fn list_keys(&mut self, prefix: &str) -> Result<Vec<String>, Error>
pub async fn list_keys(&mut self, prefix: &str) -> Result<Vec<String>, Error>
Return all keys whose path starts with prefix.
Pass an empty string or "/" to list all keys at the root level.
§Errors
Returns an error if the RPC fails.
Sourcepub async fn remove_node(
&mut self,
id: Uuid,
address: Ipv4Addr,
) -> Result<(), Error>
pub async fn remove_node( &mut self, id: Uuid, address: Ipv4Addr, ) -> Result<(), Error>
Remove a node from the cluster configuration.
id and address must match an existing member. The removal is
propagated through the Raft log so all surviving nodes eventually drop
the node from their cluster view.
§Errors
Returns an error if the RPC fails or the node is not found.
Sourcepub async fn watch(&mut self, prefix: &str) -> Result<Subscriber, Error>
pub async fn watch(&mut self, prefix: &str) -> Result<Subscriber, Error>
Subscribe to changes under prefix.
Returns a Subscriber that yields a (key, value) pair each time
an entry whose path starts with prefix is inserted. The stream
remains open until the connection is dropped or the server closes it.
§Errors
Returns an error if the RPC fails to establish the stream.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
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> 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> 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