mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 15:06:36 +08:00
feat(auth): implement decoupled sliding-window rate limiting for login and oauth
This commit is contained in:
@@ -7,6 +7,7 @@ package cache_memory
|
||||
import (
|
||||
"Wavelet/core"
|
||||
"Wavelet/core/contracts"
|
||||
"Wavelet/pkg/limiter"
|
||||
)
|
||||
|
||||
const defaultRAMCapacity = 10000
|
||||
@@ -79,5 +80,6 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
||||
}
|
||||
|
||||
core.Provide[contracts.CacheService](ctx, svc)
|
||||
core.Provide[contracts.LimiterService](ctx, limiter.NewMemoryLimiter())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -78,4 +78,14 @@ func TestCacheMemoryPlugin(t *testing.T) {
|
||||
var tempVal string
|
||||
err = cacheSvc.Get(reqCtx, "temp_key", &tempVal)
|
||||
assert.ErrorIs(t, err, contracts.ErrCacheMiss)
|
||||
|
||||
// 6. LimiterService
|
||||
limiterSvc, err := core.Inject[contracts.LimiterService](ctx)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, limiterSvc)
|
||||
|
||||
rateRes, err := limiterSvc.Allow(reqCtx, "key_a", contracts.Rate{Limit: 2, Period: time.Minute})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, rateRes.Allowed)
|
||||
assert.Equal(t, 1, rateRes.Remaining)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user