-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preserve rate limiters in memory across configuration reloads (#…
…1792) The goal with this change is to preserve the rate limiters across reloads with the least invasive changes possible. To do this I first added a LimiterOptions structure to hold the current set of rate limiters. I then identified each call of tollbooth.New and moved the construction of the limiter without modifications to options.go. I assigned each limiter a distinct field to be referenced during the API object creation. Next I needed to add an optional parameter to the NewAPI methods so I could store the new LimiterOptions onto the API object for reference during route construction. To do this without breaking all existing calls to NewAPI I used the options pattern. This makes the method accept a parametric set of values implementing a common interface, so future problems of similar nature can also be added as options. I then replaced all the local anonymous limiter.Limiters made inline within the API route construction with each corresponding newly added field within the LimiterOptions struct.
- Loading branch information
Showing
6 changed files
with
160 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/didip/tollbooth/v5" | ||
"github.com/didip/tollbooth/v5/limiter" | ||
"github.com/supabase/auth/internal/conf" | ||
) | ||
|
||
type Option interface { | ||
apply(*API) | ||
} | ||
|
||
type LimiterOptions struct { | ||
Email *limiter.Limiter | ||
Phone *limiter.Limiter | ||
Signups *limiter.Limiter | ||
AnonymousSignIns *limiter.Limiter | ||
Recover *limiter.Limiter | ||
Resend *limiter.Limiter | ||
MagicLink *limiter.Limiter | ||
Otp *limiter.Limiter | ||
Token *limiter.Limiter | ||
Verify *limiter.Limiter | ||
User *limiter.Limiter | ||
FactorVerify *limiter.Limiter | ||
FactorChallenge *limiter.Limiter | ||
SSO *limiter.Limiter | ||
SAMLAssertion *limiter.Limiter | ||
} | ||
|
||
func (lo *LimiterOptions) apply(a *API) { a.limiterOpts = lo } | ||
|
||
func NewLimiterOptions(gc *conf.GlobalConfiguration) *LimiterOptions { | ||
o := &LimiterOptions{} | ||
|
||
o.Email = tollbooth.NewLimiter(gc.RateLimitEmailSent/(60*60), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(int(gc.RateLimitEmailSent)).SetMethods([]string{"PUT", "POST"}) | ||
|
||
o.Phone = tollbooth.NewLimiter(gc.RateLimitSmsSent/(60*60), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(int(gc.RateLimitSmsSent)).SetMethods([]string{"PUT", "POST"}) | ||
|
||
o.AnonymousSignIns = tollbooth.NewLimiter(gc.RateLimitAnonymousUsers/(60*60), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(int(gc.RateLimitAnonymousUsers)).SetMethods([]string{"POST"}) | ||
|
||
o.Token = tollbooth.NewLimiter(gc.RateLimitTokenRefresh/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
o.Verify = tollbooth.NewLimiter(gc.RateLimitVerify/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
o.User = tollbooth.NewLimiter(gc.RateLimitOtp/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
o.FactorVerify = tollbooth.NewLimiter(gc.MFA.RateLimitChallengeAndVerify/60, | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Minute, | ||
}).SetBurst(30) | ||
|
||
o.FactorChallenge = tollbooth.NewLimiter(gc.MFA.RateLimitChallengeAndVerify/60, | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Minute, | ||
}).SetBurst(30) | ||
|
||
o.SSO = tollbooth.NewLimiter(gc.RateLimitSso/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
o.SAMLAssertion = tollbooth.NewLimiter(gc.SAML.RateLimitAssertion/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
o.Signups = tollbooth.NewLimiter(gc.RateLimitOtp/(60*5), | ||
&limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
|
||
// These all use the OTP limit per 5 min with 1hour ttl and burst of 30. | ||
o.Recover = newLimiterPer5mOver1h(gc.RateLimitOtp) | ||
o.Resend = newLimiterPer5mOver1h(gc.RateLimitOtp) | ||
o.MagicLink = newLimiterPer5mOver1h(gc.RateLimitOtp) | ||
o.Otp = newLimiterPer5mOver1h(gc.RateLimitOtp) | ||
return o | ||
} | ||
|
||
func newLimiterPer5mOver1h(rate float64) *limiter.Limiter { | ||
freq := rate / (60 * 5) | ||
lim := tollbooth.NewLimiter(freq, &limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}).SetBurst(30) | ||
return lim | ||
} |