Skip to content

Commit

Permalink
throw error when rate limit cache type is unknown
Browse files Browse the repository at this point in the history
Signed-off-by: William Albertus Dembo <[email protected]>
  • Loading branch information
walbertus committed Dec 7, 2020
1 parent 88c4a9b commit 79481f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
15 changes: 7 additions & 8 deletions src/redis/cache_impl.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package redis

import (
"fmt"
"math/rand"

"github.com/coocood/freecache"
"github.com/envoyproxy/ratelimit/src/limiter"
"github.com/envoyproxy/ratelimit/src/server"
"github.com/envoyproxy/ratelimit/src/settings"
logger "github.com/sirupsen/logrus"
)

func NewRateLimiterCacheImplFromSettings(s settings.Settings, localCache *freecache.Cache, srv server.Server, timeSource limiter.TimeSource, jitterRand *rand.Rand, expirationJitterMaxSeconds int64) limiter.RateLimitCache {
func NewRateLimiterCacheImplFromSettings(s settings.Settings, localCache *freecache.Cache, srv server.Server, timeSource limiter.TimeSource, jitterRand *rand.Rand, expirationJitterMaxSeconds int64) (limiter.RateLimitCache, error) {
var perSecondPool Client
if s.RedisPerSecond {
perSecondPool = NewClientImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondTls, s.RedisPerSecondAuth,
Expand All @@ -28,18 +28,17 @@ func NewRateLimiterCacheImplFromSettings(s settings.Settings, localCache *freeca
jitterRand,
expirationJitterMaxSeconds,
localCache,
s.NearLimitRatio)
} else if s.RateLimitAlgorithm == settings.WindowedRateLimit {
s.NearLimitRatio), nil
}
if s.RateLimitAlgorithm == settings.WindowedRateLimit {
return NewWindowedRateLimitCacheImpl(
otherPool,
perSecondPool,
timeSource,
jitterRand,
expirationJitterMaxSeconds,
localCache,
s.NearLimitRatio)
} else {
logger.Fatalf("Unknown rate limit algorithm. %s\n", s.RateLimitAlgorithm)
s.NearLimitRatio), nil
}
return nil
return nil, fmt.Errorf("Unknown rate limit algorithm. %s\n", s.RateLimitAlgorithm)
}
18 changes: 11 additions & 7 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ func (runner *Runner) Run() {
}

srv := server.NewServer("ratelimit", runner.statsStore, localCache, settings.GrpcUnaryInterceptor(nil))
rateLimitCache, err := redis.NewRateLimiterCacheImplFromSettings(
s,
localCache,
srv,
limiter.NewTimeSourceImpl(),
rand.New(limiter.NewLockedSource(time.Now().Unix())),
s.ExpirationJitterMaxSeconds)
if err != nil {
logger.Fatalf("Could not setup ratelimit cache. %v\n", err)
}

service := ratelimit.NewService(
srv.Runtime(),
redis.NewRateLimiterCacheImplFromSettings(
s,
localCache,
srv,
limiter.NewTimeSourceImpl(),
rand.New(limiter.NewLockedSource(time.Now().Unix())),
s.ExpirationJitterMaxSeconds),
rateLimitCache,
config.NewRateLimitConfigLoaderImpl(),
srv.Scope().Scope("service"),
s.RuntimeWatchRoot,
Expand Down

0 comments on commit 79481f3

Please sign in to comment.