restartmanager.go 3.0 KB

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