65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package reliable
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
"github.com/tech/sendico/pkg/model"
|
|
)
|
|
|
|
const SettingsBlockKey = "reliable_publisher"
|
|
|
|
type Settings struct {
|
|
Enabled bool `mapstructure:"enabled" yaml:"enabled"`
|
|
BatchSize int `mapstructure:"batch_size" yaml:"batch_size"`
|
|
PollIntervalSeconds int `mapstructure:"poll_interval_seconds" yaml:"poll_interval_seconds"`
|
|
MaxAttempts int `mapstructure:"max_attempts" yaml:"max_attempts"`
|
|
}
|
|
|
|
func DefaultSettings() Settings {
|
|
return Settings{
|
|
Enabled: true,
|
|
BatchSize: defaultBatchSize,
|
|
PollIntervalSeconds: int(defaultPollInterval.Seconds()),
|
|
MaxAttempts: defaultMaxAttempts,
|
|
}
|
|
}
|
|
|
|
func ParseSettings(driverSettings model.SettingsT) (Settings, error) {
|
|
settings := DefaultSettings()
|
|
if len(driverSettings) == 0 {
|
|
return settings, nil
|
|
}
|
|
|
|
raw, ok := driverSettings[SettingsBlockKey]
|
|
if !ok || raw == nil {
|
|
return settings, nil
|
|
}
|
|
|
|
if err := mapstructure.Decode(raw, &settings); err != nil {
|
|
return Settings{}, err
|
|
}
|
|
|
|
settings.applyDefaults()
|
|
return settings, nil
|
|
}
|
|
|
|
func (s *Settings) PollInterval() time.Duration {
|
|
if s == nil || s.PollIntervalSeconds <= 0 {
|
|
return defaultPollInterval
|
|
}
|
|
return time.Duration(s.PollIntervalSeconds) * time.Second
|
|
}
|
|
|
|
func (s *Settings) applyDefaults() {
|
|
if s.BatchSize <= 0 {
|
|
s.BatchSize = defaultBatchSize
|
|
}
|
|
if s.PollIntervalSeconds <= 0 {
|
|
s.PollIntervalSeconds = int(defaultPollInterval.Seconds())
|
|
}
|
|
if s.MaxAttempts <= 0 {
|
|
s.MaxAttempts = defaultMaxAttempts
|
|
}
|
|
}
|