diff --git a/container/container.go b/container/container.go index dada1dc980..37a6a74c1c 100644 --- a/container/container.go +++ b/container/container.go @@ -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) } diff --git a/restartmanager/restartmanager.go b/restartmanager/restartmanager.go index f7a547d2d1..43738454fc 100644 --- a/restartmanager/restartmanager.go +++ b/restartmanager/restartmanager.go @@ -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 diff --git a/restartmanager/restartmanager_test.go b/restartmanager/restartmanager_test.go index 82558946bc..3dbd37c536 100644 --- a/restartmanager/restartmanager_test.go +++ b/restartmanager/restartmanager_test.go @@ -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)