|
| 1 | +/** |
| 2 | + * @fileoverview Provides client management functionality for Kafka and Confluent Cloud services. |
| 3 | + */ |
| 4 | + |
| 5 | +import { KafkaJS } from "@confluentinc/kafka-javascript"; |
| 6 | +import { |
| 7 | + confluentCloudAuthMiddleware, |
| 8 | + confluentCloudFlinkAuthMiddleware, |
| 9 | +} from "@src/confluent/middleware.js"; |
| 10 | +import { paths } from "@src/confluent/openapi-schema.js"; |
| 11 | +import { AsyncLazy, Lazy } from "@src/lazy.js"; |
| 12 | +import createClient, { Client } from "openapi-fetch"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Interface for managing Kafka client connections and operations. |
| 16 | + */ |
| 17 | +export interface KafkaClientManager { |
| 18 | + /** Gets the main Kafka client instance */ |
| 19 | + getKafkaClient(): KafkaJS.Kafka; |
| 20 | + /** Gets a connected admin client for Kafka administration operations */ |
| 21 | + getAdminClient(): Promise<KafkaJS.Admin>; |
| 22 | + /** Gets a connected producer client for publishing messages */ |
| 23 | + getProducer(): Promise<KafkaJS.Producer>; |
| 24 | + /** Gets a connected consumer client for subscribing to topics */ |
| 25 | + getConsumer(): Promise<KafkaJS.Consumer>; |
| 26 | + /** Disconnects and cleans up all client connections */ |
| 27 | + disconnect(): Promise<void>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Interface for managing Confluent Cloud REST client connections. |
| 32 | + */ |
| 33 | +export interface ConfluentCloudRestClientManager { |
| 34 | + /** Gets a configured REST client for Confluent Cloud Flink operations */ |
| 35 | + getConfluentCloudFlinkRestClient(): Client<paths, `${string}/${string}`>; |
| 36 | + /** Gets a configured REST client for general Confluent Cloud operations */ |
| 37 | + getConfluentCloudRestClient(): Client<paths, `${string}/${string}`>; |
| 38 | +} |
| 39 | + |
| 40 | +export interface ClientManager |
| 41 | + extends KafkaClientManager, |
| 42 | + ConfluentCloudRestClientManager {} |
| 43 | + |
| 44 | +/** |
| 45 | + * Default implementation of client management for Kafka and Confluent Cloud services. |
| 46 | + * Manages lifecycle and lazy initialization of various client connections. |
| 47 | + */ |
| 48 | +export class DefaultClientManager implements ClientManager { |
| 49 | + private readonly kafkaClient: Lazy<KafkaJS.Kafka>; |
| 50 | + private readonly adminClient: AsyncLazy<KafkaJS.Admin>; |
| 51 | + private readonly producer: AsyncLazy<KafkaJS.Producer>; |
| 52 | + private readonly confluentCloudFlinkRestClient: Lazy< |
| 53 | + Client<paths, `${string}/${string}`> |
| 54 | + >; |
| 55 | + private readonly confluentCloudRestClient: Lazy< |
| 56 | + Client<paths, `${string}/${string}`> |
| 57 | + >; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a new DefaultClientManager instance. |
| 61 | + * @param config - Configuration options for KafkaJS client |
| 62 | + * @param confluentCloudBaseUrl - Base URL for Confluent Cloud REST API |
| 63 | + * @param confluentCloudFlinkBaseUrl - Base URL for Flink REST API |
| 64 | + */ |
| 65 | + constructor( |
| 66 | + config: KafkaJS.CommonConstructorConfig, |
| 67 | + confluentCloudBaseUrl?: string, |
| 68 | + confluentCloudFlinkBaseUrl?: string, |
| 69 | + ) { |
| 70 | + this.kafkaClient = new Lazy(() => new KafkaJS.Kafka(config)); |
| 71 | + this.adminClient = new AsyncLazy( |
| 72 | + async () => { |
| 73 | + console.error("Connecting Kafka Admin"); |
| 74 | + const admin = this.kafkaClient.get().admin(); |
| 75 | + await admin.connect(); |
| 76 | + return admin; |
| 77 | + }, |
| 78 | + (admin) => admin.disconnect(), |
| 79 | + ); |
| 80 | + this.producer = new AsyncLazy( |
| 81 | + async () => { |
| 82 | + console.error("Connecting Kafka Producer"); |
| 83 | + const producer = this.kafkaClient.get().producer({ |
| 84 | + kafkaJS: { |
| 85 | + acks: 1, |
| 86 | + compression: KafkaJS.CompressionTypes.GZIP, |
| 87 | + }, |
| 88 | + }); |
| 89 | + await producer.connect(); |
| 90 | + return producer; |
| 91 | + }, |
| 92 | + (producer) => producer.disconnect(), |
| 93 | + ); |
| 94 | + |
| 95 | + this.confluentCloudRestClient = new Lazy(() => { |
| 96 | + console.error( |
| 97 | + `Initializing Confluent Cloud REST client for base URL ${confluentCloudBaseUrl}`, |
| 98 | + ); |
| 99 | + const client = createClient<paths>({ |
| 100 | + baseUrl: confluentCloudBaseUrl, |
| 101 | + }); |
| 102 | + client.use(confluentCloudAuthMiddleware); |
| 103 | + return client; |
| 104 | + }); |
| 105 | + |
| 106 | + this.confluentCloudFlinkRestClient = new Lazy(() => { |
| 107 | + console.error( |
| 108 | + `Initializing Confluent Cloud Flink REST client for base URL ${confluentCloudFlinkBaseUrl}`, |
| 109 | + ); |
| 110 | + const client = createClient<paths>({ |
| 111 | + baseUrl: confluentCloudFlinkBaseUrl, |
| 112 | + }); |
| 113 | + client.use(confluentCloudFlinkAuthMiddleware); |
| 114 | + return client; |
| 115 | + }); |
| 116 | + } |
| 117 | + getConsumer(): Promise<KafkaJS.Consumer> { |
| 118 | + throw new Error("Method not implemented."); |
| 119 | + } |
| 120 | + |
| 121 | + /** @inheritdoc */ |
| 122 | + getKafkaClient(): KafkaJS.Kafka { |
| 123 | + return this.kafkaClient.get(); |
| 124 | + } |
| 125 | + |
| 126 | + /** @inheritdoc */ |
| 127 | + getConfluentCloudFlinkRestClient(): Client<paths, `${string}/${string}`> { |
| 128 | + return this.confluentCloudFlinkRestClient.get(); |
| 129 | + } |
| 130 | + |
| 131 | + /** @inheritdoc */ |
| 132 | + getConfluentCloudRestClient(): Client<paths, `${string}/${string}`> { |
| 133 | + return this.confluentCloudRestClient.get(); |
| 134 | + } |
| 135 | + |
| 136 | + /** @inheritdoc */ |
| 137 | + async getAdminClient(): Promise<KafkaJS.Admin> { |
| 138 | + return this.adminClient.get(); |
| 139 | + } |
| 140 | + |
| 141 | + /** @inheritdoc */ |
| 142 | + async getProducer(): Promise<KafkaJS.Producer> { |
| 143 | + return this.producer.get(); |
| 144 | + } |
| 145 | + |
| 146 | + /** @inheritdoc */ |
| 147 | + async disconnect(): Promise<void> { |
| 148 | + await this.adminClient.close(); |
| 149 | + await this.producer.close(); |
| 150 | + this.kafkaClient.close(); |
| 151 | + } |
| 152 | +} |
0 commit comments