Skip to content

Commit

Permalink
chore: log level and config name
Browse files Browse the repository at this point in the history
  • Loading branch information
gussf committed Jun 6, 2024
1 parent 51048f4 commit f186640
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
3 changes: 2 additions & 1 deletion config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ feedbackListeners:
database: push
connectionTimeout: 100
rateLimiter:
limit: 100
limit:
rpm: 100
redis:
host: "localhost"
port: "6379"
Expand Down
3 changes: 2 additions & 1 deletion config/docker_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ feedbackListeners:
database: push
connectionTimeout: 100
rateLimiter:
limit: 100
limit:
rpm: 100
redis:
host: "redis"
port: "6379"
Expand Down
3 changes: 2 additions & 1 deletion config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ feedbackListeners:
database: push
connectionTimeout: 100
rateLimiter:
limit: 100
limit:
rpm: 100
redis:
host: "localhost"
port: "6379"
Expand Down
2 changes: 1 addition & 1 deletion extensions/gcm_message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (g *GCMMessageHandler) sendMessage(message interfaces.KafkaMessage) error {
if !allowed {
statsReporterNotificationRateLimitReached(g.StatsReporters, message.Game, "gcm")
l.Warn("rate limit reached")
return errors.New("reached rate limit")
return errors.New("rate limit reached")
}
l.Debug("sending message to gcm")

Expand Down
22 changes: 11 additions & 11 deletions extensions/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (
)

type rateLimiter struct {
redis *redis.Client
limit int
l *logrus.Entry
redis *redis.Client
rpmLimit int
l *logrus.Entry
}

func NewRateLimiter(config *viper.Viper, logger *logrus.Logger) rateLimiter {
host := config.GetString("rateLimiter.redis.host")
port := config.GetString("rateLimiter.redis.port")
pwd := config.GetString("rateLimiter.redis.password")
limit := config.GetInt("rateLimiter.limit")
limit := config.GetInt("rateLimiter.limit.rpm")

addr := fmt.Sprintf("%s:%s", host, port)
rdb := redis.NewClient(&redis.Options{
Expand All @@ -33,11 +33,11 @@ func NewRateLimiter(config *viper.Viper, logger *logrus.Logger) rateLimiter {
})

return rateLimiter{
redis: rdb,
limit: limit,
redis: rdb,
rpmLimit: limit,
l: logger.WithFields(logrus.Fields{
"extension": "RateLimiter",
"limit": limit,
"rpmLimit": limit,
}),
}
}
Expand All @@ -55,7 +55,7 @@ func (r rateLimiter) Allow(ctx context.Context, device string) bool {
val, err := r.redis.Get(ctx, deviceKey).Result()
if err != nil && !errors.Is(err, redis.Nil) {
// Something went wrong, return true to avoid blocking notifications.
l.WithError(err).Info("could not get current rate in redis")
l.WithError(err).Error("could not get current rate in redis")
return true
}
if errors.Is(err, redis.Nil) {
Expand All @@ -66,11 +66,11 @@ func (r rateLimiter) Allow(ctx context.Context, device string) bool {
current, err := strconv.Atoi(val)
if err != nil {
// Something went wrong, return true to avoid blocking notifications.
l.WithError(err).Info("current rate is invalid")
l.WithError(err).Error("current rate is invalid")
return true
}

if current >= r.limit {
if current >= r.rpmLimit {
return false
}

Expand All @@ -81,7 +81,7 @@ func (r rateLimiter) Allow(ctx context.Context, device string) bool {
})
if err != nil {
// Allow the operation even if the transaction fails, to avoid blocking notifications.
l.WithError(err).Info("increment to current rate failed")
l.WithError(err).Error("increment to current rate failed")
}

l.WithField("currentRate", current).Debug("current rate allows message")
Expand Down
2 changes: 1 addition & 1 deletion extensions/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = FDescribe("Rate Limiter", func() {
Describe("Rate limiting", func() {
It("should return not-allowed when rate limit is reached", func() {
rl := NewRateLimiter(config, logger)
rl.limit = 1
rl.rpmLimit = 1
ctx := context.Background()
device := uuid.NewString()
allowed := rl.Allow(ctx, device)
Expand Down

0 comments on commit f186640

Please sign in to comment.