Skip to content

Commit 0125a42

Browse files
authored
Merge pull request containerd#9729 from mxpv/duration
Remove duplicated TOML duration parsers
2 parents 96bf529 + 9340be7 commit 0125a42

File tree

3 files changed

+11
-39
lines changed

3 files changed

+11
-39
lines changed

plugins/gc/scheduler.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sync"
2424
"time"
2525

26+
"github.com/containerd/containerd/v2/internal/tomlext"
2627
"github.com/containerd/containerd/v2/pkg/gc"
2728
"github.com/containerd/containerd/v2/plugins"
2829
"github.com/containerd/log"
@@ -70,7 +71,7 @@ type config struct {
7071
// schedule. Use suffix "ms" for millisecond and "s" for second.
7172
//
7273
// Default is "0ms"
73-
ScheduleDelay duration `toml:"schedule_delay"`
74+
ScheduleDelay tomlext.Duration `toml:"schedule_delay"`
7475

7576
// StartupDelay is the delay duration to do an initial garbage
7677
// collection after startup. The initial garbage collection is used to
@@ -79,22 +80,7 @@ type config struct {
7980
// "ms" for millisecond and "s" for second.
8081
//
8182
// Default is "100ms"
82-
StartupDelay duration `toml:"startup_delay"`
83-
}
84-
85-
type duration time.Duration
86-
87-
func (d *duration) UnmarshalText(text []byte) error {
88-
ed, err := time.ParseDuration(string(text))
89-
if err != nil {
90-
return err
91-
}
92-
*d = duration(ed)
93-
return nil
94-
}
95-
96-
func (d duration) MarshalText() (text []byte, err error) {
97-
return []byte(time.Duration(d).String()), nil
83+
StartupDelay tomlext.Duration `toml:"startup_delay"`
9884
}
9985

10086
func init() {
@@ -108,8 +94,8 @@ func init() {
10894
PauseThreshold: 0.02,
10995
DeletionThreshold: 0,
11096
MutationThreshold: 100,
111-
ScheduleDelay: duration(0),
112-
StartupDelay: duration(100 * time.Millisecond),
97+
ScheduleDelay: tomlext.FromStdTime(0),
98+
StartupDelay: tomlext.FromStdTime(100 * time.Millisecond),
11399
},
114100
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
115101
md, err := ic.GetSingle(plugins.MetadataPlugin)

plugins/gc/scheduler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/containerd/containerd/v2/internal/tomlext"
2526
"github.com/containerd/containerd/v2/pkg/gc"
2627
"github.com/stretchr/testify/assert"
2728
)
@@ -152,7 +153,7 @@ func TestStartupDelay(t *testing.T) {
152153
cfg = &config{
153154
// Prevent GC from scheduling again before check
154155
PauseThreshold: 0.001,
155-
StartupDelay: duration(startupDelay),
156+
StartupDelay: tomlext.Duration(startupDelay),
156157
}
157158
tc = &testCollector{
158159
d: time.Second,

plugins/restart/monitor.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,18 @@ import (
2525

2626
containerd "github.com/containerd/containerd/v2/client"
2727
"github.com/containerd/containerd/v2/core/runtime/restart"
28+
"github.com/containerd/containerd/v2/internal/tomlext"
2829
"github.com/containerd/containerd/v2/pkg/namespaces"
2930
"github.com/containerd/containerd/v2/plugins"
3031
"github.com/containerd/log"
3132
"github.com/containerd/plugin"
3233
"github.com/containerd/plugin/registry"
3334
)
3435

35-
type duration struct {
36-
time.Duration
37-
}
38-
39-
func (d *duration) UnmarshalText(text []byte) error {
40-
var err error
41-
d.Duration, err = time.ParseDuration(string(text))
42-
return err
43-
}
44-
45-
func (d duration) MarshalText() ([]byte, error) {
46-
return []byte(d.Duration.String()), nil
47-
}
48-
4936
// Config for the restart monitor
5037
type Config struct {
5138
// Interval for how long to wait to check for state changes
52-
Interval duration `toml:"interval"`
39+
Interval tomlext.Duration `toml:"interval"`
5340
}
5441

5542
func init() {
@@ -61,9 +48,7 @@ func init() {
6148
},
6249
ID: "restart",
6350
Config: &Config{
64-
Interval: duration{
65-
Duration: 10 * time.Second,
66-
},
51+
Interval: tomlext.FromStdTime(10 * time.Second),
6752
},
6853
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
6954
ic.Meta.Capabilities = []string{"no", "always", "on-failure", "unless-stopped"}
@@ -74,7 +59,7 @@ func init() {
7459
m := &monitor{
7560
client: client,
7661
}
77-
go m.run(ic.Config.(*Config).Interval.Duration)
62+
go m.run(tomlext.ToStdTime(ic.Config.(*Config).Interval))
7863
return m, nil
7964
},
8065
})

0 commit comments

Comments
 (0)