偶尔出现“连接错误:无法连接到数据库”到蒙戈

发布于 2024-10-21 08:37:59 字数 181 浏览 3 评论 0原文

我们目前正在测试一个基于 django 的项目,该项目使用 MongoEngine 作为持久层。 MongoEngine 基于 pymongo,我们使用 1.6 版本,并且运行 mongo 的单实例设置。

我们注意到,偶尔会在大约 5 分钟内无法与 mongo 实例建立连接。有人遇到过这样的行为吗?关于如何提高可靠性有什么建议吗?

We are currently testing a django based project that uses MongoEngine as the persistence layer. MongoEngine is based on pymongo and we're using version 1.6 and we are running a single instance setup of mongo.

What we have noticed is that occasionally, and for about 5 minutes, connections cannot be established to the mongo instance. Has anyone come across such behavior? any tips on how to improve reliability?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-10-28 08:37:59

我们遇到了 AutoReconnect 问题,听起来与您所描述的类似。我最终在我的 /__init__.py 文件中对 pymongo 进行了猴子修补:

from pymongo.cursor import Cursor                                                             
from pymongo.errors import AutoReconnect                                                      

from time import sleep                                                                        
import sys                                                                                    

AUTO_RECONNECT_ATTEMPTS = 10                                                                  
AUTO_RECONNECT_DELAY = 0.1                                                                    

def auto_reconnect(func):                                                                     
    """                                                                                       
    Function wrapper to automatically reconnect if AutoReconnect is raised.                   

    If still failing after AUTO_RECONNECT_ATTEMPTS, raise the exception after                 
    all. Technically this should be handled everytime a mongo query is                        
    executed so you can gracefully handle the failure appropriately, but this                 
    intermediary should handle 99% of cases and avoid having to put                           
    reconnection code all over the place.                                                     

    """                                                                                       
    def retry_function(*args, **kwargs):                                                      
        attempts = 0                                                                          
        while True:                                                                           
            try:                                                                              
                return func(*args, **kwargs)                                                  
            except AutoReconnect, e:                                                          
                attempts += 1                                                                 
                if attempts > AUTO_RECONNECT_ATTEMPTS:                                        
                    raise                                                                     
                sys.stderr.write(                                                             
                    '%s raised [%s] -- AutoReconnecting (#%d)...\n' % (                       
                        func.__name__, e, attempts))                                          
                sleep(AUTO_RECONNECT_DELAY)                                                   
    return retry_function                                                                     

# monkeypatch: wrap Cursor.__send_message (name-mangled)                                      
Cursor._Cursor__send_message = auto_reconnect(Cursor._Cursor__send_message)                   
# (may need to wrap some other methods also, we'll see...) 

这为我们解决了问题,但您可能会描述不同的东西?

We had an issue with AutoReconnect which sounds similar to what you are describing. I ended up monkeypatching pymongo in my <project>/__init__.py file:

from pymongo.cursor import Cursor                                                             
from pymongo.errors import AutoReconnect                                                      

from time import sleep                                                                        
import sys                                                                                    

AUTO_RECONNECT_ATTEMPTS = 10                                                                  
AUTO_RECONNECT_DELAY = 0.1                                                                    

def auto_reconnect(func):                                                                     
    """                                                                                       
    Function wrapper to automatically reconnect if AutoReconnect is raised.                   

    If still failing after AUTO_RECONNECT_ATTEMPTS, raise the exception after                 
    all. Technically this should be handled everytime a mongo query is                        
    executed so you can gracefully handle the failure appropriately, but this                 
    intermediary should handle 99% of cases and avoid having to put                           
    reconnection code all over the place.                                                     

    """                                                                                       
    def retry_function(*args, **kwargs):                                                      
        attempts = 0                                                                          
        while True:                                                                           
            try:                                                                              
                return func(*args, **kwargs)                                                  
            except AutoReconnect, e:                                                          
                attempts += 1                                                                 
                if attempts > AUTO_RECONNECT_ATTEMPTS:                                        
                    raise                                                                     
                sys.stderr.write(                                                             
                    '%s raised [%s] -- AutoReconnecting (#%d)...\n' % (                       
                        func.__name__, e, attempts))                                          
                sleep(AUTO_RECONNECT_DELAY)                                                   
    return retry_function                                                                     

# monkeypatch: wrap Cursor.__send_message (name-mangled)                                      
Cursor._Cursor__send_message = auto_reconnect(Cursor._Cursor__send_message)                   
# (may need to wrap some other methods also, we'll see...) 

This resolved the issue for us, but you might be describing something different?

迷你仙 2024-10-28 08:37:59

这是另一种解决方案,它使用子类化而不是猴子修补,并处理建立初始连接或访问数据库时可能引发的错误。我只是对 Connection/ReplicasetConnection 进行子类化,并处理实例化和任何方法调用期间引发的 AutoReconnect 错误。您可以在构造函数中指定重试次数和重试之间的睡眠时间。

您可以在此处查看要点:https://gist.github.com/2777345

Here is another solution that uses subclassing instead of monkey patching, and handles errors that might be raised while establishing the initial connection or while accessing a database. I just subclassed Connection/ReplicasetConnection, and handled raised AutoReconnect errors during instantiation and any method invocations. You can specify the number of retries and sleep time between retries in the constructor.

You can see the gist here: https://gist.github.com/2777345

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文