restartmanager: remove RestartManager interface

It only had a single implementation, so we may as well remove the added
complexity of defining it as an interface.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-10 17:27:38 +01:00
parent efb97da0da
commit c5d4b6b311
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 14 additions and 17 deletions

View file

@ -93,7 +93,7 @@ type Container struct {
// logDriver for closing
LogDriver logger.Logger `json:"-"`
LogCopier *logger.Copier `json:"-"`
restartManager restartmanager.RestartManager
restartManager *restartmanager.RestartManager
attachContext *attachContext
// Fields here are specific to Unix platforms
@ -570,7 +570,7 @@ func (container *Container) FullHostname() string {
}
// RestartManager returns the current restartmanager instance connected to container.
func (container *Container) RestartManager() restartmanager.RestartManager {
func (container *Container) RestartManager() *restartmanager.RestartManager {
if container.restartManager == nil {
container.restartManager = restartmanager.New(container.HostConfig.RestartPolicy, container.RestartCount)
}

View file

@ -20,13 +20,7 @@ const (
var ErrRestartCanceled = errors.New("restart canceled")
// RestartManager defines object that controls container restarting rules.
type RestartManager interface {
Cancel()
ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error)
SetPolicy(policy container.RestartPolicy)
}
type restartManager struct {
type RestartManager struct {
sync.Mutex
sync.Once
policy container.RestartPolicy
@ -37,18 +31,20 @@ type restartManager struct {
canceled bool
}
// New returns a new restartManager based on a policy.
func New(policy container.RestartPolicy, restartCount int) RestartManager {
return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
// New returns a new RestartManager based on a policy.
func New(policy container.RestartPolicy, restartCount int) *RestartManager {
return &RestartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
}
func (rm *restartManager) SetPolicy(policy container.RestartPolicy) {
// SetPolicy sets the restart-policy for the RestartManager.
func (rm *RestartManager) SetPolicy(policy container.RestartPolicy) {
rm.Lock()
rm.policy = policy
rm.Unlock()
}
func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error) {
// ShouldRestart returns whether the container should be restarted.
func (rm *RestartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error) {
if rm.policy.IsNone() {
return false, nil, nil
}
@ -126,7 +122,8 @@ func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped
return true, ch, nil
}
func (rm *restartManager) Cancel() {
// Cancel tells the RestartManager to no longer restart the container.
func (rm *RestartManager) Cancel() {
rm.Do(func() {
rm.Lock()
rm.canceled = true

View file

@ -8,7 +8,7 @@ import (
)
func TestRestartManagerTimeout(t *testing.T) {
rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager)
rm := New(container.RestartPolicy{Name: "always"}, 0)
var duration = 1 * time.Second
should, _, err := rm.ShouldRestart(0, false, duration)
if err != nil {
@ -23,7 +23,7 @@ func TestRestartManagerTimeout(t *testing.T) {
}
func TestRestartManagerTimeoutReset(t *testing.T) {
rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager)
rm := New(container.RestartPolicy{Name: "always"}, 0)
rm.timeout = 5 * time.Second
var duration = 10 * time.Second
_, _, err := rm.ShouldRestart(0, false, duration)