Skip to content

Commit 80398b1

Browse files
committed
chore(controller): optimize the use of master and slave database rules
1 parent 04deae0 commit 80398b1

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

rootfs/api/models/app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,21 @@ def _refresh_ingress(self, hosts, tls_map, ssl_redirect, appsettings):
207207
except KubeException as e:
208208
raise ServiceUnavailable('Could not create Ingress in Kubernetes') from e
209209

210-
def refresh(self, app_settings=None, tls=None, domains=None):
210+
def refresh(self):
211211
"""
212212
Read and write are separated, in transaction the read database is not updated.
213-
When calling, the corresponding resource object needs
213+
When calling, the corresponding resource object needs.
214214
"""
215215
if not getattr(self, 'refresh_enabled', True):
216216
return
217-
app_settings = app_settings if app_settings else self.appsettings_set.latest()
217+
app_settings = self.appsettings_set.latest()
218218
if not app_settings.routable:
219219
return
220-
tls = tls if tls else self.tls_set.latest()
220+
tls = self.tls_set.latest()
221221
ssl_redirect = bool(tls.https_enforced)
222222
certs_auto_enabled = bool(tls.certs_auto_enabled)
223223
hosts, tls_map = [], defaultdict(list)
224-
domains = domains if domains else Domain.objects.filter(app=self)
224+
domains = Domain.objects.filter(app=self)
225225
for domain in domains:
226226
host = str(domain.domain)
227227
hosts.append(host)

rootfs/api/models/appsettings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,4 @@ def save(self, *args, **kwargs):
180180
summary = ' '.join(self.summary)
181181
self.app.log('summary of app setting changes: {}'.format(summary), logging.DEBUG)
182182
super(AppSettings, self).save(**kwargs)
183-
# Read and write are separated, in transaction the read database is not updated
184-
self.app.refresh(app_settings=self)
183+
self.app.refresh()

rootfs/api/models/domain.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,15 @@ class Meta:
2828
@transaction.atomic
2929
def save(self, *args, **kwargs):
3030
super(Domain, self).save(*args, **kwargs)
31-
# Read and write are separated, in transaction the read database is not updated
32-
domains = list(self.app.domain_set.all())
33-
if self in domains:
34-
domains.remove(self)
35-
domains.append(self)
36-
self.app.refresh(domains=domains)
31+
self.app.refresh()
3732

3833
@transaction.atomic
3934
def delete(self, *args, **kwargs):
4035
# Deatch cert, updates k8s
4136
if self.certificate:
4237
self.certificate.detach(domain=str(self.domain))
4338
super(Domain, self).delete(*args, **kwargs)
44-
# Read and write are separated, in transaction the read database is not updated
45-
domains = list(self.app.domain_set.all())
46-
if self in domains:
47-
domains.remove(self)
48-
self.app.refresh(domains=domains)
39+
self.app.refresh()
4940

5041
def __str__(self):
5142
return self.domain

rootfs/api/models/tls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ def save(self, *args, **kwargs):
4949
self.sync()
5050

5151
def sync(self):
52-
# Read and write are separated, in transaction the read database is not updated
53-
self.app.refresh(tls=self)
52+
self.app.refresh()

rootfs/api/routers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1+
from asgiref.local import Local
12
from django.conf import settings
23

34

45
class DefaultReplicaRouter(object):
6+
"""
7+
If a model of the current thread or coroutine has used the master db,
8+
the model will also use the master in the future.
9+
This can avoid the problem that the slave database is not synchronized
10+
because a transaction is not committed.
11+
"""
12+
thread_critical = False
13+
14+
def __init__(self):
15+
self._tracker = Local(self.thread_critical)
516

617
def db_for_read(self, model, **hints):
7-
if 'replica' in settings.DATABASES:
18+
tracker_key = ".".join([model.__module__, model.__name__])
19+
if hasattr(self._tracker, tracker_key):
20+
return getattr(self._tracker, tracker_key)
21+
elif 'replica' in settings.DATABASES:
822
return 'replica'
923
return 'default'
1024

1125
def db_for_write(self, model, **hints):
26+
tracker_key = ".".join([model.__module__, model.__name__])
27+
if 'replica' in settings.DATABASES:
28+
setattr(self._tracker, tracker_key, 'default')
1229
return 'default'
1330

1431
def allow_relation(self, obj1, obj2, **hints):

0 commit comments

Comments
 (0)