danube_client/client.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
use std::sync::Arc;
use tonic::transport::Uri;
use crate::{
connection_manager::{ConnectionManager, ConnectionOptions},
consumer::ConsumerBuilder,
errors::Result,
health_check::HealthCheckService,
lookup_service::{LookupResult, LookupService},
producer::ProducerBuilder,
schema::Schema,
schema_service::SchemaService,
};
/// The main client for interacting with the Danube messaging system.
///
/// The `DanubeClient` struct is designed to facilitate communication with the Danube messaging system.
/// It provides various methods for managing producers and consumers, performing topic lookups, and retrieving schema information. This client acts as the central interface for interacting with the messaging system and managing connections and services.
#[derive(Debug, Clone)]
pub struct DanubeClient {
pub(crate) uri: Uri,
pub(crate) cnx_manager: Arc<ConnectionManager>,
pub(crate) lookup_service: LookupService,
pub(crate) schema_service: SchemaService,
pub(crate) health_check_service: HealthCheckService,
}
impl DanubeClient {
fn new_client(builder: DanubeClientBuilder, uri: Uri) -> Self {
let cnx_manager = ConnectionManager::new(builder.connection_options);
let cnx_manager = Arc::new(cnx_manager);
let lookup_service = LookupService::new(cnx_manager.clone());
let schema_service = SchemaService::new(cnx_manager.clone());
let health_check_service = HealthCheckService::new(cnx_manager.clone());
DanubeClient {
uri: uri,
cnx_manager,
lookup_service,
schema_service,
health_check_service,
}
}
/// Initializes a new `DanubeClientBuilder` instance.
///
/// The builder pattern allows for configuring and constructing a `DanubeClient` instance with optional settings and options.
/// Using the builder, you can customize various aspects of the `DanubeClient`, such as connection settings, timeouts, and other configurations before creating the final `DanubeClient` instance.
pub fn builder() -> DanubeClientBuilder {
DanubeClientBuilder::default()
}
/// Returns a new `ProducerBuilder` for configuring and creating a `Producer` instance.
///
/// This method initializes a `ProducerBuilder`, which is used to set up various options and settings for a `Producer`.
/// The builder pattern allows you to specify details such as the topic, producer name, partitions, schema, and other configurations before creating the final `Producer` instance.
pub fn new_producer(&self) -> ProducerBuilder {
ProducerBuilder::new(self)
}
/// Returns a new `ConsumerBuilder` for configuring and creating a `Consumer` instance.
///
/// This method initializes a `ConsumerBuilder`, which is used to set up various options and settings for a `Consumer`.
/// The builder pattern allows you to specify details such as the topic, consumer name, subscription, subscription type, and other configurations before creating the final `Consumer` instance.
pub fn new_consumer(&self) -> ConsumerBuilder {
ConsumerBuilder::new(self)
}
/// Retrieves the address of the broker responsible for a specified topic.
///
/// This asynchronous method performs a lookup to find the broker that is responsible for the given topic. The `addr` parameter specifies the address of the broker to connect to for performing the lookup. The method returns information about the broker handling the topic.
///
/// # Parameters
///
/// - `addr`: The address of the broker to connect to for the lookup. This is provided as a `&Uri`, which specifies where the request should be sent.
/// - `topic`: The name of the topic for which to look up the broker.
///
/// # Returns
///
/// - `Ok(LookupResult)`: Contains the result of the lookup operation, including the broker address.
/// - `Err(e)`: An error if the lookup fails or if there are issues during the operation. This could include connectivity problems, invalid topic names, or other errors related to the lookup process.
pub async fn lookup_topic(&self, addr: &Uri, topic: impl Into<String>) -> Result<LookupResult> {
self.lookup_service.lookup_topic(addr, topic).await
}
/// Retrieves the schema associated with a specified topic from the schema service.
///
/// This asynchronous method fetches the schema for the given topic from the schema service. The schema describes the structure and format of the messages for the specified topic. The method returns the schema details or an error if the retrieval fails.
///
/// # Parameters
///
/// - `topic`: The name of the topic for which the schema is to be retrieved.
///
/// # Returns
///
/// - `Ok(Schema)`: The schema associated with the specified topic. This includes information about the schema type and its definition, if available.
/// - `Err(e)`: An error if the schema retrieval fails or if there are issues during the operation. This could include errors such as non-existent topics, connectivity issues, or internal service errors.
pub async fn get_schema(&self, topic: impl Into<String>) -> Result<Schema> {
self.schema_service.get_schema(&self.uri, topic).await
}
}
/// A builder for configuring and creating a `DanubeClient` instance.
///
/// The `DanubeClientBuilder` struct provides methods for setting various options needed to construct a `DanubeClient`. This includes configuring the base URI for the Danube service, connection settings.
///
/// # Fields
///
/// - `uri`: The base URI for the Danube service. This is a required field and specifies the address of the service that the client will connect to. It is essential for constructing the `DanubeClient`.
/// - `connection_options`: Optional connection settings that define how the grpc client connects to the Danube service. These settings can include parameters such as timeouts, retries, and other connection-related configurations.
#[derive(Debug, Clone, Default)]
pub struct DanubeClientBuilder {
uri: String,
connection_options: ConnectionOptions,
}
impl DanubeClientBuilder {
/// Sets the base URI for the Danube service in the builder.
///
/// This method configures the base URI that the `DanubeClient` will use to connect to the Danube service. The base URI is a required parameter for establishing a connection and interacting with the service.
///
/// # Parameters
///
/// - `url`: The base URI to use for connecting to the Danube service. The URI should include the protocol and address of the Danube service.
pub fn service_url(mut self, url: impl Into<String>) -> Self {
self.uri = url.into();
self
}
/// Sets optional connection settings for the client in the builder.
///
/// This method allows you to configure various connection settings for the `DanubeClient` through the builder. These settings determine how the client connects to the grpc Danube service and can be tailored to meet specific requirements.
///
/// # Parameters
///
/// - `connection_options`: A `ConnectionOptions` instance that includes various settings for configuring the client's connection. This may include parameters such as connection timeouts, keep alive interval.
pub fn with_connection_options(mut self, connection_options: ConnectionOptions) -> Self {
self.connection_options = connection_options;
self
}
/// Constructs and returns a `DanubeClient` instance based on the configuration specified in the builder.
///
/// This method finalizes the configuration and creates a new `DanubeClient` instance. It uses the settings and options that were configured using the `DanubeClientBuilder` methods.
///
/// # Returns
///
/// - `Ok(DanubeClient)`: A new instance of `DanubeClient` configured with the specified options.
/// - `Err(e)`: An error if the configuration is invalid or incomplete.
pub fn build(self) -> Result<DanubeClient> {
let uri = self.uri.parse::<Uri>()?;
Ok(DanubeClient::new_client(self, uri))
}
}