Django/Heroku: FATAL: too many connections for role

戏子无情 提交于 2020-04-10 08:13:25

问题


So I just launched a website with Channels 2.0 Daphne 2.2.0 and asgi via Heroku (Hobby) and Postgres (trial). When I launch my website, I click around for a few pages and I get a 500 error. The error message I get emailed is FATAL: too many connections for role ..."

When I run heroku pg:killall or wait long enough I can click around a few more times until the error message repeats itself. However, when I run heroku pg it shows Connections 0/20. Does anyone know what is going on and how I can stop the errors? It's possible that I have two many connections open for a second, but it doesn't seem that way.

File "/app/.heroku/python/lib/python3.6/site-

packages/django/contrib/sessions/backends/base.py" in _get_session
  191.             return self._session_cache

During handling of the above exception ('SessionStore' object has no attribute '_session_cache'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  216.                 self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
  194.         self.connection = self.get_new_connection(conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
  168.         connection = Database.connect(**conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
  130.     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

The above exception (FATAL:  too many connections for role "polewdwynmvyyt"
) was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "./myproject/views.py" in home_page
  8.     print(request.session.get("first_name","Unknown")) #getter

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in get
  66.         return self._session.get(key, default)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in _get_session
  196.                 self._session_cache = self.load()

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in load
  34.                 expire_date__gt=timezone.now()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in get
  397.         num = len(clone)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in __len__
  254.         self._fetch_all()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
  1179.             self._result_cache = list(self._iterable_class(self))

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
  53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1066.             cursor = self.connection.cursor()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in cursor
  255.         return self._cursor()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in _cursor
  232.         self.ensure_connection()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  216.                 self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  216.                 self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
  194.         self.connection = self.get_new_connection(conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
  168.         connection = Database.connect(**conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
  130.     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

回答1:


Seems like the connections are not being reused and/or a new thread is being created for each request.

Remove the CONN_MAX_AGE from dj_database_url.config(default=DATABASE_URL) on settings.py (if you are using dj_database_url).

Set the environment variable ASGI_THREADS to a number of threads lower than your connections limit on your .asgi file or on heroku site -> settings -> configVars. If you are using daphne, the default threads are CPU cores * 5, each thread with a connection.

example.asgi file:

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourAppName.settings")
os.environ['ASGI_THREADS']="4"
django.setup()
application = get_default_application()


来源:https://stackoverflow.com/questions/51510259/django-heroku-fatal-too-many-connections-for-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!