bsmratelimit_fasthttp

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2018 License: MIT Imports: 2 Imported by: 0

README

bsm/ratelimit for FastHTTP

What is it?

This is a very simple FastHTTP adapter for github.com/bsm/ratelimit library.

How to use it?

Example:

package main

import (
	"github.com/valyala/fasthttp"
    "github.com/bsm/ratelimit"
	"time"
	"github.com/sviterok/bsmratelimit_fasthttp"
)

func main() {
	requestHandler := func(ctx *fasthttp.RequestCtx) {
		switch string(ctx.Path()) {
		case "/hello":
			helloHandler(ctx)
		default:
			ctx.Error("Unsupported path", fasthttp.StatusNotFound)
		}
	}

	// Create a limiter struct.
	limiter := ratelimit.New(5000, time.Second)
	statusCode := fasthttp.StatusTooManyRequests
	fasthttp.ListenAndServe(":4444", bsmratelimit_fasthttp.SimpleLimitHandler(requestHandler, limiter, statusCode))

}

func helloHandler(ctx *fasthttp.RequestCtx) {
	ctx.SetStatusCode(fasthttp.StatusOK)
	ctx.SetBody([]byte("Hello, World!"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LimitHandler

func LimitHandler(handler fasthttp.RequestHandler, limiter *ratelimit.RateLimiter, onLimitReachedHandler fasthttp.RequestHandler) fasthttp.RequestHandler

LimitHandler is a very simple FastHTTP adapter for `github.com/bsm/ratelimit`. It wraps original request handler with a rate-limited one

func SimpleLimitHandler

func SimpleLimitHandler(handler fasthttp.RequestHandler, limiter *ratelimit.RateLimiter, statusCode int) fasthttp.RequestHandler

SimpleLimitHandler covers the most simple rate limiting case when the server only needs to return a predefined status code.

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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