restartmanager.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package restartmanager
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "time"
  7. "github.com/docker/engine-api/types/container"
  8. )
  9. const (
  10. backoffMultiplier = 2
  11. defaultTimeout = 100 * time.Millisecond
  12. )
  13. // ErrRestartCanceled is returned when the restart manager has been
  14. // canceled and will no longer restart the container.
  15. var ErrRestartCanceled = errors.New("restart canceled")
  16. // RestartManager defines object that controls container restarting rules.
  17. type RestartManager interface {
  18. Cancel() error
  19. ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error)
  20. }
  21. type restartManager struct {
  22. sync.Mutex
  23. sync.Once
  24. policy container.RestartPolicy
  25. restartCount int
  26. timeout time.Duration
  27. active bool
  28. cancel chan struct{}
  29. canceled bool
  30. }
  31. // New returns a new restartmanager based on a policy.
  32. func New(policy container.RestartPolicy, restartCount int) RestartManager {
  33. return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
  34. }
  35. func (rm *restartManager) SetPolicy(policy container.RestartPolicy) {
  36. rm.Lock()
  37. rm.policy = policy
  38. rm.Unlock()
  39. }
  40. func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool, executionDuration time.Duration) (bool, chan error, error) {
  41. if rm.policy.IsNone() {
  42. return false, nil, nil
  43. }
  44. rm.Lock()
  45. unlockOnExit := true
  46. defer func() {
  47. if unlockOnExit {
  48. rm.Unlock()
  49. }
  50. }()
  51. if rm.canceled {
  52. return false, nil, ErrRestartCanceled
  53. }
  54. if rm.active {
  55. return false, nil, fmt.Errorf("invalid call on active restartmanager")
  56. }
  57. // if the container ran for more than 10s, regardless of status and policy reset the
  58. // the timeout back to the default.
  59. if executionDuration.Seconds() >= 10 {
  60. rm.timeout = 0
  61. }
  62. if rm.timeout == 0 {
  63. rm.timeout = defaultTimeout
  64. } else {
  65. rm.timeout *= backoffMultiplier
  66. }
  67. var restart bool
  68. switch {
  69. case rm.policy.IsAlways():
  70. restart = true
  71. case rm.policy.IsUnlessStopped() && !hasBeenManuallyStopped:
  72. restart = true
  73. case rm.policy.IsOnFailure():
  74. // the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count
  75. if max := rm.policy.MaximumRetryCount; max == 0 || rm.restartCount < max {
  76. restart = exitCode != 0
  77. }
  78. }
  79. if !restart {
  80. rm.active = false
  81. return false, nil, nil
  82. }
  83. rm.restartCount++
  84. unlockOnExit = false
  85. rm.active = true
  86. rm.Unlock()
  87. ch := make(chan error)
  88. go func() {
  89. select {
  90. case <-rm.cancel:
  91. ch <- ErrRestartCanceled
  92. close(ch)
  93. case <-time.After(rm.timeout):
  94. rm.Lock()
  95. close(ch)
  96. rm.active = false
  97. rm.Unlock()
  98. }
  99. }()
  100. return true, ch, nil
  101. }
  102. func (rm *restartManager) Cancel() error {
  103. rm.Do(func() {
  104. rm.Lock()
  105. rm.canceled = true
  106. close(rm.cancel)
  107. rm.Unlock()
  108. })
  109. return nil
  110. }