Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only serve from replica when there is a request in scope #8221

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions polling_stations/db_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@

from django.conf import settings
from django.db import DEFAULT_DB_ALIAS
from django_middleware_global_request import get_request


PRINCIPAL = settings.PRINCIPAL_DB_NAME
REPLICA = DEFAULT_DB_ALIAS


class ReplicationRouter(object):
def db_for_read(self, model, **hints):
# In CI, there is only one DB connection
if os.environ.get("CIRCLECI"):
return DEFAULT_DB_ALIAS

# We don't need to scale requests touching these models
# but we do want to prevent race conditions
# when reading a record we just wrote
if model._meta.label in (
"file_uploads.Upload",
"file_uploads.File",
) and not os.environ.get("CIRCLECI"):
return settings.PRINCIPAL_DB_NAME
return DEFAULT_DB_ALIAS
):
return PRINCIPAL

# We only care about trying to scale
# by serving traffic from a replica
# when we are serving HTTP traffic
request = get_request()
if request:
if request.path.startswith("/admin"):
return PRINCIPAL
return REPLICA

# in all other cases (e.g: management commands, shell)
# perform reads from the principal to prevent race conditions
return PRINCIPAL

def db_for_write(self, model, **hints):
if os.environ.get("CIRCLECI"):
return DEFAULT_DB_ALIAS
return settings.PRINCIPAL_DB_NAME

return PRINCIPAL

def allow_relation(self, obj1, obj2, **hints):
return True
Expand All @@ -26,6 +51,6 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):


def get_principal_db_name():
if settings.PRINCIPAL_DB_NAME in settings.DATABASES:
return settings.PRINCIPAL_DB_NAME
return DEFAULT_DB_ALIAS
if PRINCIPAL in settings.DATABASES:
return PRINCIPAL
return REPLICA
2 changes: 2 additions & 0 deletions polling_stations/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def get_ec2_ip():
MIDDLEWARE = (
"whitenoise.middleware.WhiteNoiseMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django_middleware_global_request.middleware.GlobalRequestMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.locale.LocaleMiddleware",
Expand Down Expand Up @@ -197,6 +198,7 @@ def get_ec2_ip():
"dc_design_system",
"dc_utils",
"drf_spectacular",
"django_middleware_global_request",
)

PROJECT_APPS = (
Expand Down
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ django-dotenv
django-extensions
django-filter
django-localflavor
django-middleware-global-request
django-sesame
djangorestframework
djangorestframework-csv
Expand Down
7 changes: 7 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ django==4.2.13 \
# django-extensions
# django-filter
# django-localflavor
# django-middleware-global-request
# django-sesame
# djangorestframework
# drf-spectacular
Expand Down Expand Up @@ -233,6 +234,12 @@ django-localflavor==4.0 \
# -c requirements/constraints.txt
# -r requirements/base.in
# dc-django-utils
django-middleware-global-request==0.3.5 \
--hash=sha256:37c79c9cd80e73751ae2821696e20af13ba7628a0915ff1818d124e0e72980b1 \
--hash=sha256:7beb2c8ca9106aac641692cda2bac290bd7083a315446a12db06fa91b73903d6
# via
# -c requirements/constraints.txt
# -r requirements/base.in
django-pipeline==3.0.0 \
--hash=sha256:49a8bee298668100bb6e8a2144dff8c607baa5297820a2503793c38693f34103 \
--hash=sha256:e9e08b084ef3ebf599795510519a8d44f2240b487782bebf4a8fcaf6302c31d1
Expand Down
5 changes: 5 additions & 0 deletions requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ django==4.2.13 \
# django-extensions
# django-filter
# django-localflavor
# django-middleware-global-request
# django-sesame
# djangorestframework
# drf-spectacular
Expand Down Expand Up @@ -415,6 +416,10 @@ django-localflavor==4.0 \
# via
# -r requirements/base.in
# dc-django-utils
django-middleware-global-request==0.3.5 \
--hash=sha256:37c79c9cd80e73751ae2821696e20af13ba7628a0915ff1818d124e0e72980b1 \
--hash=sha256:7beb2c8ca9106aac641692cda2bac290bd7083a315446a12db06fa91b73903d6
# via -r requirements/base.in
django-pipeline==3.0.0 \
--hash=sha256:49a8bee298668100bb6e8a2144dff8c607baa5297820a2503793c38693f34103 \
--hash=sha256:e9e08b084ef3ebf599795510519a8d44f2240b487782bebf4a8fcaf6302c31d1
Expand Down