Documentation ¶
Overview ¶
Package throttle provides functionality to limit the frequency with which code is called
Throttling is of the Trigger() method and depends on the parameters passed (period, trailing).
The period parameter defines how often the throttled code can run. A period of one second means that the throttled code will run at most once per second.
The trailing parameter defines what hapens if Trigger() is called after the throttled code has been started, but before the period is finished. If trailing is false then these triggers are ignored. If trailing is true then the throttled code is executed one more time at the beginning of the next period.
Example with period = time.Second and trailing = false:
Whole seconds after first trigger...|0|0|0|0|1|1|1|1| Trigger() gets called...............|X| |X| | |X| | | Throttled code gets called..........|X| | | | |X| | |
Note that the second trigger had no effect. The third Trigger() caused immediate execution of the throttled code.
Example with period = time.Second and trailing = true:
Whole seconds after first trigger...|0|0|0|0|1|1|1|1| Trigger() gets called...............|X| |X| | |X| | | Throttled code gets called..........|X| | | |X| | | |
Note that the second Trigger() causes the throttled code to get called once the first period is over. The third Trigger() will do the same.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver interface { // Trigger() requests execution of the throttled resource. Trigger() // Stop() stops the throttler. Stop() }
Driver is an interface for requesting execution of the throttled resource and for stopping the throttler.
func Func ¶
Func executes f at most once every period. Stop() must eventually be called on the return value to prevent a leaked go proc.
Example ¶
TODO: fix output
func ExampleNewThrottle_trailing() {
throttle := throttle.NewThrottle(period, true) go func() { for throttle.Next() { fmt.Println("hello trailing") } }() go func() { for i := 0; i < 5; i++ { throttle.Trigger() time.Sleep(period / 4) } }() time.Sleep(2 * period) throttle.Stop() // Output: hello trailing // hello trailing }
package main import ( "fmt" "time" "github.com/go-phorce/dolly/algorithms/throttle" ) const period = 600 * time.Millisecond func main() { throttle := throttle.Func(period, false, func() { fmt.Println("fun, throttled.") }) go func() { for i := 0; i < 5; i++ { throttle.Trigger() time.Sleep(period / 6) } }() time.Sleep(2 * period) throttle.Stop() }
Output: fun, throttled.
type Throttle ¶
type Throttle interface { Driver // Next() returns true at most once per `period`. If false is returned the throttler has been stoped. Next() bool }
Throttle extends ThrottleDriver with Next(), which is used by the client to throttle its code.
func NewThrottle ¶
NewThrottle returns a new Throttle. If trailing is true then a multiple Trigger() calls in one period will cause a delayed Trigger() to be called in the next period.
Example (Untrailing) ¶
package main import ( "fmt" "time" "github.com/go-phorce/dolly/algorithms/throttle" ) const period = 600 * time.Millisecond func main() { throttle := throttle.NewThrottle(period, false) go func() { for throttle.Next() { fmt.Println("hello not trailing") } }() go func() { for i := 0; i < 5; i++ { throttle.Trigger() time.Sleep(period / 6) } }() time.Sleep(2 * period) throttle.Stop() }
Output: hello not trailing