The middleware has been archived, please use the middleware of elton.
Concurrent limiter for elton. It support to get lock value from five ways. Client IP, QueryString, Request Header, Route Params and Post Body.
IPThe key's name is:ipQueryStringThe key's name has prefixq:Request HeaderThe key's name has prefixh:Route ParamsThe key's name has prefixp:Post BodyThe other's key
package main
import (
"bytes"
"sync"
"time"
"github.com/vicanso/elton"
concurrentLimiter "github.com/vicanso/elton-concurrent-limiter"
)
func main() {
e := elton.New()
m := new(sync.Map)
limit := concurrentLimiter.New(concurrentLimiter.Config{
Keys: []string{
":ip",
"h:X-Token",
"q:type",
"p:id",
"account",
},
Lock: func(key string, c *elton.Context) (success bool, unlock func(), err error) {
_, loaded := m.LoadOrStore(key, true)
// the key not exists
if !loaded {
success = true
unlock = func() {
m.Delete(key)
}
}
return
},
})
e.POST("/login", limit, func(c *elton.Context) (err error) {
time.Sleep(3 * time.Second)
c.BodyBuffer = bytes.NewBufferString("hello world")
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
})
}curl -XPOST 'http://127.0.0.1:7001/login'