Fix data races.

This commit is contained in:
Tristan Colgate-McFarlane 2018-01-15 10:46:04 +00:00 committed by Traefiker
parent 9790aa91fe
commit bcadd68904
2 changed files with 7 additions and 4 deletions

View file

@ -19,9 +19,9 @@ type Handler struct {
Dashboard bool `description:"Activate dashboard" export:"true"`
Debug bool `export:"true"`
CurrentConfigurations *safe.Safe
Statistics *types.Statistics `description:"Enable more detailed statistics" export:"true"`
Stats *thoas_stats.Stats
StatsRecorder *middlewares.StatsRecorder
Statistics *types.Statistics `description:"Enable more detailed statistics" export:"true"`
Stats *thoas_stats.Stats `json:"-"`
StatsRecorder *middlewares.StatsRecorder `json:"-"`
}
var (

View file

@ -48,6 +48,7 @@ type BackendHealthCheck struct {
//HealthCheck struct
type HealthCheck struct {
mutex sync.Mutex
Backends map[string]*BackendHealthCheck
cancel context.CancelFunc
}
@ -75,14 +76,16 @@ func NewBackendHealthCheck(options Options) *BackendHealthCheck {
//SetBackendsConfiguration set backends configuration
func (hc *HealthCheck) SetBackendsConfiguration(parentCtx context.Context, backends map[string]*BackendHealthCheck) {
hc.mutex.Lock()
hc.Backends = backends
if hc.cancel != nil {
hc.cancel()
}
ctx, cancel := context.WithCancel(parentCtx)
hc.cancel = cancel
hc.mutex.Unlock()
for backendID, backend := range hc.Backends {
for backendID, backend := range backends {
currentBackendID := backendID
currentBackend := backend
safe.Go(func() {