Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 682d31c

Browse files
authored
Allow use of the /filter Client-Server APIs on workers. (#15134)
1 parent c369d82 commit 682d31c

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

changelog.d/15134.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow use of the `/filter` Client-Server APIs on workers.

docker/configure_workers_and_start.py

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"^/_matrix/client/(api/v1|r0|v3|unstable/.*)/rooms/.*/aliases",
143143
"^/_matrix/client/v1/rooms/.*/timestamp_to_event$",
144144
"^/_matrix/client/(api/v1|r0|v3|unstable)/search",
145+
"^/_matrix/client/(r0|v3|unstable)/user/.*/filter(/|$)",
145146
],
146147
"shared_extra_conf": {},
147148
"worker_extra_conf": "",

docs/workers.md

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ information.
232232
^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$
233233
^/_matrix/client/v1/rooms/.*/timestamp_to_event$
234234
^/_matrix/client/(api/v1|r0|v3|unstable)/search$
235+
^/_matrix/client/(r0|v3|unstable)/user/.*/filter(/|$)
235236

236237
# Encryption requests
237238
^/_matrix/client/(r0|v3|unstable)/keys/query$

synapse/rest/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ def register_servlets(client_resource: HttpServer, hs: "HomeServer") -> None:
108108
if is_main_process:
109109
logout.register_servlets(hs, client_resource)
110110
sync.register_servlets(hs, client_resource)
111-
if is_main_process:
112-
filter.register_servlets(hs, client_resource)
111+
filter.register_servlets(hs, client_resource)
113112
account.register_servlets(hs, client_resource)
114113
register.register_servlets(hs, client_resource)
115114
if is_main_process:

synapse/storage/databases/main/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from .event_push_actions import EventPushActionsStore
4444
from .events_bg_updates import EventsBackgroundUpdatesStore
4545
from .events_forward_extremities import EventForwardExtremitiesStore
46-
from .filtering import FilteringStore
46+
from .filtering import FilteringWorkerStore
4747
from .keys import KeyStore
4848
from .lock import LockStore
4949
from .media_repository import MediaRepositoryStore
@@ -99,7 +99,7 @@ class DataStore(
9999
EventFederationStore,
100100
MediaRepositoryStore,
101101
RejectionsStore,
102-
FilteringStore,
102+
FilteringWorkerStore,
103103
PusherStore,
104104
PushRuleStore,
105105
ApplicationServiceTransactionStore,

synapse/storage/databases/main/filtering.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from canonicaljson import encode_canonical_json
1919

20-
from synapse.api.errors import Codes, SynapseError
20+
from synapse.api.errors import Codes, StoreError, SynapseError
2121
from synapse.storage._base import SQLBaseStore, db_to_json
2222
from synapse.storage.database import LoggingTransaction
2323
from synapse.types import JsonDict
@@ -46,8 +46,6 @@ async def get_user_filter(
4646

4747
return db_to_json(def_json)
4848

49-
50-
class FilteringStore(FilteringWorkerStore):
5149
async def add_user_filter(self, user_localpart: str, user_filter: JsonDict) -> int:
5250
def_json = encode_canonical_json(user_filter)
5351

@@ -79,4 +77,23 @@ def _do_txn(txn: LoggingTransaction) -> int:
7977

8078
return filter_id
8179

82-
return await self.db_pool.runInteraction("add_user_filter", _do_txn)
80+
attempts = 0
81+
while True:
82+
# Try a few times.
83+
# This is technically needed if a user tries to create two filters at once,
84+
# leading to two concurrent transactions.
85+
# The failure case would be:
86+
# - SELECT filter_id ... filter_json = ? → both transactions return no rows
87+
# - SELECT MAX(filter_id) ... → both transactions return e.g. 5
88+
# - INSERT INTO ... → both transactions insert filter_id = 6
89+
# One of the transactions will commit. The other will get a unique key
90+
# constraint violation error (IntegrityError). This is not the same as a
91+
# serialisability violation, which would be automatically retried by
92+
# `runInteraction`.
93+
try:
94+
return await self.db_pool.runInteraction("add_user_filter", _do_txn)
95+
except self.db_pool.engine.module.IntegrityError:
96+
attempts += 1
97+
98+
if attempts >= 5:
99+
raise StoreError(500, "Couldn't generate a filter ID.")

0 commit comments

Comments
 (0)