router concurrent limiter

路由限制中间件,可以指定路由的并发访问数量,建议使用NewLocalLimiter每个实例的限制分开,主要是用于避免某个接口并发过高导致系统不稳定。

Example

  1. package main
  2. import (
  3. "bytes"
  4. "time"
  5. "github.com/vicanso/elton"
  6. "github.com/vicanso/elton/middleware"
  7. )
  8. func main() {
  9. e := elton.New()
  10. e.Use(middleware.NewRCL(middleware.RCLConfig{
  11. Limiter: middleware.NewLocalLimiter(map[string]uint32{
  12. "GET /users/me": 2,
  13. }),
  14. }))
  15. e.GET("/users/me", func(c *elton.Context) (err error) {
  16. time.Sleep(time.Second)
  17. c.BodyBuffer = bytes.NewBufferString(`{
  18. "account": "tree",
  19. "name": "tree.xie"
  20. }`)
  21. return nil
  22. })
  23. err := e.ListenAndServe(":3000")
  24. if err != nil {
  25. panic(err)
  26. }
  27. }