Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

vicanso/elton-concurrent-limiter

Repository files navigation

elton-concurrent-limiter

The middleware has been archived, please use the middleware of elton.

Build Status

Concurrent limiter for elton. It support to get lock value from five ways. Client IP, QueryString, Request Header, Route Params and Post Body.

  • IP The key's name is :ip
  • QueryString The key's name has prefix q:
  • Request Header The key's name has prefix h:
  • Route Params The key's name has prefix p:
  • Post Body The 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'