@@ -2,16 +2,10 @@ package config
22
33import (
44 "errors"
5- "fmt"
6- "slices"
7- "time"
85
9- "github.com/mitchellh/mapstructure"
106 "github.com/spf13/viper"
117
128 "github.com/openmeterio/openmeter/pkg/errorsx"
13- "github.com/openmeterio/openmeter/pkg/models"
14- "github.com/openmeterio/openmeter/pkg/redis"
159)
1610
1711type BalanceWorkerConfiguration struct {
@@ -46,115 +40,35 @@ type rawBalanceWorkerStateStorageConfiguration struct {
4640}
4741
4842type BalanceWorkerStateStorageConfiguration struct {
49- Driver BalanceWorkerStateStorageDriver
50-
51- BalanceWorkerStateStorageBackendConfiguration
52- }
53-
54- func (c * BalanceWorkerStateStorageConfiguration ) DecodeMap (v map [string ]any ) error {
55- var raw rawBalanceWorkerStateStorageConfiguration
56-
57- if err := mapstructure .Decode (v , & raw ); err != nil {
58- return err
59- }
60-
61- c .Driver = raw .Driver
62-
63- switch c .Driver {
64- case BalanceWorkerStateStorageDriverRedis :
65- var redisConfig BalanceWorkerStateStorageRedisBackendConfiguration
66-
67- decoder , err := mapstructure .NewDecoder (& mapstructure.DecoderConfig {
68- Metadata : nil ,
69- Result : & redisConfig ,
70- WeaklyTypedInput : true ,
71- DecodeHook : mapstructure .ComposeDecodeHookFunc (
72- mapstructure .StringToTimeDurationHookFunc (),
73- ),
74- })
75- if err != nil {
76- return err
77- }
78-
79- if err := decoder .Decode (raw .Config ); err != nil {
80- return err
81- }
82-
83- c .BalanceWorkerStateStorageBackendConfiguration = redisConfig
84- case BalanceWorkerStateStorageDriverInMemory :
85- // no config
86- }
87-
88- return nil
43+ HighWatermarkCache BalanceWorkerHighWatermarkCacheConfiguration
8944}
9045
9146func (c BalanceWorkerStateStorageConfiguration ) Validate () error {
92- errs := []error {}
93- if ! slices .Contains ([]BalanceWorkerStateStorageDriver {
94- BalanceWorkerStateStorageDriverRedis ,
95- BalanceWorkerStateStorageDriverInMemory ,
96- }, c .Driver ) {
97- errs = append (errs , fmt .Errorf ("invalid driver: %s" , c .Driver ))
98- }
47+ var errs []error
9948
100- if c .Driver == BalanceWorkerStateStorageDriverRedis {
101- if c .BalanceWorkerStateStorageBackendConfiguration == nil {
102- errs = append (errs , errors .New ("state storage backend configuration is required" ))
103- } else {
104- if err := c .BalanceWorkerStateStorageBackendConfiguration .Validate (); err != nil {
105- errs = append (errs , fmt .Errorf ("state storage backend: %w" , err ))
106- }
107- }
49+ if err := c .HighWatermarkCache .Validate (); err != nil {
50+ errs = append (errs , errorsx .WithPrefix (err , "high watermark cache" ))
10851 }
10952
11053 return errors .Join (errs ... )
11154}
11255
113- func (c BalanceWorkerStateStorageConfiguration ) GetRedisBackendConfiguration () (BalanceWorkerStateStorageRedisBackendConfiguration , error ) {
114- if c .Driver != BalanceWorkerStateStorageDriverRedis {
115- return BalanceWorkerStateStorageRedisBackendConfiguration {}, fmt .Errorf ("driver is not redis" )
116- }
117-
118- if c .BalanceWorkerStateStorageBackendConfiguration == nil {
119- return BalanceWorkerStateStorageRedisBackendConfiguration {}, errors .New ("state storage backend configuration is required" )
120- }
121-
122- redisConfig , ok := c .BalanceWorkerStateStorageBackendConfiguration .(BalanceWorkerStateStorageRedisBackendConfiguration )
123- if ! ok {
124- return BalanceWorkerStateStorageRedisBackendConfiguration {}, fmt .Errorf ("state storage backend configuration is not a redis configuration" )
125- }
126-
127- return redisConfig , nil
56+ type BalanceWorkerHighWatermarkCacheConfiguration struct {
57+ LRUCacheSize int
12858}
12959
130- type BalanceWorkerStateStorageBackendConfiguration interface {
131- models.Validator
132- }
133-
134- type BalanceWorkerStateStorageRedisBackendConfiguration struct {
135- redis.Config `mapstructure:",squash"`
136- Expiration time.Duration
137- }
138-
139- func (c BalanceWorkerStateStorageRedisBackendConfiguration ) Validate () error {
140- errs := []error {}
141-
142- if c .Expiration <= 0 {
143- errs = append (errs , errors .New ("expiration should be greater than 0" ))
144- }
145-
146- if err := c .Config .Validate (); err != nil {
147- errs = append (errs , fmt .Errorf ("redis: %w" , err ))
60+ func (c BalanceWorkerHighWatermarkCacheConfiguration ) Validate () error {
61+ if c .LRUCacheSize <= 0 {
62+ return errors .New ("LRU cache size must be positive" )
14863 }
14964
150- return errors . Join ( errs ... )
65+ return nil
15166}
15267
15368func ConfigureBalanceWorker (v * viper.Viper ) {
15469 ConfigureConsumer (v , "balanceWorker" )
15570 v .SetDefault ("balanceWorker.dlq.topic" , "om_sys.balance_worker_dlq" )
15671 v .SetDefault ("balanceWorker.consumerGroupName" , "om_balance_worker" )
15772
158- v .SetDefault ("balanceWorker.stateStorage.driver" , BalanceWorkerStateStorageDriverInMemory )
159- v .SetDefault ("balanceWorker.stateStorage.config.expiration" , 24 * time .Hour )
73+ v .SetDefault ("balanceWorker.stateStorage.highWatermarkCache.lruCacheSize" , 100_000 )
16074}
0 commit comments