feat(auth): implement decoupled sliding-window rate limiting for login and oauth

This commit is contained in:
ryan
2026-09-02 22:15:21 +08:00
parent 39f02b5d7a
commit 9632604958
23 changed files with 927 additions and 50 deletions
@@ -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)
}