Skip to content

Commit 415a3ac

Browse files
committed
Add ping method to async confluent producer and fix confluent ping method
1 parent 6ab41fa commit 415a3ac

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

faststream/confluent/broker/broker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,5 @@ async def ping(self, timeout: Optional[float]) -> bool:
533533

534534
if self._producer is None:
535535
return False
536+
537+
return await self._producer._producer.ping(timeout=timeout)

faststream/confluent/client.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626

2727
_missing = object()
2828

29+
ADMINCLIENT_CONFIG_PARAMS = (
30+
"allow.auto.create.topics",
31+
"bootstrap.servers",
32+
"client.id",
33+
"request.timeout.ms",
34+
"metadata.max.age.ms",
35+
"security.protocol",
36+
"connections.max.idle.ms",
37+
"sasl.mechanism",
38+
"sasl.username",
39+
"sasl.password",
40+
)
41+
2942

3043
class MsgToSend(BaseModel):
3144
"""A Pydantic model representing a message to be sent to Kafka.
@@ -214,6 +227,24 @@ async def send_batch(
214227
]
215228
await asyncio.gather(*tasks)
216229

230+
async def ping(self, timeout: Optional[float] = 5.0) -> bool:
231+
"""Implement ping using AdminClient."""
232+
try:
233+
admin_client = AdminClient(
234+
{
235+
x: self.config[x]
236+
for x in ADMINCLIENT_CONFIG_PARAMS
237+
if x in self.config
238+
}
239+
)
240+
cluster_metadata = await call_or_await(
241+
admin_client.list_topics, timeout=timeout
242+
)
243+
244+
return bool(cluster_metadata)
245+
except Exception:
246+
return False
247+
217248

218249
class TopicPartition(NamedTuple):
219250
"""A named tuple representing a Kafka topic and partition."""
@@ -228,22 +259,8 @@ def create_topics(
228259
logger: Union["LoggerProto", None, object] = logger,
229260
) -> None:
230261
"""Creates Kafka topics using the provided configuration."""
231-
required_config_params = (
232-
"allow.auto.create.topics",
233-
"bootstrap.servers",
234-
"client.id",
235-
"request.timeout.ms",
236-
"metadata.max.age.ms",
237-
"security.protocol",
238-
"connections.max.idle.ms",
239-
"sasl.mechanism",
240-
"sasl.username",
241-
"sasl.password",
242-
"sasl.kerberos.service.name",
243-
)
244-
245262
admin_client = AdminClient(
246-
{x: config[x] for x in required_config_params if x in config}
263+
{x: config[x] for x in ADMINCLIENT_CONFIG_PARAMS if x in config}
247264
)
248265

249266
fs = admin_client.create_topics(

0 commit comments

Comments
 (0)