Start polling HTTP provider at the beginning
Co-authored-by: Jason Quigley <jason@onecha.net>
This commit is contained in:
parent
f8f685193d
commit
467c8b31c3
1 changed files with 38 additions and 27 deletions
|
@ -73,39 +73,18 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
|
||||||
logger := log.FromContext(ctxLog)
|
logger := log.FromContext(ctxLog)
|
||||||
|
|
||||||
operation := func() error {
|
operation := func() error {
|
||||||
|
if err := p.updateConfiguration(configurationChan); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(p.PollInterval))
|
ticker := time.NewTicker(time.Duration(p.PollInterval))
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
configData, err := p.fetchConfigurationData()
|
if err := p.updateConfiguration(configurationChan); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-routineCtx.Done():
|
case <-routineCtx.Done():
|
||||||
|
@ -126,6 +105,38 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
|
||||||
return nil
|
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.
|
// fetchConfigurationData fetches the configuration data from the configured endpoint.
|
||||||
func (p *Provider) fetchConfigurationData() ([]byte, error) {
|
func (p *Provider) fetchConfigurationData() ([]byte, error) {
|
||||||
res, err := p.httpClient.Get(p.Endpoint)
|
res, err := p.httpClient.Get(p.Endpoint)
|
||||||
|
|
Loading…
Reference in a new issue