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

Initial impl of capping MAU#3630

Merged
neilisfragile merged 19 commits into
developfrom
neilj/mau_sign_in_log_in_limits
Aug 1, 2018
Merged

Initial impl of capping MAU#3630
neilisfragile merged 19 commits into
developfrom
neilj/mau_sign_in_log_in_limits

Conversation

@neilisfragile
Copy link
Copy Markdown
Contributor

@neilisfragile neilisfragile commented Jul 31, 2018

Tracks Mau naiving by count(*) on user_ips.
Blocks registration and sign in if value exceeded
Publishes metrics for consumption by Prometheus

The idea here is to cap the monthly active users (MAU) on a server. This is an initial cut that simply stops registration and login for if MAU is above configured threshold - @erikjohnston

Comment thread synapse/handlers/auth.py Outdated
log in.
"""
if self.hs.config.limit_usage_by_mau is True:
current_mau = self.store.count_monthly_users()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method will be hit very often. So you might want to either add some cache on store.count_monthly_users() or use a value which might be generated by generate_monthly_active_users

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed though deliberately omitted since count_monthly_users() is really only a stepping stone method to get the rest of the infra out. In current form only called regularly in the case where the config is enabled.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. If that is on your radar I am fine with it for now

action="join",
)

def _check_mau_limits(self):
Copy link
Copy Markdown
Contributor

@krombel krombel Jul 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to deduplicate this somehow

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, and also the calculating the total count is duped with the daily count - but likley to change structure

@erikjohnston erikjohnston changed the title Neilj/mau sign in log in limits Initial impl of capping MAU Jul 31, 2018
Comment thread synapse/app/homeserver.py Outdated
max_mau_value_gauge = Gauge("synapse_admin_max_mau_value", "MAU Limit")
limit_usage_by_mau_gauge = Gauge(
"synapse_admin_limit_usage_by_mau", "MAU Limiting enabled"
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not bother having limit_usage_by_mau_gauge, and simply set it to 0 or null if limit isn't enabled

Comment thread synapse/app/homeserver.py Outdated
limit_usage_by_mau_gauge.set(float(hs.config.limit_usage_by_mau))

generate_monthly_active_users()
clock.looping_call(generate_monthly_active_users, 5 * 60 * 1000)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense for this to be done if hs.config.limit_usage_by_mau is false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right yes, need to be called once but the looping is a waste

Comment thread synapse/storage/__init__.py Outdated
count, = txn.fetchone()
return count
finally:
txn.close()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should either accept a txn argument or use runInteraction, otherwise its going to be too easy to call this on the main thread (which is bad as this method blocks)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally trying to keep things simple without deferring - will make the change

Comment thread synapse/app/homeserver.py Outdated
def generate_monthly_active_users():
count = 0
if hs.config.limit_usage_by_mau:
count = hs.get_datastore().count_monthly_users()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count_monthly_users will need to be asynchronous and return a deferred, which we'll need to yield

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed as above

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a yield

@neilisfragile
Copy link
Copy Markdown
Contributor Author

review items implemented

This was referenced Aug 1, 2018
Copy link
Copy Markdown
Member

@erikjohnston erikjohnston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just needs to have the yield added, and then its good to go

Comment thread changelog.d/3630.feature Outdated
@@ -0,0 +1 @@
Blocks registration and sign in if max mau value exceeded
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, can we quickly change this to "Add ability to limit number of monthly active users on the server", or some such. Otherwise its going to be confusing for people to read the changelog who haven't had the full context.

Comment thread synapse/app/homeserver.py Outdated
def generate_monthly_active_users():
count = 0
if hs.config.limit_usage_by_mau:
count = hs.get_datastore().count_monthly_users()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a yield

Comment thread synapse/storage/__init__.py Outdated
This method should be refactored with count_daily_users - the only
reason not to is waiting on definition of mau
returns:
defered: resolves to int
Copy link
Copy Markdown
Member

@erikjohnston erikjohnston Aug 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, we generally start the docstring on the sameline of the quotes, so something like:

"""Counts the number of users who used this homeserver in the last 30 days.

This method should be refactored with count_daily_users - the only
reason not to is waiting on definition of MAU.

Returns:
    Deferred[int]
"""

And that's also how you annotate return types (such that IDEs correctly interpret it)

@neilisfragile neilisfragile merged commit 085435e into develop Aug 1, 2018
@neilisfragile neilisfragile deleted the neilj/mau_sign_in_log_in_limits branch August 1, 2018 15:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants