Skip to content

Commit

Permalink
config: add newtype for Durations
Browse files Browse the repository at this point in the history
This was an oversight that prevented JSON serialization working. The
change is a breaking code change, but keeps the YAML serialization
(currently the only one) the same.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jun 15, 2023
1 parent 1ebbbf2 commit cee776b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
19 changes: 19 additions & 0 deletions config/config.go
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"net"
"time"
)

// Config is the configuration object for the commands in
Expand Down Expand Up @@ -75,3 +76,21 @@ func (c *Config) lint() (ws []Warning, err error) {
}
return ws, nil
}

// Duration is a serializeable [time.Duration].
type Duration time.Duration

// UnmarshalText implements [encoding.TextUnmarshaler].
func (d *Duration) UnmarshalText(b []byte) error {
dur, err := time.ParseDuration(string(b))
if err != nil {
return err
}
*d = Duration(dur)
return nil
}

// MarshalText implements [encoding.TextMarshaler].
func (d *Duration) MarshalText() ([]byte, error) {
return []byte(time.Duration(*d).String()), nil
}
9 changes: 4 additions & 5 deletions config/matcher.go
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"net/url"
"time"
)

// Matcher is the configuration for the matcher service.
Expand All @@ -23,7 +22,7 @@ type Matcher struct {
// Period controls how often updaters are run.
//
// The default is 30 minutes.
Period time.Duration `yaml:"period,omitempty" json:"period,omitempty"`
Period Duration `yaml:"period,omitempty" json:"period,omitempty"`
// UpdateRetention controls the number of updates to retain between
// garbage collection periods.
//
Expand All @@ -48,7 +47,7 @@ type Matcher struct {
//
// If empty, the duration set in "Period" will be used. This means client
// may cache "stale" results for 2(Period) - 1 seconds.
CacheAge time.Duration `yaml:"cache_age,omitempty" json:"cache_age,omitempty"`
CacheAge Duration `yaml:"cache_age,omitempty" json:"cache_age,omitempty"`
// A "true" or "false" value
//
// Whether Matcher nodes handle migrations to their databases.
Expand All @@ -65,7 +64,7 @@ func (m *Matcher) validate(mode Mode) ([]Warning, error) {
return nil, nil
}
if m.Period == 0 {
m.Period = DefaultMatcherPeriod
m.Period = Duration(DefaultMatcherPeriod)
}
switch {
case m.UpdateRetention < 0:
Expand Down Expand Up @@ -103,7 +102,7 @@ func (m *Matcher) lint() (ws []Warning, err error) {
ws[i].path = ".connstring"
}

if m.Period < DefaultMatcherPeriod {
if m.Period < Duration(DefaultMatcherPeriod) {
ws = append(ws, Warning{
path: ".period",
msg: "updater period is very aggressive: most sources are updated daily",
Expand Down
16 changes: 8 additions & 8 deletions config/notifier.go
Expand Up @@ -42,14 +42,14 @@ type Notifier struct {
// The frequency at which the notifier will query at Matcher for Update Operations.
// If a value smaller then 1 second is provided it will be replaced with the
// default 5 second poll interval.
PollInterval time.Duration `yaml:"poll_interval,omitempty" json:"poll_interval,omitempty"`
PollInterval Duration `yaml:"poll_interval,omitempty" json:"poll_interval,omitempty"`
// A time.ParseDuration parsable string
//
// The frequency at which the notifier attempt delivery of created or previously failed
// notifications
// If a value smaller then 1 second is provided it will be replaced with the
// default 5 second delivery interval.
DeliveryInterval time.Duration `yaml:"delivery_interval,omitempty" json:"delivery_interval,omitempty"`
DeliveryInterval Duration `yaml:"delivery_interval,omitempty" json:"delivery_interval,omitempty"`
// DisableSummary disables summarizing vulnerabilities per-manifest.
//
// The default is to summarize any new vulnerabilities to the most severe
Expand All @@ -70,11 +70,11 @@ func (n *Notifier) validate(mode Mode) ([]Warning, error) {
if mode != ComboMode && mode != NotifierMode {
return nil, nil
}
if n.PollInterval < 1*time.Second {
n.PollInterval = DefaultNotifierPollInterval
if n.PollInterval < Duration(1*time.Second) {
n.PollInterval = Duration(DefaultNotifierPollInterval)
}
if n.DeliveryInterval < 1*time.Second {
n.DeliveryInterval = DefaultNotifierDeliveryInterval
if n.DeliveryInterval < Duration(1*time.Second) {
n.DeliveryInterval = Duration(DefaultNotifierDeliveryInterval)
}
switch mode {
case ComboMode:
Expand Down Expand Up @@ -120,13 +120,13 @@ func (n *Notifier) lint() (ws []Warning, err error) {
})
}

if n.PollInterval < DefaultNotifierPollInterval {
if n.PollInterval < Duration(DefaultNotifierPollInterval) {
ws = append(ws, Warning{
path: ".poll_interval",
msg: "interval is very fast: may result in increased workload",
})
}
if n.DeliveryInterval < DefaultNotifierDeliveryInterval {
if n.DeliveryInterval < Duration(DefaultNotifierDeliveryInterval) {
ws = append(ws, Warning{
path: ".delivery_interval",
msg: "interval is very fast: may result in increased workload",
Expand Down

0 comments on commit cee776b

Please sign in to comment.