-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f8c3c8
commit 3e56cf1
Showing
5 changed files
with
66 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
package rpc | ||
|
||
import ( | ||
"time" | ||
|
||
grpcRateLimit "github.com/grpc-ecosystem/go-grpc-middleware/ratelimit" | ||
"github.com/juju/ratelimit" | ||
) | ||
|
||
const ( | ||
LimiterAlwaysPass = "always_pass" | ||
LimiterNoBlock = "no_block" | ||
) | ||
|
||
var limiterMap = map[string]grpcRateLimit.Limiter{ | ||
LimiterAlwaysPass: &alwaysPassLimiter{}, | ||
LimiterNoBlock: &rateLimitNoBlock{}, | ||
} | ||
|
||
func name2Limiter(typeName string) grpcRateLimit.Limiter { | ||
return limiterMap[typeName] | ||
} | ||
|
||
// alwaysPassLimiter is an example limiter which implements Limiter interface. | ||
// It does not limit any request because Limit function always returns false. | ||
type alwaysPassLimiter struct{} | ||
|
||
func (*alwaysPassLimiter) Limit() bool { | ||
return false | ||
} | ||
|
||
var tb *ratelimit.Bucket | ||
|
||
func initRateLimit(cfg *Config) grpcRateLimit.Limiter { | ||
if cfg.RateLimit.Type != LimiterAlwaysPass { | ||
tb = ratelimit.NewBucket(time.Duration(cfg.RateLimit.FillInterval), cfg.RateLimit.Capacity) | ||
} | ||
limiter := name2Limiter(cfg.RateLimit.Type) | ||
return limiter | ||
} | ||
|
||
// rateLimitNoBlock 如果桶中没有token,不block,直接返回 | ||
type rateLimitNoBlock struct{} | ||
|
||
func (*rateLimitNoBlock) Limit() bool { | ||
if tb == nil { | ||
return false | ||
} | ||
count := tb.TakeAvailable(1) | ||
if count == 0 { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters