23 lines
		
	
	
		
			568 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			23 lines
		
	
	
		
			568 B
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Optional
 | 
						|
 | 
						|
from aiokafka import AIOKafkaProducer
 | 
						|
 | 
						|
from backend.config import KAFKA_URL, KAFKA_ENABLE_SSL
 | 
						|
from external.kafka.context import context
 | 
						|
 | 
						|
_producer: Optional[AIOKafkaProducer] = None
 | 
						|
 | 
						|
 | 
						|
async def init_producer():
 | 
						|
    global _producer
 | 
						|
    _producer = AIOKafkaProducer(
 | 
						|
        bootstrap_servers=KAFKA_URL,
 | 
						|
        security_protocol='SSL' if KAFKA_ENABLE_SSL else 'PLAINTEXT',
 | 
						|
        ssl_context=context if KAFKA_ENABLE_SSL else None,
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
async def get_producer() -> Optional[AIOKafkaProducer]:
 | 
						|
    global _producer
 | 
						|
    return _producer
 |