From 90a14abb0c693287d10c5b2b8a6e5515f3ed4c37 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 4 Jan 2023 11:57:59 +0100 Subject: [feature] HTTP request throttling middleware (#1297) * [feature] Add throttling middleware to AP endpoints * refactor a lil bit * use config setting, start updating docs * doc updates * use relative links in faq doc * small docs fixes * return code 503 instead of 429 when throttled * throttle other endpoints too * simplify token channel prefills --- cmd/gotosocial/action/server/server.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'cmd') diff --git a/cmd/gotosocial/action/server/server.go b/cmd/gotosocial/action/server/server.go index 7a4b315da..70d8cd4dc 100644 --- a/cmd/gotosocial/action/server/server.go +++ b/cmd/gotosocial/action/server/server.go @@ -184,20 +184,29 @@ var Start action.GTSAction = func(ctx context.Context) error { ) // create required middleware + // rate limiting limit := config.GetAdvancedRateLimitRequests() - gzip := middleware.Gzip() // all except fileserver clLimit := middleware.RateLimit(limit) // client api s2sLimit := middleware.RateLimit(limit) // server-to-server (AP) fsLimit := middleware.RateLimit(limit) // fileserver / web templates - // these should be routed in order - authModule.Route(router, clLimit, gzip) - clientModule.Route(router, clLimit, gzip) - fileserverModule.Route(router, fsLimit) - wellKnownModule.Route(router, gzip, s2sLimit) - nodeInfoModule.Route(router, s2sLimit, gzip) - activityPubModule.Route(router, s2sLimit, gzip) - webModule.Route(router, fsLimit, gzip) + // throttling + cpuMultiplier := config.GetAdvancedThrottlingMultiplier() + clThrottle := middleware.Throttle(cpuMultiplier) // client api + s2sThrottle := middleware.Throttle(cpuMultiplier) // server-to-server (AP) + fsThrottle := middleware.Throttle(cpuMultiplier) // fileserver / web templates + + gzip := middleware.Gzip() // applied to all except fileserver + + // these should be routed in order; + // apply throttling *after* rate limiting + authModule.Route(router, clLimit, clThrottle, gzip) + clientModule.Route(router, clLimit, clThrottle, gzip) + fileserverModule.Route(router, fsLimit, fsThrottle) + wellKnownModule.Route(router, gzip, s2sLimit, s2sThrottle) + nodeInfoModule.Route(router, s2sLimit, s2sThrottle, gzip) + activityPubModule.Route(router, s2sLimit, s2sThrottle, gzip) + webModule.Route(router, fsLimit, fsThrottle, gzip) gts, err := gotosocial.NewServer(dbService, router, federator, mediaManager) if err != nil { -- cgit v1.3