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 for closing
LogDriver logger.Logger `json:"-"` LogDriver logger.Logger `json:"-"`
LogCopier *logger.Copier `json:"-"` LogCopier *logger.Copier `json:"-"`
restartManager restartmanager.RestartManager restartManager *restartmanager.RestartManager
attachContext *attachContext attachContext *attachContext
// Fields here are specific to Unix platforms // 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. // 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 { if container.restartManager == nil {
container.restartManager = restartmanager.New(container.HostConfig.RestartPolicy, container.RestartCount) container.restartManager = restartmanager.New(container.HostConfig.RestartPolicy, container.RestartCount)
} }

View file

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

View file

@ -8,7 +8,7 @@ import (
) )
func TestRestartManagerTimeout(t *testing.T) { 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 var duration = 1 * time.Second
should, _, err := rm.ShouldRestart(0, false, duration) should, _, err := rm.ShouldRestart(0, false, duration)
if err != nil { if err != nil {
@ -23,7 +23,7 @@ func TestRestartManagerTimeout(t *testing.T) {
} }
func TestRestartManagerTimeoutReset(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 rm.timeout = 5 * time.Second
var duration = 10 * time.Second var duration = 10 * time.Second
_, _, err := rm.ShouldRestart(0, false, duration) _, _, err := rm.ShouldRestart(0, false, duration)