项目作者: iowar

项目描述 :
Golang dynamic throttle
高级语言: Go
项目地址: git://github.com/iowar/throttle.git
创建时间: 2019-04-02T20:46:16Z
项目社区:https://github.com/iowar/throttle

开源协议:GNU General Public License v3.0

下载


Golang Dynamic Throttle

GoDoc

In some cases it may be necessary to change the throttle at run time.
The ‘Update’ channel in throttle can be used to provide triggering like time.Ticker.

Installation

  1. $ go get -u github.com/iowar/throttle

Usage

Simple usage example:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/iowar/throttle"
  6. )
  7. var (
  8. kill = make(chan struct{})
  9. )
  10. func main() {
  11. th := throttle.NewThrottle(time.Second)
  12. th.Start()
  13. go func() {
  14. time.Sleep(time.Second * 6)
  15. th.ChangeInterval(time.Second * 3)
  16. fmt.Println("Changing interval.")
  17. }()
  18. go func() {
  19. time.Sleep(time.Second * 15)
  20. th.Stop()
  21. fmt.Println("Stopping throttle.")
  22. }()
  23. go func() {
  24. time.Sleep(time.Second * 24)
  25. th.Start()
  26. fmt.Println("Starting throttle.")
  27. }()
  28. go func() {
  29. time.Sleep(time.Second * 33)
  30. kill <- struct{}{}
  31. fmt.Println("Finish.")
  32. }()
  33. go func() {
  34. for {
  35. <-th.Update
  36. fmt.Println(time.Now())
  37. }
  38. }()
  39. <-kill
  40. }