Skip to content

Conversation

@MatMaul
Copy link
Contributor

@MatMaul MatMaul commented Dec 9, 2025

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

@MatMaul MatMaul force-pushed the rc-search branch 2 times, most recently from 67eac2f to 151b288 Compare December 9, 2025 13:57
Copy link
Contributor

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

Seems reasonable

@@ -0,0 +1 @@
Add a config to be able to rate limit search in the user directory.
Copy link
Contributor

Choose a reason for hiding this comment

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

What's spawning this change?

self.rc_user_directory = RatelimitSettings.parse(
config,
"rc_user_directory",
defaults={"per_second": 0.016, "burst_count": 50},
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reasoning behind these values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The per_second value should be low enough so that an user can't scrape the user directory too quickly, and the burst is big-ish because you may need to get the next page of results several times, and you may make spelling mistakes to correct.

We may want to put the burst even higher actually, since invites are often issued by batches so an user may want to search for several users in a row.

I could bump it to 200 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

We're going from no rate limit to rate limit so any value is fine to start with (bump it to 200). These are the kind of thoughts I am expecting though 👍

if not self.hs.config.userdirectory.user_directory_search_enabled:
return 200, {"limited": False, "results": []}

await self._per_user_limiter.ratelimit(requester)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to wrap this to return nice errors?

I see we do it here for example:

try:
await self._presence_per_user_limiter.ratelimit(requester)
except LimitExceededError as e:
logger.debug("User presence ratelimit exceeded; ignoring it.")
return 429, {
"errcode": Codes.LIMIT_EXCEEDED,
"error": "Too many requests",
"retry_after_ms": e.retry_after_ms,
}

Copy link
Contributor

@MadLittleMods MadLittleMods Dec 17, 2025

Choose a reason for hiding this comment

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

LimitExceededError is already a SynapseError with 429 so it's probably fine without it. Just calling this out to make sure we've tried this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we would gain mainly the log line. I don't think it's that useful since we can also see the 429 in the request log line, and 429 is pretty well defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants