Skip to content

Commit b993d76

Browse files
authored
Merge pull request #137 from cloudblue/feature/LITE-27792
LITE-27792 Initial support for replica `CQRS_QUERY_TIMEOUT`
2 parents 80498b4 + 416dc8a commit b993d76

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

dj_cqrs/controller/consumer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def route_signal_to_replica_model(
6565

6666
is_meta_supported = model_cls.CQRS_META
6767
try:
68+
if db_is_needed:
69+
_apply_query_timeouts(model_cls)
70+
6871
with transaction.atomic(savepoint=False) if db_is_needed else ExitStack():
6972
if signal_type == SignalType.DELETE:
7073
if is_meta_supported:
@@ -97,3 +100,24 @@ def route_signal_to_replica_model(
97100
model_cls.CQRS_ID,
98101
),
99102
)
103+
104+
105+
def _apply_query_timeouts(model_cls): # pragma: no cover
106+
query_timeout = int(settings.CQRS['replica'].get('CQRS_QUERY_TIMEOUT', 0))
107+
if query_timeout <= 0:
108+
return
109+
110+
model_db = model_cls._default_manager.db
111+
conn = transaction.get_connection(using=model_db)
112+
conn_vendor = getattr(conn, 'vendor', '')
113+
114+
if conn_vendor not in {'postgresql', 'mysql'}:
115+
return
116+
117+
if conn_vendor == 'postgresql':
118+
statement = 'SET statement_timeout TO %s'
119+
else:
120+
statement = 'SET SESSION MAX_EXECUTION_TIME=%s'
121+
122+
with conn.cursor() as cursor:
123+
cursor.execute(statement, params=(query_timeout,))

0 commit comments

Comments
 (0)