Skip to content

Commit

Permalink
dev: Use Redis client instead of Config
Browse files Browse the repository at this point in the history
  • Loading branch information
happyRip committed Feb 21, 2024
1 parent 29e784a commit ffeb714
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
3 changes: 0 additions & 3 deletions cmd/internal/shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ var DefaultKeyVaultConfig = config.KeyVault{
// DefaultRateLimitingConfig is the default config for rate limiting.
var DefaultRateLimitingConfig = config.RateLimiting{
Provider: "memory",
Redis: config.RateLimitingRedis{
Config: DefaultRedisConfig,
},
}

// DefaultTracingConfig is the default config for telemetry tracing.
Expand Down
9 changes: 9 additions & 0 deletions cmd/ttn-lw-stack/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func NewJoinServerSessionKeyRegistryRedis(conf *Config) *redis.Client {
return redis.New(conf.Redis.WithNamespace("js", "keys"))
}

// NewRateLimitingRedis instantiates a new redis client with the Rate Limiting namespace.
func NewRateLimitingRedis(conf *Config) *redis.Client {
if conf.RateLimiting.Provider != "redis" {
return nil
}
return redis.New(conf.Cache.Redis.WithNamespace("rate-limiting"))
}

var errUnknownComponent = errors.DefineInvalidArgument("unknown_component", "unknown component `{component}`")

var startCommand = &cobra.Command{
Expand Down Expand Up @@ -218,6 +226,7 @@ var startCommand = &cobra.Command{
logger.Warn("No cookie block key configured, generated a random one")
}

config.RateLimiting.Redis.Client = NewRateLimitingRedis(config)
c, err := component.New(logger, &component.Config{ServiceBase: config.ServiceBase}, componentOptions...)
if err != nil {
return shared.ErrInitializeBaseComponent.WithCause(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (f MQTTConfigProviderFunc) GetMQTTConfig(ctx context.Context) (*MQTT, error

// RateLimitingRedis represents configuration for the in-Redis rate limiting store.
type RateLimitingRedis struct {
Config redis.Config `name:"config"`
Client *redis.Client `name:"-"`
}

// RateLimitingProfile represents configuration for a rate limiting class.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gatewayserver/io/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Serve(ctx context.Context, server io.Server, conn *net.UDPConn, conf Config
}
limitLogs, err := ratelimit.NewProfile(ctx, limitLogsConfig, ratelimit.StoreConfig{
Provider: conf.RateLimiting.Provider,
Redis: ratelimit.NewRedisClient(conf.RateLimiting.Redis.Config),
Redis: conf.RateLimiting.Redis.Client,
})
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions pkg/ratelimit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ type StoreConfig struct {
Redis *ttnredis.Client
}

// NewRedisClient creates a new Redis client with rate-limiting namespace.
func NewRedisClient(conf ttnredis.Config) *ttnredis.Client {
return ttnredis.New(conf.WithNamespace("rate-limiting"))
}

// New creates a new ratelimit.Interface from configuration.
func New(ctx context.Context, conf config.RateLimiting, blobConf config.BlobConfig, httpClientProvider httpclient.Provider) (Interface, error) {
defaultLimiter := &NoopRateLimiter{}
Expand Down Expand Up @@ -81,7 +76,7 @@ func New(ctx context.Context, conf config.RateLimiting, blobConf config.BlobConf
limiter, err := NewProfile(ctx, profile, StoreConfig{
Provider: conf.Provider,
Memory: conf.Memory,
Redis: NewRedisClient(conf.Redis.Config),
Redis: conf.Redis.Client,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit ffeb714

Please sign in to comment.