mirror of https://github.com/gofiber/fiber.git
📦 Change default configuration and add test for custom store
parent
23bddfd23f
commit
3308a32a3a
|
@ -63,7 +63,6 @@ var ConfigDefault = Config{
|
|||
LimitReached: func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusTooManyRequests)
|
||||
},
|
||||
Store: &defaultStore{stmap: map[string][]byte{}},
|
||||
}
|
||||
|
||||
// trackedSession is the type used for session tracking
|
||||
|
@ -104,9 +103,7 @@ func New(config ...Config) fiber.Handler {
|
|||
if cfg.LimitReached == nil {
|
||||
cfg.LimitReached = ConfigDefault.LimitReached
|
||||
}
|
||||
if cfg.Store == nil {
|
||||
cfg.Store = ConfigDefault.Store
|
||||
} else {
|
||||
if cfg.Store != nil {
|
||||
cfg.usingCustomStore = true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,36 @@ func Test_Limiter_Concurrency(t *testing.T) {
|
|||
resp, err = app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
app = fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Max: 50,
|
||||
Duration: 2 * time.Second,
|
||||
Store: defaultStore{stmap: map[string][]byte{}},
|
||||
}))
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello tester!")
|
||||
})
|
||||
|
||||
for i := 0; i <= 49; i++ {
|
||||
wg.Add(1)
|
||||
go singleRequest(&wg)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, 429, resp.StatusCode)
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_Limiter -benchmem -count=4
|
||||
|
|
|
@ -23,7 +23,7 @@ type defaultStore struct {
|
|||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func (s *defaultStore) Get(id string) ([]byte, error) {
|
||||
func (s defaultStore) Get(id string) ([]byte, error) {
|
||||
s.mutex.Lock()
|
||||
val, ok := s.stmap[id]
|
||||
s.mutex.Unlock()
|
||||
|
@ -34,7 +34,7 @@ func (s *defaultStore) Get(id string) ([]byte, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *defaultStore) Set(id string, val []byte, _ time.Duration) error {
|
||||
func (s defaultStore) Set(id string, val []byte, _ time.Duration) error {
|
||||
s.mutex.Lock()
|
||||
s.stmap[id] = val
|
||||
s.mutex.Unlock()
|
||||
|
@ -42,7 +42,7 @@ func (s *defaultStore) Set(id string, val []byte, _ time.Duration) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *defaultStore) Clear() error {
|
||||
func (s defaultStore) Clear() error {
|
||||
s.mutex.Lock()
|
||||
s.stmap = map[string][]byte{}
|
||||
s.mutex.Unlock()
|
||||
|
@ -50,7 +50,7 @@ func (s *defaultStore) Clear() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *defaultStore) Delete(id string) error {
|
||||
func (s defaultStore) Delete(id string) error {
|
||||
s.mutex.Lock()
|
||||
_, ok := s.stmap[id]
|
||||
if ok {
|
||||
|
|
Loading…
Reference in New Issue