influxdb-client-go: Client is leaking TCP connections
I discovered this issue with the grafana flux datasource, but traced it back to the client. It seems TCP connections are not closed after calling client.Close().
Output of the program below:
...
Query returned 181 rows
Open TCP connections: 59
Query returned 181 rows
Open TCP connections: 60
Query returned 181 rows
Open TCP connections: 61
Query returned 181 rows
Open TCP connections: 62
...
Please run this go program to see for yourself (when on linux, change .8086 to :8086 for the netstat command).
package main
import (
"context"
"fmt"
"log"
"os/exec"
influxdb2 "github.com/influxdata/influxdb-client-go"
)
func main() {
for {
client := influxdb2.NewClient("https://demo.factry.io:8086", "flux:xulf")
queryAPI := client.QueryAPI("factry")
query := `from(bucket: "simulation_tanksystem")
|> range(start: -2h, stop:-30m)
|> filter(fn: (r) => r._measurement == "WP1_fill_level")
|> aggregateWindow(every: 30s, timeSrc: "_start", fn: mean, createEmpty:false)`
// get parser flux query result
result, err := queryAPI.Query(context.Background(), query)
if err != nil {
fmt.Println(err)
return
}
defer result.Close()
count := 0
// Use Next() to iterate over query result lines
for result.Next() {
// Observe when there is new grouping key producing new table
count++
}
fmt.Printf("Query returned %v rows\n", count)
if result.Err() != nil {
fmt.Printf("Query error: %s\n", result.Err().Error())
}
// Ensures background processes finishes
client.Close()
getOpenConns()
}
}
func getOpenConns() {
out, err := exec.Command("sh", "-c", `netstat -atn | grep ".8086" | wc -l`).Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Open TCP connections: %s", out)
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 31 (10 by maintainers)
Commits related to this issue
- fix: Closing body at the end of the result (#183) — committed to bonitoo-io/influxdb-client-go by vlastahajek 4 years ago
- fix: Client.Close() also closes idle connection if underlying HTTP client was created internally (#183) — committed to bonitoo-io/influxdb-client-go by vlastahajek 4 years ago
@coussej @aknuds1 thank you for the discussion herein. I would end up with the following results:
Thank you @coussej, the ESTABLISHED connections are caused by (3) in https://github.com/influxdata/influxdb-client-go/issues/183#issuecomment-675394837 . It is the reason for keeping this issue open, it can be fixed.
@aknuds1, yes. The library does all what’s needed for proper handling of connections, as mentioned in the net/http docs:
@aknuds1 yes, it was the root cause. Your code in grafana handles the situation the best way possible. The code in this client seems to close everything properly from the go’s POV, I don’t think that we can force-close the socket.
@aknuds1 I am able to reproduce this on Ubuntu, the extra sockets that increase the number are in TIMED-WAIT state:
This is AFAIK a normal state that depends on OS (https://knowledgebase.progress.com/articles/Article/Can-the-time-a-socket-spends-in-TIMED-WAIT-state-be-reduced). The sockets remain in TIMED-WAIT state for an additional 60 seconds in my case:
After 60 seconds, the count of sockets returns to an initial state.
The code should better reuse clients to avoid this issue. https://stackoverflow.com/questions/39813587/go-client-program-generates-a-lot-a-sockets-in-time-wait-state might also provide a solution for you.
I will fix that, however, I’m on Windows 10 and I’m not able to reproduce that. Go 1.4, InfluxDB 1.8.1, InfluxDB 2 beta 16