Skip to main content

ClientBlocking

Struct ClientBlocking 

Source
pub struct ClientBlocking(/* private fields */);
Expand description

A blocking (synchronous) version of Client.

Implementations§

Source§

impl ClientBlocking

Source

pub fn cache(&self) -> Option<&dyn Cache>

Returns a reference to the internal cache.

Source

pub fn dht(&self) -> Option<Dht>

Returns a reference to the internal mainline::Dht node.

Gives you access to methods like mainline::Dht::info, mainline::Dht::bootstrapped, and mainline::Dht::to_bootstrap among the rest of the API.

Source

pub fn publish( &self, signed_packet: &SignedPacket, cas: Option<Timestamp>, ) -> Result<(), PublishError>

Publishes a SignedPacket to the mainline Dht and or Relays.

§Lost Update Problem

Mainline DHT and remote relays form a distributed network, and like all distributed networks, it is vulnerable to Write–write conflict.

§Read first

To mitigate the risk of lost updates, you should call the Self::resolve_most_recent method then start authoring the new SignedPacket based on the most recent as in the following example:

 use pkarr::{Client, SignedPacket, Keypair};
 // For local testing
 use pkarr::mainline::Testnet;

 fn run() -> anyhow::Result<()> {
     let testnet = Testnet::new(3)?;
     let client = Client::builder()
         // Disable the default network settings (builtin relays and mainline bootstrap nodes).
         .no_default_network()
         .bootstrap(&testnet.bootstrap)
         .build()?
         .as_blocking();

     let keypair = Keypair::random();

     let (signed_packet, cas) = if let Some(most_recent) = client
         .resolve_most_recent(&keypair.public_key())
     {

         let mut builder = SignedPacket::builder();

         // 1. Optionally inherit all or some of the existing records.
         for record in most_recent.all_resource_records() {
             let name = record.name.to_string();

             if name != "foo" && name != "sercert" {
                 builder = builder.record(record.clone());
             }
         };

         // 2. Optionally add more new records.
         let signed_packet = builder
             .txt("foo".try_into()?, "bar".try_into()?, 30)
             .a("secret".try_into()?, 42.into(), 30)
             .sign(&keypair)?;

         (
             signed_packet,
             // 3. Use the most recent [SignedPacket::timestamp] as a `CAS`.
             Some(most_recent.timestamp())
         )
     } else {
         (
             SignedPacket::builder()
                 .txt("foo".try_into()?, "bar".try_into()?, 30)
                 .a("secret".try_into()?, 42.into(), 30)
                 .sign(&keypair)?,
             None
         )
     };

     client.publish(&signed_packet, cas)?;

     Ok(())
 }
§Errors

This method may return on of these errors:

  1. super::QueryError: when the query fails, and you need to retry or debug the network.
  2. super::ConcurrencyError: when an write conflict (or the risk of it) is detedcted.

If you get a super::ConcurrencyError; you should resolver the most recent packet again, and repeat the steps in the previous example.

Source

pub fn resolve(&self, public_key: &PublicKey) -> Option<SignedPacket>

Returns a SignedPacket from the cache even if it is expired. If there is no packet in the cache, or if the cached packet is expired, it will make a DHT query in a background query and caches any more recent packets it receives.

If you want to get the most recent version of a SignedPacket, you should use Self::resolve_most_recent.

Source

pub fn resolve_most_recent( &self, public_key: &PublicKey, ) -> Option<SignedPacket>

Returns the most recent SignedPacket found after querying all mainline Dht nodes and or Relays.

Useful if you want to read the most recent packet before publishing a new packet.

This is a best effort, and doesn’t guarantee consistency.

Trait Implementations§

Source§

impl Clone for ClientBlocking

Source§

fn clone(&self) -> ClientBlocking

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClientBlocking

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CompatExt for T

Source§

fn compat(self) -> Compat<T>

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more