traefik/provider/eureka/eureka.go

80 lines
2.1 KiB
Go
Raw Normal View History

package eureka
2016-08-31 20:43:05 +00:00
import (
"time"
2016-08-31 20:43:05 +00:00
"github.com/cenk/backoff"
"github.com/containous/traefik/job"
2017-06-13 23:22:14 +00:00
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
2016-08-31 20:43:05 +00:00
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
)
// Provider holds configuration of the Provider provider.
type Provider struct {
2017-10-02 08:32:02 +00:00
provider.BaseProvider `mapstructure:",squash" export:"true"`
Endpoint string `description:"Eureka server endpoint"`
2017-10-02 08:32:02 +00:00
Delay string `description:"Override default configuration time between refresh" export:"true"`
2016-08-31 20:43:05 +00:00
}
// Provide allows the eureka provider to provide configurations to traefik
2016-08-31 20:43:05 +00:00
// using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ types.Constraints) error {
2016-08-31 20:43:05 +00:00
operation := func() error {
configuration, err := p.buildConfiguration()
2016-08-31 20:43:05 +00:00
if err != nil {
log.Errorf("Failed to build configuration for Provider, error: %s", err)
2016-08-31 20:43:05 +00:00
return err
}
configurationChan <- types.ConfigMessage{
ProviderName: "eureka",
Configuration: configuration,
}
var delay time.Duration
if len(p.Delay) > 0 {
2016-08-31 20:43:05 +00:00
var err error
delay, err = time.ParseDuration(p.Delay)
2016-08-31 20:43:05 +00:00
if err != nil {
log.Errorf("Failed to parse delay for Provider, error: %s", err)
2016-08-31 20:43:05 +00:00
return err
}
} else {
delay = time.Second * 30
}
ticker := time.NewTicker(delay)
safe.Go(func() {
2016-08-31 20:43:05 +00:00
for t := range ticker.C {
log.Debugf("Refreshing Provider %s", t.String())
2016-08-31 20:43:05 +00:00
configuration, err := p.buildConfiguration()
2016-08-31 20:43:05 +00:00
if err != nil {
log.Errorf("Failed to refresh Provider configuration, error: %s", err)
2016-08-31 20:43:05 +00:00
return
}
configurationChan <- types.ConfigMessage{
ProviderName: "eureka",
Configuration: configuration,
}
}
})
2016-08-31 20:43:05 +00:00
return nil
}
notify := func(err error, time time.Duration) {
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
2016-08-31 20:43:05 +00:00
}
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
if err != nil {
log.Errorf("Cannot connect to Provider server %+v", err)
2016-08-31 20:43:05 +00:00
return err
}
return nil
}