fiber

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	WithLimiter = func(limiter rate.Limiter) OptionFiber {
		return func(o *optionFiber) {
			o.limiter = limiter
		}
	}
	WithConfig = func(config Config) OptionFiber {
		return func(o *optionFiber) {
			o.config = config
		}
	}
)
View Source
var DefaultConfig = Config{
	CommonConfig: middlewares.DefaultCommonConfig,

	ErrHandler: func(ctx *sysfiber.Ctx, conf *Config, err error) error {
		middlewares.ErrorThrottleHandler(
			conf.Logger,
			middlewares.Client{
				IP:   ctx.IP(),
				Path: ctx.Path(),
			},
			err,
			time.Now(),
		)
		ctx.Response().Header.Add(middlewares.HeaderRetryAfter, middlewares.DefaultRetryAfter)
		return ctx.SendStatus(http.StatusTooManyRequests)
	},

	LimitHandler: func(c *sysfiber.Ctx, conf *Config, now time.Time) error {
		middlewares.LimitHandler(
			conf.Logger,
			middlewares.Client{
				IP:   c.IP(),
				Path: c.Path(),
			},
			now,
		)

		var wtrStr string = middlewares.DefaultRetryAfter
		wtr := time.Since(now).Round(time.Second)
		if wtr > time.Second {
			wtrStr = strconv.Itoa(2 * int(wtr.Seconds()))
		}
		c.Response().Header.Set(middlewares.HeaderRetryAfter, wtrStr)

		return c.SendStatus(http.StatusTooManyRequests)
	},
}

Functions

func RateLimit added in v0.1.2

func RateLimit(ops ...OptionFiber) sysfiber.Handler

RateLimitWithConfig echo middleware with custom conf

Types

type Config

type Config struct {
	middlewares.CommonConfig

	ErrHandler func(*sysfiber.Ctx, *Config, error) error

	LimitHandler func(*sysfiber.Ctx, *Config, time.Time) error

	// может быть nil
	Skip func(*sysfiber.Ctx) bool
}

type OptionFiber added in v0.1.2

type OptionFiber func(*optionFiber)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL