dynamo_runtime/
discovery.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{Result, transports::etcd};
5
6pub use etcd::Lease;
7
8pub struct DiscoveryClient {
9    namespace: String,
10    etcd_client: etcd::Client,
11}
12
13impl DiscoveryClient {
14    /// Create a new [`DiscoveryClient`]
15    ///
16    /// This will establish a connection to the etcd server, create a primary lease,
17    /// and spawn a task to keep the lease alive and tie the lifetime of the [`Runtime`]
18    /// to the lease.
19    ///
20    /// If the lease expires, the [`Runtime`] will be shutdown.
21    /// If the [`Runtime`] is shutdown, the lease will be revoked.
22    pub(crate) fn new(namespace: String, etcd_client: etcd::Client) -> Self {
23        DiscoveryClient {
24            namespace,
25            etcd_client,
26        }
27    }
28
29    /// Get the primary lease ID
30    pub fn primary_lease_id(&self) -> u64 {
31        self.etcd_client.lease_id()
32    }
33
34    /// Create a [`Lease`] with a given time-to-live (TTL).
35    /// This [`Lease`] will be tied to the [`crate::Runtime`], but has its own independent [`crate::CancellationToken`].
36    pub async fn create_lease(&self, ttl: u64) -> Result<Lease> {
37        self.etcd_client.create_lease(ttl).await
38    }
39}