From 467c8b31c33078373d7edecafccf620e19bf1a33 Mon Sep 17 00:00:00 2001 From: Maxence Moutoussamy Date: Fri, 24 Jun 2022 12:34:08 +0200 Subject: [PATCH] Start polling HTTP provider at the beginning Co-authored-by: Jason Quigley --- pkg/provider/http/http.go | 65 +++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/pkg/provider/http/http.go b/pkg/provider/http/http.go index c7caf7fdb..a9e4bc111 100644 --- a/pkg/provider/http/http.go +++ b/pkg/provider/http/http.go @@ -73,39 +73,18 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe. logger := log.FromContext(ctxLog) operation := func() error { + if err := p.updateConfiguration(configurationChan); err != nil { + return err + } + ticker := time.NewTicker(time.Duration(p.PollInterval)) defer ticker.Stop() for { select { case <-ticker.C: - configData, err := p.fetchConfigurationData() - if err != nil { - return fmt.Errorf("cannot fetch configuration data: %w", err) - } - - fnvHasher := fnv.New64() - - _, err = fnvHasher.Write(configData) - if err != nil { - return fmt.Errorf("cannot hash configuration data: %w", err) - } - - hash := fnvHasher.Sum64() - if hash == p.lastConfigurationHash { - continue - } - - p.lastConfigurationHash = hash - - configuration, err := decodeConfiguration(configData) - if err != nil { - return fmt.Errorf("cannot decode configuration data: %w", err) - } - - configurationChan <- dynamic.Message{ - ProviderName: "http", - Configuration: configuration, + if err := p.updateConfiguration(configurationChan); err != nil { + return err } case <-routineCtx.Done(): @@ -126,6 +105,38 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe. return nil } +func (p *Provider) updateConfiguration(configurationChan chan<- dynamic.Message) error { + configData, err := p.fetchConfigurationData() + if err != nil { + return fmt.Errorf("cannot fetch configuration data: %w", err) + } + + fnvHasher := fnv.New64() + + if _, err = fnvHasher.Write(configData); err != nil { + return fmt.Errorf("cannot hash configuration data: %w", err) + } + + hash := fnvHasher.Sum64() + if hash == p.lastConfigurationHash { + return nil + } + + p.lastConfigurationHash = hash + + configuration, err := decodeConfiguration(configData) + if err != nil { + return fmt.Errorf("cannot decode configuration data: %w", err) + } + + configurationChan <- dynamic.Message{ + ProviderName: "http", + Configuration: configuration, + } + + return nil +} + // fetchConfigurationData fetches the configuration data from the configured endpoint. func (p *Provider) fetchConfigurationData() ([]byte, error) { res, err := p.httpClient.Get(p.Endpoint)