Skip to content

Fix/rate limiter - #21

Merged
codetheuri merged 3 commits into
mainfrom
fix/rate-limiter
Oct 27, 2026
Merged

codetheuri merged 3 commits into
mainfrom
fix/rate-limiter

Conversation

@codetheuri

Copy link
Copy Markdown
Owner

No description provided.

ConnectWith migrated first and only then took the advisory lock that
serialises test packages. go test runs packages as separate processes
against one database, so the first run after a new migration raced: two
packages applied it at once and one failed with "column ... already
exists".

The lock is now taken before the migration, and every failure path after
it releases the lock before failing the test.

Verified by recreating the test database and running four database
packages at once with -p 4.
The request logger wrapped the ResponseWriter without Unwrap, so
http.ResponseController could not see through it. Every handler behind it
(all routes, since app.New installs it) was stuck with the server's
WriteTimeout and could not flush. Salio's APK downloads, which take half an
hour on a slow connection, would be cut at 60 seconds.

Unwrap returns the underlying writer. A test checks that SetWriteDeadline and
Flush work through the logger; both failed before.
It keyed on RemoteAddr with its port, new on every connection, and on
X-Forwarded-For from anyone, which a client sets freely: each request
could arrive as a new caller with a full bucket. A caller is now its
address without the port (IPv6 by /64); X-Forwarded-For is read only
from TRUSTED_PROXIES, right to left past trusted hops; the table is
capped. Adds ClientIP, ParseTrustedProxies and RateLimiter.Allow(key).

BREAKING CHANGE: behind a reverse proxy, set TRUSTED_PROXIES, or every
client shares the proxy's allowance.
@codetheuri
codetheuri merged commit db642b6 into main Oct 27, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant