discovery/consul: close idle connections on stop

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-08-01 11:11:41 +02:00
parent 70c98a06f1
commit 1cd29f782c
1 changed files with 7 additions and 3 deletions

View File

@ -147,7 +147,6 @@ func init() {
// and updates them via watches. // and updates them via watches.
type Discovery struct { type Discovery struct {
client *consul.Client client *consul.Client
clientConf *consul.Config
clientDatacenter string clientDatacenter string
tagSeparator string tagSeparator string
watchedServices []string // Set of services which will be discovered. watchedServices []string // Set of services which will be discovered.
@ -155,6 +154,7 @@ type Discovery struct {
watchedNodeMeta map[string]string watchedNodeMeta map[string]string
allowStale bool allowStale bool
refreshInterval time.Duration refreshInterval time.Duration
finalizer func()
logger log.Logger logger log.Logger
} }
@ -169,6 +169,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
return nil, err return nil, err
} }
transport := &http.Transport{ transport := &http.Transport{
IdleConnTimeout: 5 * time.Duration(conf.RefreshInterval),
TLSClientConfig: tls, TLSClientConfig: tls,
DialContext: conntrack.NewDialContextFunc( DialContext: conntrack.NewDialContextFunc(
conntrack.DialWithTracing(), conntrack.DialWithTracing(),
@ -197,14 +198,14 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
} }
cd := &Discovery{ cd := &Discovery{
client: client, client: client,
clientConf: clientConf,
tagSeparator: conf.TagSeparator, tagSeparator: conf.TagSeparator,
watchedServices: conf.Services, watchedServices: conf.Services,
watchedTag: conf.ServiceTag, watchedTag: conf.ServiceTag,
watchedNodeMeta: conf.NodeMeta, watchedNodeMeta: conf.NodeMeta,
allowStale: conf.AllowStale, allowStale: conf.AllowStale,
refreshInterval: time.Duration(conf.RefreshInterval), refreshInterval: time.Duration(conf.RefreshInterval),
clientDatacenter: clientConf.Datacenter, clientDatacenter: conf.Datacenter,
finalizer: transport.CloseIdleConnections,
logger: logger, logger: logger,
} }
return cd, nil return cd, nil
@ -298,6 +299,9 @@ func (d *Discovery) initialize(ctx context.Context) {
// Run implements the Discoverer interface. // Run implements the Discoverer interface.
func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
if d.finalizer != nil {
defer d.finalizer()
}
d.initialize(ctx) d.initialize(ctx)
if len(d.watchedServices) == 0 || d.watchedTag != "" { if len(d.watchedServices) == 0 || d.watchedTag != "" {