Ejemplo n.º 1
0
func main() {
	limit5 := throttler.Limit(5)
	limit2 := throttler.Limit(2)

	handlerFunc := http.HandlerFunc(handler)
	http.Handle("/limit/5", limit5(handlerFunc))
	http.Handle("/limit/2", limit2(handlerFunc))

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/limit/5 &); done\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/limit/2 &); done\n\n")

	log.Fatal(http.ListenAndServe(":8000", nil))
}
Ejemplo n.º 2
0
func main() {
	// Limit to 5 requests globally.
	goji.Use(throttler.Limit(5))

	// Limit /admin route to 2 requests.
	admin := web.New()
	admin.Use(throttler.Limit(2))
	admin.Get("/*", handler)

	goji.Handle("/admin/*", admin)
	goji.Get("/*", handler)

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/admin/ &); done\n\n")

	goji.Serve()
}
Ejemplo n.º 3
0
func main() {
	// Limit to 5 requests globally.
	handlerFunc := http.HandlerFunc(handler)
	chain := alice.New(throttler.Limit(5)).Then(handlerFunc)

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")

	http.ListenAndServe(":8000", chain)
}
Ejemplo n.º 4
0
func main() {
	r := chi.NewRouter()

	// Limit to 5 requests globally.
	r.Use(throttler.Limit(5))
	r.Get("/*", handler)

	admin := chi.NewRouter()
	// Limit to 2 requests for admin route
	admin.Use(throttler.Limit(2))
	admin.Get("/", handler)

	r.Mount("/admin/", admin)

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/admin/ &); done\n\n")

	http.ListenAndServe(":8000", r)
}
Ejemplo n.º 5
0
func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", handler)

	// Limit to 5 requests globally.
	limit := throttler.Limit(5)
	http.Handle("/", limit(r))

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")

	log.Fatal(http.ListenAndServe(":8000", nil))
}
Ejemplo n.º 6
0
func main() {
	// Limit to 5 requests globally.
	middle := interpose.New()
	middle.Use(throttler.Limit(2))

	router := mux.NewRouter()
	router.HandleFunc("/", handler)
	middle.UseHandler(router)

	fmt.Printf("Try running the following commands (in different terminal):\n\n")
	fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")

	http.ListenAndServe(":8000", middle)
}