Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close HTTP2 connections on timeout in influxdb outputs #7517

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/subtle"
"net"
"net/http"
"net/url"
)

type BasicAuthErrorFunc func(rw http.ResponseWriter)
Expand Down Expand Up @@ -95,3 +96,13 @@ func (h *ipRangeHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

h.onError(rw, http.StatusForbidden)
}

func OnClientError(client *http.Client, err error) {
// Close connection after a timeout error. If this is a HTTP2
// connection this ensures that next interval a new connection will be
// used and name lookup will be performed.
// https://github.com/golang/go/issues/36026
if err, ok := err.(*url.Error); ok && err.Timeout() {
client.CloseIdleConnections()
}
}
15 changes: 0 additions & 15 deletions internal/http_go1.11.go

This file was deleted.

9 changes: 0 additions & 9 deletions internal/http_go1.12.go

This file was deleted.

8 changes: 5 additions & 3 deletions plugins/outputs/influxdb/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (c *httpClient) CreateDatabase(ctx context.Context, database string) error

resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
internal.OnClientError(c.client, err)
return err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -311,7 +312,7 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
}

func (c *httpClient) writeBatch(ctx context.Context, db, rp string, metrics []telegraf.Metric) error {
url, err := makeWriteURL(c.config.URL, db, rp, c.config.Consistency)
loc, err := makeWriteURL(c.config.URL, db, rp, c.config.Consistency)
if err != nil {
return err
}
Expand All @@ -322,13 +323,14 @@ func (c *httpClient) writeBatch(ctx context.Context, db, rp string, metrics []te
}
defer reader.Close()

req, err := c.makeWriteRequest(url, reader)
req, err := c.makeWriteRequest(loc, reader)
if err != nil {
return err
}

resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
internal.OnClientError(c.client, err)
return err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -505,5 +507,5 @@ func makeQueryURL(loc *url.URL) (string, error) {
}

func (c *httpClient) Close() {
internal.CloseIdleConnections(c.client)
c.client.CloseIdleConnections()
}
7 changes: 4 additions & 3 deletions plugins/outputs/influxdb_v2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
}

func (c *httpClient) writeBatch(ctx context.Context, bucket string, metrics []telegraf.Metric) error {
url, err := makeWriteURL(*c.url, c.Organization, bucket)
loc, err := makeWriteURL(*c.url, c.Organization, bucket)
if err != nil {
return err
}
Expand All @@ -221,13 +221,14 @@ func (c *httpClient) writeBatch(ctx context.Context, bucket string, metrics []te
}
defer reader.Close()

req, err := c.makeWriteRequest(url, reader)
req, err := c.makeWriteRequest(loc, reader)
if err != nil {
return err
}

resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
internal.OnClientError(c.client, err)
return err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -347,5 +348,5 @@ func makeWriteURL(loc url.URL, org, bucket string) (string, error) {
}

func (c *httpClient) Close() {
internal.CloseIdleConnections(c.client)
c.client.CloseIdleConnections()
}