mirror of https://github.com/docker/docs.git
Merge pull request #1263 from aluzzardi/refresh-loop
engine: More robust refresh loop.
This commit is contained in:
commit
fe655270b9
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -20,7 +21,9 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Force-refresh the state of the engine this often.
|
// Force-refresh the state of the engine this often.
|
||||||
stateRefreshPeriod = 30 * time.Second
|
stateRefreshMinRange = 30
|
||||||
|
stateRefreshMaxRange = 60
|
||||||
|
stateRefreshRetries = 3
|
||||||
|
|
||||||
// Timeout for requests sent out to the engine.
|
// Timeout for requests sent out to the engine.
|
||||||
requestTimeout = 10 * time.Second
|
requestTimeout = 10 * time.Second
|
||||||
|
@ -29,6 +32,10 @@ const (
|
||||||
minSupportedVersion = version.Version("1.6.0")
|
minSupportedVersion = version.Version("1.6.0")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(int64(time.Now().Nanosecond()))
|
||||||
|
}
|
||||||
|
|
||||||
// NewEngine is exported
|
// NewEngine is exported
|
||||||
func NewEngine(addr string, overcommitRatio float64) *Engine {
|
func NewEngine(addr string, overcommitRatio float64) *Engine {
|
||||||
e := &Engine{
|
e := &Engine{
|
||||||
|
@ -336,12 +343,16 @@ func (e *Engine) updateContainer(c dockerclient.Container, containers map[string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) refreshLoop() {
|
func (e *Engine) refreshLoop() {
|
||||||
|
failedAttempts := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
refreshPeriod := time.Duration(rand.Intn(stateRefreshMaxRange-stateRefreshMinRange) + stateRefreshMinRange)
|
||||||
|
|
||||||
// Sleep stateRefreshPeriod or quit if we get stopped.
|
// Sleep stateRefreshPeriod or quit if we get stopped.
|
||||||
select {
|
select {
|
||||||
case <-time.After(stateRefreshPeriod):
|
case <-time.After(refreshPeriod * time.Second):
|
||||||
case <-e.stopCh:
|
case <-e.stopCh:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -354,14 +365,17 @@ func (e *Engine) refreshLoop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e.healthy {
|
failedAttempts++
|
||||||
e.emitEvent("engine_disconnect")
|
if failedAttempts >= stateRefreshRetries {
|
||||||
|
if e.healthy {
|
||||||
|
e.emitEvent("engine_disconnect")
|
||||||
|
}
|
||||||
|
e.healthy = false
|
||||||
|
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Flagging engine as dead. Updated state failed %d times: %v", failedAttempts, err)
|
||||||
}
|
}
|
||||||
e.healthy = false
|
|
||||||
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Flagging engine as dead. Updated state failed: %v", err)
|
|
||||||
} else {
|
} else {
|
||||||
if !e.healthy {
|
if !e.healthy {
|
||||||
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Info("Engine came back to life. Hooray!")
|
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Info("Engine came back to life after %d retries. Hooray!", failedAttempts)
|
||||||
if err := e.updateSpecs(); err != nil {
|
if err := e.updateSpecs(); err != nil {
|
||||||
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Update engine specs failed: %v", err)
|
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Update engine specs failed: %v", err)
|
||||||
continue
|
continue
|
||||||
|
@ -371,6 +385,7 @@ func (e *Engine) refreshLoop() {
|
||||||
e.emitEvent("engine_reconnect")
|
e.emitEvent("engine_reconnect")
|
||||||
}
|
}
|
||||||
e.healthy = true
|
e.healthy = true
|
||||||
|
failedAttempts = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue