Skip to content

Commit ba218ad

Browse files
authored
feat: allow setting key prefix for progress manager (#3615)
1 parent e3b8ea2 commit ba218ad

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

app/common/progressmanager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func NewProgressManager(logger *slog.Logger, conf config.ProgressManagerConfigur
2828

2929
pm, err := adapter.New(adapter.Config{
3030
Expiration: conf.Expiration,
31+
KeyPrefix: conf.KeyPrefix,
3132
Logger: logger,
3233
Redis: redisClient,
3334
})

app/config/progressmanager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// ProgressManagerConfiguration stores the configuration parameters for the progress manager
1414
type ProgressManagerConfiguration struct {
1515
Enabled bool
16+
KeyPrefix string
1617
Expiration time.Duration
1718
Redis redis.Config
1819
}
@@ -39,5 +40,6 @@ func (c ProgressManagerConfiguration) Validate() error {
3940
// ConfigureProgressManager sets the default values for the progress manager configuration
4041
func ConfigureProgressManager(v *viper.Viper) {
4142
v.SetDefault("progressManager.expiration", "5m")
43+
v.SetDefault("progressManager.keyPrefix", "")
4244
redis.Configure(v, "progressManager.redis")
4345
}

openmeter/progressmanager/adapter/adapter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config struct {
1414
Expiration time.Duration
1515
Redis *redis.Client
1616
Logger *slog.Logger
17+
KeyPrefix string
1718
}
1819

1920
func (c Config) Validate() error {
@@ -41,12 +42,15 @@ func New(config Config) (progressmanager.Adapter, error) {
4142
expiration: config.Expiration,
4243
redis: config.Redis,
4344
logger: config.Logger,
45+
keyPrefix: config.KeyPrefix,
4446
}, nil
4547
}
4648

4749
var _ progressmanager.Adapter = (*adapter)(nil)
4850

4951
type adapter struct {
52+
// keyPrefix is the prefix for progress data in the Redis store, if needed, the key format will be "<keyPrefix>:progress:<namespace>:<id>" or "progress:<namespace>:<id>" if the prefix is empty
53+
keyPrefix string
5054
// expiration defines how long progress data is stored in Redis before automatic removal
5155
expiration time.Duration
5256
// redis is the client for storing and retrieving progress data

openmeter/progressmanager/adapter/progress.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// keyPrefix is the prefix for progress data in the Redis store.
1515
// All progress keys will be stored as "progress:<namespace>:<id>"
16-
const keyPrefix = "progress:"
16+
const staticKeyPrefix = "progress:"
1717

1818
// GetProgress retrieves the progress
1919
func (a *adapter) GetProgress(ctx context.Context, input entity.GetProgressInput) (*entity.Progress, error) {
@@ -23,7 +23,7 @@ func (a *adapter) GetProgress(ctx context.Context, input entity.GetProgressInput
2323

2424
var progress entity.Progress
2525

26-
cmd := a.redis.Get(ctx, getKey(input.ProgressID))
26+
cmd := a.redis.Get(ctx, a.getKey(input.ProgressID))
2727

2828
if cmd.Err() != nil {
2929
if cmd.Err() == redis.Nil {
@@ -53,7 +53,7 @@ func (a *adapter) UpsertProgress(ctx context.Context, input entity.UpsertProgres
5353
return fmt.Errorf("marshal progress: %w", err)
5454
}
5555

56-
cmd := a.redis.Set(ctx, getKey(input.ProgressID), data, a.expiration)
56+
cmd := a.redis.Set(ctx, a.getKey(input.ProgressID), data, a.expiration)
5757
if cmd.Err() != nil {
5858
return fmt.Errorf("set progress: %w", cmd.Err())
5959
}
@@ -62,6 +62,10 @@ func (a *adapter) UpsertProgress(ctx context.Context, input entity.UpsertProgres
6262
}
6363

6464
// getKey returns the key for the KV store
65-
func getKey(id entity.ProgressID) string {
66-
return fmt.Sprintf("%s:%s:%s", keyPrefix, id.Namespace, id.ID)
65+
func (a *adapter) getKey(id entity.ProgressID) string {
66+
if a.keyPrefix == "" {
67+
return fmt.Sprintf("%s:%s:%s", staticKeyPrefix, id.Namespace, id.ID)
68+
}
69+
70+
return fmt.Sprintf("%s:%s:%s:%s", a.keyPrefix, staticKeyPrefix, id.Namespace, id.ID)
6771
}

0 commit comments

Comments
 (0)