I have a consumer reading from kafka, which has a continuous stream of events, everyone has to write to the mongo collection so often, for which I have to have mongo open continuously. My solution, which is pretty hacks, I feel, is to reinitialize the connection every 5 minutes or so to avoid a network timeout. This is done in order to avoid periods in which there are no events from kafka, and the connection is idle.
Can anyone suggest a better way to do this? Since I'm sure this is the wrong way to establish a continuous connection with mongo.
I am using pymongo client.
I have a MongoAdapter class that has helper methods:
from pymongo import MongoClient
import pymongo
import time
class MongoAdapter:
def __init__(self,databaseName,userid,password,host):
self.databaseName=databaseName
self.userid=userid
self.password=password
self.host=host
self.connection=MongoClient(host=self.host,maxPoolSize=100,socketTimeoutMS=1000,connectTimeoutMS=1000)
self.getDatabase()
def getDatabase(self):
try:
if(self.connection[self.databaseName].authenticate(self.userid,self.password)):
print "authenticated true"
self.database=self.connection[self.databaseName]
except pymongo.errors.OperationFailure:
print "Error: Please check Database Name, UserId,Password"
:
adapter_reinit_threshold=300
adapter_config_time=time.time()
while True
if (time.time()-adapter_config_time) > adapter_reinit_threshold:
adapter=MongoAdapter(config.db_name,config.db_user,config.db_password,config.db_host)
adapter_config_time=time.time()
, , , , ( ). , , .