From cede3048e29127542262813a072cca48936a111e Mon Sep 17 00:00:00 2001 From: AllianceTing <2426446427@qq.com> Date: Wed, 28 Jun 2023 18:55:56 +0800 Subject: [PATCH 1/6] feat: issue20 --- .../internal/middleware/frequencyControl.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 server/internal/middleware/frequencyControl.go diff --git a/server/internal/middleware/frequencyControl.go b/server/internal/middleware/frequencyControl.go new file mode 100644 index 0000000..4ec151f --- /dev/null +++ b/server/internal/middleware/frequencyControl.go @@ -0,0 +1,55 @@ +package middleware + +import ( + "sync" + "time" + + "github.com/gin-gonic/gin" +) + +// define rate limit struct +type frequencyControlByTokenBucket struct { + refreshRate float64 // 令牌的刷新速率 + capacity int64 // 桶的容量 + tokens float64 // 当前令牌数量 + lastToken time.Time // 上一次放令牌的时间 + mtx sync.Mutex // 互斥锁 +} + +// allow frequency +func (tb *frequencyControlByTokenBucket) Allow() bool { + tb.mtx.Lock() + defer tb.mtx.Unlock() + now := time.Now() + // compute tokens which needs + tb.tokens = tb.tokens + tb.refreshRate*now.Sub(tb.lastToken).Seconds() + if tb.tokens > float64(tb.capacity) { + tb.tokens = float64(tb.capacity) + } + // judge weather to pass through + if tb.tokens >= 1 { + tb.tokens-- + tb.lastToken = now + return true + } else { + return false + } +} + +// registried a middle ware +func LimitHandler(maxConn int, refreshRate float64) gin.HandlerFunc { + tb := &frequencyControlByTokenBucket{ + capacity: int64(maxConn), + refreshRate: refreshRate, + tokens: 0, + lastToken: time.Now(), + } + return func(c *gin.Context) { + if !tb.Allow() { + c.String(503, "Too many request") + c.Abort() + return + } + c.Next() + } +} From 4d7217a644bf338406b416e58e0d54bc4aa9a1a2 Mon Sep 17 00:00:00 2001 From: SgitBusity <72297937+AllianceTing@users.noreply.github.com> Date: Thu, 29 Jun 2023 00:40:46 +0800 Subject: [PATCH 2/6] Rename frequencyControl.go to frequency_control.go Signed-off-by: SgitBusity <72297937+AllianceTing@users.noreply.github.com> --- .../middleware/{frequencyControl.go => frequency_control.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename server/internal/middleware/{frequencyControl.go => frequency_control.go} (100%) diff --git a/server/internal/middleware/frequencyControl.go b/server/internal/middleware/frequency_control.go similarity index 100% rename from server/internal/middleware/frequencyControl.go rename to server/internal/middleware/frequency_control.go From 24bad4e3d8e142d8c83336f93eba8509476ed95a Mon Sep 17 00:00:00 2001 From: SgitBusity <72297937+AllianceTing@users.noreply.github.com> Date: Fri, 30 Jun 2023 11:58:29 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20frequency=5Fcontrol.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit transfer to I18N --- server/internal/middleware/frequency_control.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/internal/middleware/frequency_control.go b/server/internal/middleware/frequency_control.go index 4ec151f..3ebb536 100644 --- a/server/internal/middleware/frequency_control.go +++ b/server/internal/middleware/frequency_control.go @@ -10,10 +10,10 @@ import ( // define rate limit struct type frequencyControlByTokenBucket struct { refreshRate float64 // 令牌的刷新速率 - capacity int64 // 桶的容量 - tokens float64 // 当前令牌数量 - lastToken time.Time // 上一次放令牌的时间 - mtx sync.Mutex // 互斥锁 + capacity int64 // bucket's capacity + tokens float64 // tokens' count + lastToken time.Time //latest time token stored + mtx sync.Mutex // mutex } // allow frequency From 3ecd154115ddfd1760645634549cc00aecb3e118 Mon Sep 17 00:00:00 2001 From: SgitBusity <72297937+AllianceTing@users.noreply.github.com> Date: Sun, 2 Jul 2023 15:24:59 +0800 Subject: [PATCH 4/6] Update frequency_control.go Signed-off-by: SgitBusity <72297937+AllianceTing@users.noreply.github.com> --- server/internal/middleware/frequency_control.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/internal/middleware/frequency_control.go b/server/internal/middleware/frequency_control.go index 3ebb536..3354789 100644 --- a/server/internal/middleware/frequency_control.go +++ b/server/internal/middleware/frequency_control.go @@ -31,12 +31,10 @@ func (tb *frequencyControlByTokenBucket) Allow() bool { tb.tokens-- tb.lastToken = now return true - } else { - return false - } + } } -// registried a middle ware +// LimitHandler registried a middle ware to use func LimitHandler(maxConn int, refreshRate float64) gin.HandlerFunc { tb := &frequencyControlByTokenBucket{ capacity: int64(maxConn), @@ -48,6 +46,7 @@ func LimitHandler(maxConn int, refreshRate float64) gin.HandlerFunc { if !tb.Allow() { c.String(503, "Too many request") c.Abort() + return } c.Next() From 1c02220834bcbf86be030e11ce4c7eb92d130b9d Mon Sep 17 00:00:00 2001 From: SgitBusity <72297937+AllianceTing@users.noreply.github.com> Date: Sun, 2 Jul 2023 15:35:48 +0800 Subject: [PATCH 5/6] Update frequency_control.go Signed-off-by: SgitBusity <72297937+AllianceTing@users.noreply.github.com> --- server/internal/middleware/frequency_control.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/internal/middleware/frequency_control.go b/server/internal/middleware/frequency_control.go index 3354789..c79fab7 100644 --- a/server/internal/middleware/frequency_control.go +++ b/server/internal/middleware/frequency_control.go @@ -31,7 +31,8 @@ func (tb *frequencyControlByTokenBucket) Allow() bool { tb.tokens-- tb.lastToken = now return true - } + } + return false } // LimitHandler registried a middle ware to use From e2d61ccbf0dd6fe886ab89494d791b9b35e80714 Mon Sep 17 00:00:00 2001 From: SgitBusity <72297937+AllianceTing@users.noreply.github.com> Date: Sun, 2 Jul 2023 15:38:03 +0800 Subject: [PATCH 6/6] Update frequency_control.go fix return formating Signed-off-by: SgitBusity <72297937+AllianceTing@users.noreply.github.com> --- server/internal/middleware/frequency_control.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/internal/middleware/frequency_control.go b/server/internal/middleware/frequency_control.go index c79fab7..6cc124f 100644 --- a/server/internal/middleware/frequency_control.go +++ b/server/internal/middleware/frequency_control.go @@ -32,6 +32,7 @@ func (tb *frequencyControlByTokenBucket) Allow() bool { tb.lastToken = now return true } + return false }