2018-02-05 16:05:59 -05:00
|
|
|
package restartmanager // import "github.com/docker/docker/restartmanager"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
import (
|
2016-04-18 14:48:28 -04:00
|
|
|
"errors"
|
2016-03-18 14:50:19 -04:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
backoffMultiplier = 2
|
|
|
|
defaultTimeout = 100 * time.Millisecond
|
2017-02-02 13:19:17 -05:00
|
|
|
maxRestartTimeout = 1 * time.Minute
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
2016-04-18 14:48:28 -04:00
|
|
|
// ErrRestartCanceled is returned when the restart manager has been
|
|
|
|
// canceled and will no longer restart the container.
|
|
|
|
var ErrRestartCanceled = errors.New("restart canceled")
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
// RestartManager defines object that controls container restarting rules.
|
|
|
|
type RestartManager interface {
|
|
|
|
Cancel() error
|
2016-04-18 16:10:15 -04:00
|
|
|
ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type restartManager struct {
|
|
|
|
sync.Mutex
|
|
|
|
sync.Once
|
|
|
|
policy container.RestartPolicy
|
2016-03-22 11:46:40 -04:00
|
|
|
restartCount int
|
2016-03-18 14:50:19 -04:00
|
|
|
timeout time.Duration
|
|
|
|
active bool
|
|
|
|
cancel chan struct{}
|
|
|
|
canceled bool
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:10:49 -05:00
|
|
|
// New returns a new restartManager based on a policy.
|
2016-03-22 11:46:40 -04:00
|
|
|
func New(policy container.RestartPolicy, restartCount int) RestartManager {
|
|
|
|
return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *restartManager) SetPolicy(policy container.RestartPolicy) {
|
|
|
|
rm.Lock()
|
|
|
|
rm.policy = policy
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:10:15 -04:00
|
|
|
func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error) {
|
2016-04-14 21:40:20 -04:00
|
|
|
if rm.policy.IsNone() {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.Lock()
|
|
|
|
unlockOnExit := true
|
|
|
|
defer func() {
|
|
|
|
if unlockOnExit {
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if rm.canceled {
|
2016-04-18 14:48:28 -04:00
|
|
|
return false, nil, ErrRestartCanceled
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rm.active {
|
2016-11-24 09:10:49 -05:00
|
|
|
return false, nil, fmt.Errorf("invalid call on an active restart manager")
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2016-06-21 03:28:30 -04:00
|
|
|
// if the container ran for more than 10s, regardless of status and policy reset the
|
2016-04-18 16:10:15 -04:00
|
|
|
// the timeout back to the default.
|
|
|
|
if executionDuration.Seconds() >= 10 {
|
|
|
|
rm.timeout = 0
|
|
|
|
}
|
2017-02-02 13:19:17 -05:00
|
|
|
switch {
|
|
|
|
case rm.timeout == 0:
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.timeout = defaultTimeout
|
2017-02-02 13:19:17 -05:00
|
|
|
case rm.timeout < maxRestartTimeout:
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.timeout *= backoffMultiplier
|
|
|
|
}
|
2017-02-02 13:19:17 -05:00
|
|
|
if rm.timeout > maxRestartTimeout {
|
|
|
|
rm.timeout = maxRestartTimeout
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
var restart bool
|
|
|
|
switch {
|
2016-03-22 11:46:40 -04:00
|
|
|
case rm.policy.IsAlways():
|
|
|
|
restart = true
|
|
|
|
case rm.policy.IsUnlessStopped() && !hasBeenManuallyStopped:
|
2016-03-18 14:50:19 -04:00
|
|
|
restart = true
|
|
|
|
case rm.policy.IsOnFailure():
|
|
|
|
// the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count
|
2016-03-22 11:46:40 -04:00
|
|
|
if max := rm.policy.MaximumRetryCount; max == 0 || rm.restartCount < max {
|
2016-03-18 14:50:19 -04:00
|
|
|
restart = exitCode != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !restart {
|
|
|
|
rm.active = false
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-22 11:46:40 -04:00
|
|
|
rm.restartCount++
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
unlockOnExit = false
|
|
|
|
rm.active = true
|
|
|
|
rm.Unlock()
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
2019-01-09 13:24:03 -05:00
|
|
|
timeout := time.NewTimer(rm.timeout)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
select {
|
|
|
|
case <-rm.cancel:
|
2016-04-18 14:48:28 -04:00
|
|
|
ch <- ErrRestartCanceled
|
2016-03-18 14:50:19 -04:00
|
|
|
close(ch)
|
2019-01-09 13:24:03 -05:00
|
|
|
case <-timeout.C:
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.Lock()
|
|
|
|
close(ch)
|
|
|
|
rm.active = false
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return true, ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *restartManager) Cancel() error {
|
|
|
|
rm.Do(func() {
|
|
|
|
rm.Lock()
|
|
|
|
rm.canceled = true
|
|
|
|
close(rm.cancel)
|
|
|
|
rm.Unlock()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|