restartmanager.go 2.9 KB

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