-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle retries and state refresh when dirty
- Loading branch information
Thibault Gilles
committed
Oct 4, 2019
1 parent
9594138
commit 556c75e
Showing
5 changed files
with
152 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,139 @@ | ||
package haproxy | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/criteo/haproxy-consul-connect/consul" | ||
"github.com/criteo/haproxy-consul-connect/haproxy/state" | ||
"github.com/criteo/haproxy-consul-connect/lib" | ||
log "github.com/sirupsen/logrus" | ||
"gopkg.in/d4l3k/messagediff.v1" | ||
) | ||
|
||
func (h *HAProxy) handleChange(cfg consul.Config) error { | ||
log.Info("handling new configuration") | ||
|
||
newState, err := state.Generate(state.Options{ | ||
EnableIntentions: h.opts.EnableIntentions, | ||
LogRequests: h.opts.LogRequests, | ||
LogSocket: h.haConfig.LogsSock, | ||
SPOEConfigPath: h.haConfig.SPOE, | ||
SPOESocket: h.haConfig.SPOESock, | ||
}, h.haConfig, h.oldState, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
func (h *HAProxy) watch(sd *lib.Shutdown) error { | ||
throttle := time.Tick(50 * time.Millisecond) | ||
currentState := state.State{} | ||
nextState := &atomic.Value{} | ||
next := make(chan struct{}, 1) | ||
dirty := false | ||
|
||
tx := h.dataplaneClient.Tnx() | ||
go func() { | ||
for c := range h.cfgC { | ||
select { | ||
case <-sd.Stop: | ||
return | ||
default: | ||
} | ||
|
||
log.Debugf("applying new state: %+v", newState) | ||
log.Info("received consul config update") | ||
nextState.Store(c) | ||
h.currentConsulConfig = &c | ||
select { | ||
case next <- struct{}{}: | ||
default: | ||
} | ||
} | ||
}() | ||
|
||
err = state.Apply(tx, h.oldState, newState) | ||
if err != nil { | ||
return err | ||
} | ||
go func() { | ||
for range time.Tick(5 * time.Minute) { | ||
select { | ||
case <-sd.Stop: | ||
return | ||
default: | ||
} | ||
|
||
dirty = true | ||
} | ||
}() | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return err | ||
retry := func() { | ||
time.Sleep(3 * time.Second) | ||
select { | ||
case next <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
h.oldState = newState | ||
h.currentCfg = &cfg | ||
started := false | ||
for { | ||
for { | ||
select { | ||
case <-sd.Stop: | ||
return nil | ||
case <-next: | ||
} | ||
|
||
<-throttle | ||
|
||
log.Info("handling new configuration") | ||
if !started { | ||
err := h.start(sd) | ||
if err != nil { | ||
return err | ||
} | ||
started = true | ||
close(h.Ready) | ||
} | ||
|
||
if dirty { | ||
log.Info("refreshing haproxy state") | ||
fromHa, err := state.FromHAProxy(h.dataplaneClient) | ||
if err != nil { | ||
log.Errorf("error retrieving haproxy conf: %s", err) | ||
retry() | ||
continue | ||
} | ||
diff, equal := messagediff.PrettyDiff(currentState, fromHa) | ||
if !equal { | ||
log.Errorf("diff found between expected state and haproxy state: %s", diff) | ||
} | ||
currentState = fromHa | ||
dirty = false | ||
} | ||
|
||
newConsulCfg := nextState.Load().(consul.Config) | ||
|
||
log.Info("state successfully applied") | ||
newState, err := state.Generate(state.Options{ | ||
EnableIntentions: h.opts.EnableIntentions, | ||
LogRequests: h.opts.LogRequests, | ||
LogSocket: h.haConfig.LogsSock, | ||
SPOEConfigPath: h.haConfig.SPOE, | ||
SPOESocket: h.haConfig.SPOESock, | ||
}, h.haConfig, currentState, newConsulCfg) | ||
if err != nil { | ||
log.Error(err) | ||
retry() | ||
continue | ||
} | ||
|
||
return nil | ||
if currentState.Equal(newState) { | ||
log.Info("no change to apply to haproxy") | ||
continue | ||
} | ||
|
||
tx := h.dataplaneClient.Tnx() | ||
|
||
log.Debugf("applying new state: %+v", newState) | ||
|
||
err = state.Apply(tx, currentState, newState) | ||
if err != nil { | ||
log.Error(err) | ||
retry() | ||
continue | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
log.Error(err) | ||
retry() | ||
continue | ||
} | ||
|
||
currentState = newState | ||
|
||
log.Info("state applied") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters