Skip to content

使用golang通道实现的线程安全的令牌桶限流器

License

Notifications You must be signed in to change notification settings

liangwt/token-bucket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

简单令牌桶实现限流

Principle:

Feature:

  • 使用带缓冲通道作为桶实现
  • 使用定时器触发令牌发放
  • 线程安全可实现多并发

Example:

package main

import (
	"github.com/liangwt/token-bucket"
	"log"
	"time"
)

func main() {
	bucket := bucket.New(10, time.Second, 100, true)
	isLimit := bucket.Consume(100)
	log.Println(isLimit)

	time.Sleep(2 * time.Second)
	isLimit = bucket.Consume(10)
	log.Println(isLimit)
}

About

使用golang通道实现的线程安全的令牌桶限流器

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages