Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DownloadSpeed ¶
func DownloadSpeed(keyFn KeyFn) *downloadBuilder
Example ¶
Watch the download speed with wget http://localhost:3333/file -q --show-progress
package main
import (
"net/http"
"time"
"github.com/VojtechVitek/ratelimit"
"github.com/VojtechVitek/ratelimit/memory"
)
func main() {
middleware := ratelimit.DownloadSpeed(ratelimit.IP).Rate(1024, time.Second).LimitBy(memory.New())
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/dev/random")
})
http.ListenAndServe(":3333", middleware(handler))
}
Output:
func Request ¶
func Request(keyFn KeyFn) *requestBuilder
Example ¶
package main
import (
"net/http"
"time"
"github.com/VojtechVitek/ratelimit"
"github.com/VojtechVitek/ratelimit/memory"
)
func main() {
middleware := ratelimit.Request(ratelimit.IP).Rate(30, time.Minute).LimitBy(memory.New())
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
http.ListenAndServe(":3333", middleware(handler))
}
Output:
func Throttle ¶
Throttle is a middleware that limits number of currently processed requests at a time.
Example ¶
package main
import (
"net/http"
"time"
"github.com/VojtechVitek/ratelimit"
)
func main() {
middleware := ratelimit.Throttle(1)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("working hard...\n\n"))
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
time.Sleep(10 * time.Second)
w.Write([]byte("done"))
})
http.ListenAndServe(":3333", middleware(handler))
}
Output:
Types ¶
Click to show internal directories.
Click to hide internal directories.