skip to Main Content

I know there are other similar questions but I believe I have gone through all of them but none of them was able to solve my problem.
Everything is working fine when I am using WSGI server. It only happens when I use ASGI server however I must use ASGI server because my project’s functionalities are limited without it. I am using postgres database.

Also, I believe there is no problem while running the project on localhost because I checked the number of connections to my database (which is on my machine for development) and I saw just 1. That 1 connection was also not from the django project to my database but it was my own connection which I was using to access the database explicitly. I don’t know why there was not even a single connection from my project to the database.

Procfile

    web: daphne aretheycoming.asgi:application --port $PORT --bind 0.0.0.0 -v2
    aretheycomingworker: python manage.py runworker --settings=aretheycoming.settings -v2
asgi.py

    import os
    import django
    from channels.routing import get_default_application

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aretheycoming.settings")
    django.setup()
    application = get_default_application()
settings.py

    import django_heroku

    import os

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = hidden

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = []


    # Application definition

    INSTALLED_APPS = [
        'channels',
        .
        .
        .
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]

    ROOT_URLCONF = 'aretheycoming.urls'

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    WSGI_APPLICATION = 'aretheycoming.wsgi.application'
    ASGI_APPLICATION = 'aretheycoming.routing.application'

    CHANNEL_LAYERS = {
        'default': {
            'BACKEND': 'channels_redis.core.RedisChannelLayer',
            'CONFIG': {
                "hosts": [('127.0.0.1', 6379)],
                "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')]
            },
        },
    }


    # Database
    # https://docs.djangoproject.com/en/2.2/ref/settings/#databases

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': hidden,
            'USER': hidden,
            'PASSWORD': hidden,
            'HOST': '127.0.0.1',
            'PORT': '5432',
        }
    }


    # Password validation
    # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]


    # Internationalization
    # https://docs.djangoproject.com/en/2.2/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/2.2/howto/static-files/

    STATIC_ROOT = ''

    STATIC_URL = '/static/'

    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]

    # Activate Django-Heroku.
    django_heroku.settings(locals())

I tried using django.db.close_all_connections() but it didn’t work and the project works fine on wsgi server anyway so I don’t think that’s the problem.
I don’t have a good understanding of this stuff so I would really appreciate it if you can be a little extra explanatory.

I have the environment variable ASGI_THREADS = 1 set on Heroku.

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution that seems to be working fine. The solution is pgbouncer. Install pgbouncer to heroku by executing the following commands:

    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-pgbouncer
    
    heroku buildpacks:add heroku/python
    

    After that, if you deploy to Heroku, you might see the following error on Heroku logs-

    heroku server does not support SSL, but SSL was required
    

    A very simple working solution to that is given by LtKvasir here.

    I still have db.connections.close_all() written everywhere in my code but I think that's really not necessary now.


  2. https://devcenter.heroku.com/articles/postgres-logs-errors#fatal-too-many-connections-for-role

    Too achieve asynchronity it is establishing multiple connections at the same time. This is why it is using more than 1 connection.

    This user had the same problem and a solution was provided: Django/Heroku: FATAL: too many connections for role

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search