Documentation ¶
Overview ¶
Example ¶
package main import ( "log" "net/http" "time" "github.com/keratin/throttled-valve/valve" "github.com/throttled/throttled" "github.com/throttled/throttled/store/memstore" ) func main() { store, err := memstore.New(65536) if err != nil { log.Fatal(err) } // Valve will allow: // * 10/minute (1/6 seconds) with an additional burst of 4 // * After two minutes: 2/minute (1/30 seconds) valve := valve.NewValve(store, valve.NewSchedule(4, valve.Entry{Rate: throttled.PerMin(10)}, valve.Entry{Rate: throttled.PerMin(2), Delay: 2 * time.Minute}, )) // valve is compatible with throttled.HTTPRateLimiter loginLimiter := throttled.HTTPRateLimiter{ RateLimiter: valve, VaryBy: &throttled.VaryBy{RemoteAddr: true}, } // you may wish to use a router and only apply the limit on specific endpoints var loginHandler http.HandlerFunc http.ListenAndServe(":8080", loginLimiter.RateLimit(loginHandler)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
Entry is a configuration struct for NewSchedule. It describes a Rate that will only take effect after some Delay in maximally sustained traffic according to the previous rate.
type Schedule ¶
Schedule is configuration struct for NewValve. It comprises multiple RateQuotas that have been calculated by NewSchedule to work together in a constricting fashion.
func NewSchedule ¶
NewSchedule constructs a Schedule from the set of Entries provided. The Entries are converted to throttled.RateQuota structs with burst values calculated to allow the previous rate in the Schedule to be maximally sustained during the delay.
TODO: ensure entries are ordered from most to least permissive
type Valve ¶
type Valve struct {
// contains filtered or unexported fields
}
Valve is a RateLimiter implementation comprising multiple limiters that must each be queried and maintained on every unit of work. It is intended to be used in situations where sustained work is suspicious because it is sustained, so that the sustained rate can be throttled down to a crawl.
Accomplishing this requires querying and maintaining each limiter on every call to RateLimit. A Valve with 3 limiters will take 3x longer to verify, which may be meaningful if checking a limiter requires network I/O to a centralized store.