throttle

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

throttle

https://github.com/boz/go-throttle

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.

Usage

Throttling execution of a function:

throttle := throttle.ThrottleFunc(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.

Throttling arbitrary code:

package cache

import (
	"time"
	"github.com/boz/go-throttle"
)

type CacheRebuilder struct {
	throttle throttle.Throttle
}

// Create a cache rebuilder which will rebuild the cache at most once every 5 minutes, regardless
// of how often a rebuild is requested.
func NewRebuilder() *CacheRebuilder {
	cr := &CacheRebuilder{NewThrottle(5*time.Minute, true)}

	go func() {
		for cr.throttle.Next() {
			cr.doRebuild()
		}
	}()

	return cr
}

func (cr *CacheRebuilder) Stop() {
	cr.throttle.Stop()
}

func (cr *CacheRebuilder) Rebuild() {
	cr.throttle.Trigger()
}

func (cr *CacheRebuilder) doRebuild() {
	// actually rebuild the cache.
}

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 Func(period time.Duration, trailing bool, f func()) Driver

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

func NewThrottle(period time.Duration, trailing bool) Throttle

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL