• Stars
    star
    311
  • Rank 134,521 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

基于令牌桶算法和漏桶算法来实现的限速限流,Golang实现。

ratelimit Build Status Documentation Go Report Card Coverage Status

基于令牌桶算法和漏桶算法来实现的限速限流,Golang实现。

Stargazers over time

Stargazers over time

算法介绍

漏桶算法

漏桶算法思路很简单,水(请求)先进入到漏桶里,漏桶以一定的速度出水,当水流入速度过大会直接溢出,可以看出漏桶算法能强行限制数据的传输速率。

leaky-bucket

漏桶算法示意图

漏桶算法可以很好地限制容量池的大小,从而防止流量暴增。

令牌桶算法

令牌桶算法的原理是系统会以一个恒定的速度往桶里放入令牌,而如果请求需要被处理,则需要先从桶里获取一个令牌,当桶里没有令牌可取时,则拒绝服务。

token-bucket

令牌桶算法示意图

令牌桶算法通过发放令牌,根据令牌的rate频率做请求频率限制,容量限制等。

示例

package main

import (
	"log"
	"time"

	"github.com/yangwenmai/ratelimit/leakybucket"
	"github.com/yangwenmai/ratelimit/simpleratelimit"
)

func main() {
    // rate limit: simple
	rl := simpleratelimit.New(10, time.Second)

	for i := 0; i < 100; i++ {
		log.Printf("limit result: %v\n", rl.Limit())
	}
	log.Printf("limit result: %v\n", rl.Limit())

    // rate limit: leaky-bucket
	lb := leakybucket.New()
	b, err := lb.Create("leaky_bucket", 10, time.Second)
	if err != nil {
		log.Println(err)
	}
	log.Printf("bucket capacity:%v", b.Capacity())

    // rate limit: token-bucket
}

参考资料

  1. 限流:漏桶算法和令牌桶算法
  2. 维基百科:Token_bucket
  3. 维基百科:Leaky_bucket
  4. 接口限流实践
  5. 流量调整和限流技术

More Repositories

1

learning-golang

Go 学习之路:Go 开发者博客、Go 微信公众号、Go 学习资料(文档、书籍、视频)
Go
2,677
star
2

learning-rust

Rust 学习之路 > Rust Programming Tutorial, include articles, interview, example, problems.
Rust
577
star
3

github-trending-backup

Github trending backup by everyday.
Go
325
star
4

learning-growth

「一叶知秋」集散地,主要是我的一些阅读、学习、社交、研究、思考、放松娱乐记录整理。
99
star
5

how-to-add-badge-in-github-readme

How to add badge (eg: Travis-CI & GoReportCard & Coveralls & Codecov ) in Github README.
Go
99
star
6

learning-kubernetes

Learning Kubernetes ServiceMesh Istio
Shell
69
star
7

learning-english

英语学习,资料来源英语流利说,英语平台:medium、kubernetes 等各种技术网站、博客等。该项目即将 Archive,请移步 https://github.com/yangwenmai/learning-growth
42
star
8

learning-linux

Linux 攻坚计划
26
star
9

learning-algorithms

💻✅数据结构与算法学习,LeetCode 题
Go
24
star
10

go-in-action

《Go In Action》代码笔记,练习题,项目源码分析,改进代码等。
Go
22
star
11

GifImageViewEx

a simple for android gif ImageView base on ImageViewEx。android解析本地或者URL的gif图片或者jpg、png等图片,基于ImageViewEx。
Java
18
star
12

pg

研发团队的工程效率实践-《如何通过Gitlab+七牛云存储来构建在线产品需求文档》
JavaScript
17
star
13

shell-scripts

my docker docker-compose kubernetes shell start/stop/delete/cleanup ... scripts
Shell
16
star
14

examples

一些 examples 集合(包括 gRPC, Kubernetes, opentracing, Drone, Python 等)
Go
11
star
15

awesome-papers

Awesome papers in reading list.
10
star
16

weekly_digest

Weekly digest on your GitHub repository in golang 📆
Go
8
star
17

jaeger-opentracing-examples

A Distributed Tracing System examples
Go
8
star
18

learning-javascript

learning JavaScript
8
star
19

gitfeed

A command-line for displaying NewsFeed using GitHub public API in Go
Go
6
star
20

telegram-bot-go

telegram bot using go
Go
6
star
21

goAlgorithm

some algorithm in golang
Go
5
star
22

Douyu

一种新颖的将编译器、HTTP服务器、MVC框架、ORM框架有效结合的Java开发平台
Java
5
star
23

night-reading-go-bitbar-plugin

Go 夜读 BitBar 插件
Go
5
star
24

gpt

Create a new project based on how-to-add-badge-in-github-readme.
Go
4
star
25

talkgo-algorithms

TalkGo 算法之美题解
Go
4
star
26

learning-blockchain

Learning Blockchain - Any time, any place, just do it!
3
star
27

maiyang.me

茶歇驿站
HTML
3
star
28

learning-qa

Learning QA - 学习测试技术技能,实践交流分享、总结
3
star
29

algocasts-go

Solutions to the problems at algocasts.io in Go
Go
2
star
30

iloveyou

iloveyou
HTML
2
star
31

maitodo

a simple todo application base on nodejs express mongodb.一个基于nodejs、express、mongodb的简单todo应用。
JavaScript
1
star
32

wg

用 Go 写的一个简单的命令行天气小助手
Go
1
star
33

github-workflow

Github Workflow
1
star
34

hackathon2022-plugin-server

PingCAP Hackathon 2022 plugin server(include http and rpc)
Go
1
star
35

mahonia

Automatically exported from code.google.com/p/mahonia
Go
1
star
36

log

a logging for Go.
1
star
37

content-analyzer

Video Audio Text Content Analyzer
Python
1
star
38

gotime

Gotime is a time toolkit for golang.
1
star
39

url-parse-feed

load chrome export bookmark, tag <a> parse to URL, URL parse to feed URL. 将chrome导出的书签解析成URL,再将URL解析成feed的URL
Java
1
star