Hello,
I’m writing simple parser for storing data from websocket to Influx 2.0 using Go. And everything seemed to be fine till I noticed that every time I use WritePoint method two new goroutines are spawned and they are not finished even after client.Close(). The effect is that after some time I have hundreds of goroutines.
Here is a part of code:
sublogger.Debug().Msgf("Goroutines before client instace: %d", runtime.NumGoroutine())`
client := influxdb2.NewClient(influxdb2URL, influxToken)
sublogger.Debug().Msgf("Goroutines after client instace: %d", runtime.NumGoroutine())
defer func() {
sublogger.Debug().Msgf("Goroutines before client close: %d", runtime.NumGoroutine())
client.Close()
sublogger.Debug().Msgf("Goroutines on exit: %d", runtime.NumGoroutine())
}()
sublogger.Debug().Msgf("Goroutines before writeApi instance: %d", runtime.NumGoroutine())
writeAPI := client.WriteAPIBlocking(influxOrg, "ISA_data")
sublogger.Debug().Msgf("Goroutines after writeApi instance: %d", runtime.NumGoroutine())
stateDataPoint := influxdb2.NewPointWithMeasurement("raw_state1")
stateDataPoint.AddTag("ISID", rtu.ISID)
stateDataPoint.AddTag("core_id", event.Data.CoreID)
stateDataPoint.SetTime(event.Data.Timestamp)
for idx, reg := range framemap.State.Variable {
rawValue := string(rtu.State[(reg.StartByte)*2-1 : (reg.StartByte)*2-1+4])
decodedValue, err := strconv.ParseInt(rawValue, 16, 16)
if err != nil {
sublogger.Error().Str("event", "RTUValueDecode").Str("eventData", event.Data.Data).Err(err).Msg("")
return
}
if framemap.State.Variable[idx].Factor != 1 {
framemap.State.Variable[idx].DecodedValue = float64(decodedValue) / float64(framemap.State.Variable[idx].Factor)
} else {
framemap.State.Variable[idx].DecodedValue = decodedValue
}
framemap.State.Variable[idx].DecodedValue)
stateDataPoint.AddField(framemap.State.Variable[idx].FieldKey, framemap.State.Variable[idx].DecodedValue)
}
sublogger.Debug().Msgf("Goroutines before write: %d", runtime.NumGoroutine())
err = writeAPI.WritePoint(context.Background(), stateDataPoint)
if err != nil {
sublogger.Error().Err(err).Msg("")
}
sublogger.Debug().Msgf("Goroutines after write: %d", runtime.NumGoroutine())
And here is log:
"Goroutines before client instace: 5"
"Goroutines after client instace: 5"
"Goroutines before writeApi instance: 5"
"Goroutines after writeApi instance: 5"
"Goroutines before write: 5"
"Goroutines after write: 7"
"Goroutines before client close: 7"
"Goroutines on exit: 7"
Next time I start this function number of goroutines will be 7 and two more will be spwned and so on.
Same situation is with asynchronous write.
If I use debuger there are more and more of these net/http and internal/pool:
So am I doing something wrong or influx-client is not closing the connection?
Thanks for help in advance.