fiatjaf / relayer

A Nostr relay server framework.
The Unlicense
309 stars 84 forks source link

feat: basic per-connection soft rate limiter #81

Closed bndw closed 1 year ago

bndw commented 1 year ago

Adds an optional per-connection token bucket rate limiter. When a connection exceeds the configured rate limit, the limiter will wait until a token is available. This limiter is transparent to clients and will simply throttle incoming messages to protect the relay from being overwhelmed by a single connection. This patch is currently running on the stemstr relay.

Usage:

-   server, err := relayer.NewServer(r)
+   opts := []relayer.Option{
+       relayer.WithPerConnectionLimiter(rate.Every(time.Second), 1),
+   }
+
+   server, err := relayer.NewServer(r, opts...)

It may also make sense to add another limiter option for a hard rate limiter that will close connections.

Closes #80

bndw commented 1 year ago

@fiatjaf was this intentionally deleted from master? Here's what I have locally...

| * 3660300 - (upstream/master) close channels (2 days ago) <Yasuhiro Matsumoto>
* | 9395f33 - (tag: v2.1.2) fix: do not delete old notes by default (5 days ago) <bndw>
* | 0b93ce6 - fix nip42 handling (#82) (5 days ago) <anon>
* | 78d8b56 - feat: basic per-connection rate limiter (#1) (7 days ago) <Ben Woodward>
* | 7411f3d - Fix port (13 days ago) <cm>
* | 174a162 - Update Dockerfile (2 weeks ago) <CM>
|/
* e4fe82d - (tag: v2.1.0) keep up with the latest specs for since/until filter (4 weeks ago) <jiftechnify>
* fb3626f - fix typo (4 weeks ago) <Yasuhiro Matsumoto>
fiatjaf commented 1 year ago

No, it was unintentional. I ended up force pushing a completely different commit to this repo, then I tried to recover by restoring the last PR I found, but I made a mistake and didn't realize this one was missing until now. I'm very sorry.

fiatjaf commented 1 year ago

Pushed now as 1935f62c29c002f250235361ad77f4fb3ed17b04.

bndw commented 1 year ago

I think you're still missing a few commits, ie the fix for not deleting events I submitted recently.

Here is what I have on my fork. Note I'm one commit ahead, testing an unrelated bugfix:

| * 3660300 - (upstream/master) close channels (2 days ago) <Yasuhiro Matsumoto>
* | 9395f33 - (tag: v2.1.2) fix: do not delete old notes by default (5 days ago) <bndw>
* | 0b93ce6 - fix nip42 handling (#82) (5 days ago) <anon>
* | 78d8b56 - feat: basic per-connection rate limiter (#1) (7 days ago) <Ben Woodward>
* | 7411f3d - Fix port (13 days ago) <cm>
* | 174a162 - Update Dockerfile (2 weeks ago) <CM>
|/
* e4fe82d - (tag: v2.1.0) keep up with the latest specs for since/until filter (4 weeks ago) <jiftechnify>
* fb3626f - fix typo (4 weeks ago) <Yasuhiro Matsumoto>
bndw commented 1 year ago

@fiatjaf ^ 😬