服务器有时会意外关闭连接

时间:2019-09-26 02:34:41

标签: django postgresql operationalerror python-socketio

项目背景:

  1. Django项目,我使用gunicorn运行该项目,在项目中,我使用python socket io处理某些事件
  2. PostgreSQL,配置如下:
DATABASES['default'] = {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': xxx,
    'USER': xxx,
    'PASSWORD': xxx,
    'HOST': 'xxx',
    'PORT': '5432',
    'CONN_MAX_AGE': 60 * 10,  # seconds
    'OPTIONS': {
        'connect_timeout': 20,
    },
}

python socket io将保留一个线程来处理一些事件。因此该线程具有自己的postgresql数据库连接。有时候,我发现它会引发数据库连接问题,就像这样,我不知道为什么数据库连接不能正常关闭

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/engineio/server.py", line 520, in _trigger_event
    return self.handlers[event](*args)
  File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 590, in _handle_eio_message
    self._handle_event(sid, pkt.namespace, pkt.id, pkt.data)
  File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 526, in _handle_event
    self._handle_event_internal(self, sid, data, namespace, id)
  File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 529, in _handle_event_internal
    r = server._trigger_event(data[0], namespace, sid, *data[1:])
  File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 558, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "/usr/src/app/async_worker/controllers/server/event_handler.py", line 61, in on_jira_handle_from_client_retrieve
    ticket = Ticket.get_ticket_by_id(ticket_id)
  File "/usr/src/app/review/models/ticket.py", line 144, in get_ticket_by_id
    return Ticket.objects.filter(id=ticket_id).first()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 564, in first
    objects = list((self if self.ordered else self.order_by('pk'))[:1])
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
    raise original_exception
OperationalError: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

这是我在PostgreSQL日志文件中得到的:

LOG: could not receive data from client: Connection reset by peer

有关该问题的一些想法?我认为如果连接保持的时间太长(> CONN_MAX_AGE),则应该关闭conn并将其设置为None,但实际上连接不是null,而是已经关闭的

1 个答案:

答案 0 :(得分:0)

您看到的错误消息来自PostgreSQL客户端库。

现在PostgreSQL服务器和客户端都抱怨对方突然消失了,因此几乎可以肯定这是一个神经网络问题。

我的钱放在配置错误的防火墙上,该防火墙会在一段时间后断开空闲连接。我一直想知道是否有任何有效用例可以让防火墙执行此操作...