add request rate limiter
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"github.com/go-gost/core/auth"
|
||||
"github.com/go-gost/core/bypass"
|
||||
"github.com/go-gost/core/chain"
|
||||
"github.com/go-gost/core/limiter/rate"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/core/metadata"
|
||||
)
|
||||
@ -16,6 +17,7 @@ type Options struct {
|
||||
Router *chain.Router
|
||||
Auth *url.Userinfo
|
||||
Auther auth.Authenticator
|
||||
RateLimiter rate.RateLimiter
|
||||
TLSConfig *tls.Config
|
||||
Logger logger.Logger
|
||||
}
|
||||
@ -46,6 +48,12 @@ func AutherOption(auther auth.Authenticator) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func RateLimiterOption(limiter rate.RateLimiter) Option {
|
||||
return func(opts *Options) {
|
||||
opts.RateLimiter = limiter
|
||||
}
|
||||
}
|
||||
|
||||
func TLSConfigOption(tlsConfig *tls.Config) Option {
|
||||
return func(opts *Options) {
|
||||
opts.TLSConfig = tlsConfig
|
||||
|
10
limiter/rate/limiter.go
Normal file
10
limiter/rate/limiter.go
Normal file
@ -0,0 +1,10 @@
|
||||
package rate
|
||||
|
||||
type Limiter interface {
|
||||
Allow(n int) bool
|
||||
Limit() float64
|
||||
}
|
||||
|
||||
type RateLimiter interface {
|
||||
Limiter(key string) Limiter
|
||||
}
|
@ -49,6 +49,8 @@ func GetFloat(md metadata.Metadata, key string) (v float64) {
|
||||
}
|
||||
|
||||
switch vv := md.Get(key).(type) {
|
||||
case float64:
|
||||
return vv
|
||||
case int:
|
||||
return float64(vv)
|
||||
case string:
|
||||
|
Reference in New Issue
Block a user