Tollbooth HTTP 限速中间件开源项目

我要开发同款
匿名用户2015年05月23日
45阅读
所属分类Google Go、程序开发、网络工具包
授权协议MIT

作品详情

Tollbooth是一个用Go语言编写的用来限制HTTP访问速度的中间件,可用来限制每个HTTP请求的传输速率。例如你可以不限制/的访问速率,但是可以针对/login限制每个IP每秒最多POST多少个请求。

Go程序中使用的方法:

package mainimport (    "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)
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论