You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
802 B
28 lines
802 B
package rate_test |
|
|
|
import ( |
|
"go-common/library/net/http/blademaster" |
|
"go-common/library/net/http/blademaster/middleware/rate" |
|
) |
|
|
|
// This example create a rate middleware instance and attach to a blademaster engine, |
|
// it will protect '/ping' API frequency with specified policy. |
|
// If any internal service who requests this API more frequently than 1 req/second, |
|
// a StatusTooManyRequests error will be raised. |
|
func Example() { |
|
lim := rate.New(&rate.Config{ |
|
URLs: map[string]*rate.Limit{ |
|
"/ping": &rate.Limit{Limit: 1, Burst: 2}, |
|
}, |
|
Apps: map[string]*rate.Limit{ |
|
"a-secret-app-key": &rate.Limit{Limit: 1, Burst: 2}, |
|
}, |
|
}) |
|
|
|
engine := blademaster.Default() |
|
engine.Use(lim) |
|
engine.GET("/ping", func(c *blademaster.Context) { |
|
c.String(200, "%s", "pong") |
|
}) |
|
engine.Run(":18080") |
|
}
|
|
|