add rate limiter

This commit is contained in:
ginuerzh
2022-09-05 22:47:51 +08:00
parent e23da0f319
commit 4c2131ca6d
40 changed files with 1622 additions and 135 deletions

61
limiter/generator.go Normal file
View File

@ -0,0 +1,61 @@
package limiter
import (
"github.com/go-gost/core/limiter"
)
type RateLimitGenerator interface {
In() limiter.Limiter
Out() limiter.Limiter
}
type rateLimitGenerator struct {
in int
out int
}
func NewRateLimitGenerator(in, out int) RateLimitGenerator {
return &rateLimitGenerator{
in: in,
out: out,
}
}
func (p *rateLimitGenerator) In() limiter.Limiter {
if p == nil || p.in <= 0 {
return nil
}
return NewLimiter(p.in)
}
func (p *rateLimitGenerator) Out() limiter.Limiter {
if p == nil || p.out <= 0 {
return nil
}
return NewLimiter(p.out)
}
type rateLimitSingleGenerator struct {
in limiter.Limiter
out limiter.Limiter
}
func NewRateLimitSingleGenerator(in, out int) RateLimitGenerator {
p := &rateLimitSingleGenerator{}
if in > 0 {
p.in = NewLimiter(in)
}
if out > 0 {
p.out = NewLimiter(out)
}
return p
}
func (p *rateLimitSingleGenerator) In() limiter.Limiter {
return p.in
}
func (p *rateLimitSingleGenerator) Out() limiter.Limiter {
return p.out
}