Skip to main content

Module blocking

Module blocking 

Source
Expand description

Synchronous, runtime-agnostic facade over the async client (feature blocking).

BlockingClient lets synchronous code (a sync trait impl, a plugin host, a thread pool) call RegistryClient, VerifiedContext, CrossRegistryResolver and verify_change without owning a tokio runtime:

  • Its own runtime. Every call runs on a small multi-threaded tokio runtime that the facade starts on a dedicated thread and shuts down on that same thread when the last clone is dropped. The caller’s thread only waits, so the facade works from plain threads and from inside an existing tokio runtime alike, without the “cannot start a runtime from within a runtime” panic.
  • A hard per-call deadline. Each method takes a std::time::Instant. When it passes, the call returns AcdpError::DeadlineExceeded and the in-flight work is cancelled. No partial result is ever returned: a deadline fails closed. A deadline already in the past fails without issuing any request.
  • Thread-safe. BlockingClient is Clone + Send + Sync; clones share the runtime and the underlying clients, so one instance can serve many threads concurrently.

Give the facade its own RegistryClient, WebResolver and CrossRegistryResolver rather than sharing instances that are also used on another runtime: an HTTP connection is driven by the runtime that opened it, and a pooled connection whose runtime has gone fails on reuse.

Called from inside a tokio runtime, the facade blocks the calling worker thread while it waits (on a multi-threaded runtime it first hands the worker’s other tasks off with tokio::task::block_in_place). Async callers should prefer the async API; the facade is for code that cannot be async.

use std::time::{Duration, Instant};
use acdp_client::blocking::BlockingClient;
use acdp_client::{RegistryClient, VerificationPolicy};
use acdp_did::WebResolver;
use acdp_types::CtxId;

let client = BlockingClient::new(
    RegistryClient::new("https://registry.example.com")?,
    WebResolver::new(),
)?;
let ctx_id = CtxId(
    "acdp://registry.example.com/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa".into(),
);
let deadline = Instant::now() + Duration::from_secs(2);
let verified =
    client.fetch_verified(&ctx_id, &VerificationPolicy::default(), deadline)?;
println!("{}", verified.body().ctx_id);

Structs§

BlockingClient
A synchronous client with a hard per-call deadline. See the module docs.

Constants§

DEFAULT_WORKER_THREADS
Default number of runtime worker threads (see BlockingClient::with_worker_threads).