【文件属性】:
文件名称:HTTP限速中间件Tollbooth.zip
文件大小:43KB
文件格式:ZIP
更新时间:2022-08-05 14:32:41
开源项目
Tollbooth 是一个用 Go 语言编写的用来限制 HTTP 访问速度的中间件,可用来限制每个 HTTP 请求的传输速率。例如你可以不限制 / 的访问速率,但是可以针对 /login 限制每个 IP 每秒最多 POST 多少个请求。Go 程序中使用的方法:package main
import (
"github.com/didip/tollbooth"
"net/http"
"time"
)
func HelloHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, World!"))
}
func main() {
// You can create a generic limiter for all your handlers
// or one for each handler. Your choice.
// This limiter basically says: allow at most 1 request per 1 second.
limiter := tollbooth.NewLimiter(1, time.Second)
// This is an example on how to limit only GET and POST requests.
limiter.Methods = []string{"GET", "POST"}
// You can also limit by specific request headers, containing certain values.
// Typically, you prefetched these values from the database.
limiter.Headers = make(map[string][]string)
limiter.Headers["X-Access-Token"] = []string{"abc123", "xyz098"}
// And finally, you can limit access based on basic auth usernames.
// Typically, you prefetched these values from the database as well.
limiter.BasicAuthUsers = []string{"bob", "joe", "didip"}
// Example on how to wrap your request handler.
http.Handle("/", tollbooth.LimitFuncHandler(limiter, HelloHandler))
http.ListenAndServe(":12345", nil)
标签:Tollbooth
【文件预览】:
tollbooth-master
----limiter()
--------limiter.go(12KB)
--------limiter_options.go(179B)
--------limiter_test.go(4KB)
--------limiter_benchmark_test.go(454B)
--------limiter_setter_getter_test.go(4KB)
----tollbooth_test.go(10KB)
----tollbooth_bug_report_test.go(4KB)
----LICENSE(1KB)
----README.md(6KB)
----errors()
--------errors_test.go(190B)
--------errors.go(370B)
----.gitignore(16B)
----libstring()
--------libstring.go(1KB)
--------libstring_test.go(3KB)
----tollbooth.go(7KB)
----tollbooth_benchmark_test.go(1KB)
----vendor()
--------github.com()
--------golang.org()
--------vendor.json(701B)
网友评论
- 别下载,是源代码,还要19分,github都有