Back to

Package gin

Latest Go to latest

The highest tagged major version is .

Published: Aug 27, 2019 | License: BSD-3-Clause | Module:


func DefaultKeyGetter

func DefaultKeyGetter(c *gin.Context) (string, bool)

DefaultKeyGetter - returns the Client IP

func DefaultMetricIncrementer

func DefaultMetricIncrementer(c *gin.Context, l RateLimiter, metric *prometheus.CounterVec)

DefaultMetricIncrementer - increment the metric when a rate is exceeded

func DefaultRateExceededReporter

func DefaultRateExceededReporter(c *gin.Context, l RateLimiter, span opentracing.Span, doTracing bool) string

DefaultRateExceededReporter - handle all the reporting/logging/tagging for rate limiting

func LimitRoute

func LimitRoute(limiters []RateLimiter, handle gin.HandlerFunc) gin.HandlerFunc

LimitRoute - Decorator

func NewDefaultMetric

func NewDefaultMetric(subsystem string, name string, help string) *prometheus.CounterVec

NewDefaultMetric - define what a metric is

type ErrorResponse

type ErrorResponse struct {
	Code      int    `json:"code" binding:"required"`
	Subcode   int    `json:"subcode" binding:"required"`
	Message   string `json:"message" binding:"required"`
	RequestID string `json:"requestid" binding:"required"`

ErrorResponse - define the structure of errors sent to consumers

type KeyGetter

type KeyGetter func(c *gin.Context) (string, bool)

KeyGetter will define the rate limiter key given the gin Context

type MetricIncrementer

type MetricIncrementer func(c *gin.Context, l RateLimiter, metric *prometheus.CounterVec)

MetricIncrementer - will define how the metric for the limiter is incremented (and deal with labels)

type RateExceededReporter

type RateExceededReporter func(c *gin.Context, l RateLimiter, span opentracing.Span, doTracing bool) string

RateExceededReporter - will define what/how things will get reported when rates are exceeded

type RateLimiter

type RateLimiter struct {
	LimiterGUID      string                  // GUID for the limiter, since routes can have multiple limiters, this ensures the key is uniq - typically should be method + route, example == GET::/hello
	Limiter          *golimiter.Limiter      // The limiter
	Store            *golimiter.LimiterStore // Store for the limiter (* may include a central store of redis)
	KeyGetter        KeyGetter               // Func that gets the Key for the request (default is the clientIP)
	Reporter         RateExceededReporter    // func that logs/traces/etc when the rate is exceeded
	Type             string                  // the type of rate limiter (user, product, IP, etc)
	Behavior         string                  // the string you want to send back to the client for the behavior you'd prefer from the retry logic
	Rate             string                  // the Rate of the limiter
	ErrorCode        int                     // the primary error code returned when the rate is exceeded
	ErrorSubCode     int                     // the secondary error code returned when the rate is exceeded
	MetricLabel      string                  // prometheus label for the type of limiter to increment (user, IP, product, etc)
	Metric           *prometheus.CounterVec  // prometheus metric for observing exceeded rates
	MetricIncremeter MetricIncrementer       // func called to increment the prometheus metric with the required labels

RateLimiter - define a rate limiter type

func NewLimiter

func NewLimiter(
	limiterGUID string,
	rateStr string,
	rateType string,
	keyGetter KeyGetter,
	reporter RateExceededReporter,
	maxEntriesInMemory int,
	behavior string,
	errorCode int,
	errorSubCode int,
	metricLabel string,
	metric *prometheus.CounterVec,
	metricIncrementer MetricIncrementer,
	opts ...golimiter.Option) (l RateLimiter, err error)

NewLimiter - create a new one

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier