Merge pull request #42264 from thaJeztah/update_the_update

restartmanager:  Remove RestartManager interface, and unused error return
This commit is contained in:
Sebastiaan van Stijn 2023-01-10 11:40:36 +01:00 committed by GitHub
commit d73aba2500
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 24 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
@ -557,13 +557,7 @@ func (container *Container) InitDNSHostConfig() {
// UpdateMonitor updates monitor configure for running container
func (container *Container) UpdateMonitor(restartPolicy containertypes.RestartPolicy) {
type policySetter interface {
SetPolicy(containertypes.RestartPolicy)
}
if rm, ok := container.RestartManager().(policySetter); ok {
rm.SetPolicy(restartPolicy)
}
container.RestartManager().SetPolicy(restartPolicy)
}
// FullHostname returns hostname and optional domain appended to it.
@ -576,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,12 +20,7 @@ const (
var ErrRestartCanceled = errors.New("restart canceled")
// RestartManager defines object that controls container restarting rules.
type RestartManager interface {
Cancel() error
ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error)
}
type restartManager struct {
type RestartManager struct {
sync.Mutex
sync.Once
policy container.RestartPolicy
@ -36,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
}
@ -125,12 +122,12 @@ func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped
return true, ch, nil
}
func (rm *restartManager) Cancel() error {
// Cancel tells the RestartManager to no longer restart the container.
func (rm *RestartManager) Cancel() {
rm.Do(func() {
rm.Lock()
rm.canceled = true
close(rm.cancel)
rm.Unlock()
})
return nil
}

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)