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

Commit

Permalink
Merge branch 'neilj/limit_exceeded_error' of github.com:matrix-org/sy…
Browse files Browse the repository at this point in the history
…napse into neilj/limit_exceeded_error
  • Loading branch information
neilisfragile committed Aug 17, 2018
2 parents 0195dfb + 69c49d3 commit 9fd161c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/3705.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support more federation endpoints on workers
1 change: 1 addition & 0 deletions changelog.d/3707.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add new error type ResourceLimit
1 change: 1 addition & 0 deletions changelog.d/3708.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For resource limit blocked users, prevent writing into rooms
1 change: 1 addition & 0 deletions changelog.d/3712.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update admin register API documentation to reference a real user ID.
2 changes: 1 addition & 1 deletion docs/admin_api/register_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ As an example::

< {
"access_token": "token_here",
"user_id": "@pepper_roni@test",
"user_id": "@pepper_roni:localhost",
"home_server": "test",
"device_id": "device_id_here"
}
Expand Down
3 changes: 2 additions & 1 deletion synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ def check_auth_blocking(self, user_id=None):
current_mau = yield self.store.get_monthly_active_count()
if current_mau >= self.hs.config.max_mau_value:
raise ResourceLimitError(
403, "Monthly Active User Limits AU Limit Exceeded",
403, "Monthly Active User Limit Exceeded",

admin_uri=self.hs.config.admin_uri,
errcode=Codes.RESOURCE_LIMIT_EXCEED,
limit_type="monthly_active_user"
Expand Down
2 changes: 2 additions & 0 deletions synapse/app/federation_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from synapse.metrics import RegistryProxy
from synapse.metrics.resource import METRICS_PREFIX, MetricsResource
from synapse.replication.slave.storage._base import BaseSlavedStore
from synapse.replication.slave.storage.account_data import SlavedAccountDataStore
from synapse.replication.slave.storage.appservice import SlavedApplicationServiceStore
from synapse.replication.slave.storage.directory import DirectoryStore
from synapse.replication.slave.storage.events import SlavedEventStore
Expand All @@ -54,6 +55,7 @@


class FederationReaderSlavedStore(
SlavedAccountDataStore,
SlavedProfileStore,
SlavedApplicationServiceStore,
SlavedPusherStore,
Expand Down
1 change: 1 addition & 0 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ def generate_user_daily_visit_stats():
clock.looping_call(
hs.get_datastore().reap_monthly_active_users, 1000 * 60 * 60
)
hs.get_datastore().reap_monthly_active_users()

@defer.inlineCallbacks
def generate_monthly_active_users():
Expand Down
6 changes: 5 additions & 1 deletion synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,14 @@ def create_event(self, requester, event_dict, token_id=None, txn_id=None,
where *hashes* is a map from algorithm to hash.
If None, they will be requested from the database.
Raises:
ResourceLimitError if server is blocked to some resource being
exceeded
Returns:
Tuple of created event (FrozenEvent), Context
"""
yield self.auth.check_auth_blocking(requester.user.to_string())

builder = self.event_builder_factory.new(event_dict)

self.validator.validate_new(builder)
Expand Down
4 changes: 4 additions & 0 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ def create_room(self, requester, config, ratelimit=True,
Raises:
SynapseError if the room ID couldn't be stored, or something went
horribly wrong.
ResourceLimitError if server is blocked to some resource being
exceeded
"""
user_id = requester.user.to_string()

self.auth.check_auth_blocking(user_id)

if not self.spam_checker.user_may_create_room(user_id):
raise SynapseError(403, "You are not permitted to create rooms")

Expand Down
5 changes: 4 additions & 1 deletion synapse/storage/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def _reap_users(txn):
# While Postgres does not require 'LIMIT', but also does not support
# negative LIMIT values. So there is no way to write it that both can
# support
query_args = [self.hs.config.max_mau_value]
safe_guard = self.hs.config.max_mau_value - len(self.reserved_users)
# Must be greater than zero for postgres
safe_guard = safe_guard if safe_guard > 0 else 0
query_args = [safe_guard]

base_sql = """
DELETE FROM monthly_active_users
Expand Down
13 changes: 13 additions & 0 deletions tests/storage/test_monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ def test_initialise_reserved_users(self):
active_count = yield self.store.get_monthly_active_count()
self.assertEquals(active_count, user_num)

# Test that regalar users are removed from the db
ru_count = 2
yield self.store.upsert_monthly_active_user("@ru1:server")
yield self.store.upsert_monthly_active_user("@ru2:server")
active_count = yield self.store.get_monthly_active_count()

self.assertEqual(active_count, user_num + ru_count)
self.hs.config.max_mau_value = user_num
yield self.store.reap_monthly_active_users()

active_count = yield self.store.get_monthly_active_count()
self.assertEquals(active_count, user_num)

@defer.inlineCallbacks
def test_can_insert_and_count_mau(self):
count = yield self.store.get_monthly_active_count()
Expand Down

0 comments on commit 9fd161c

Please sign in to comment.