influxdata / influxdb-client-go

InfluxDB 2 Go Client
MIT License
609 stars 116 forks source link

In the continuous data query, there will often be a timeout, and then the service will exit directly. Do you have any good suggestions, thank you! #329

Closed bufanyun closed 2 years ago

bufanyun commented 2 years ago

In the continuous data query, there will often be a timeout, and then the service will exit directly. Do you have any good suggestions, thank you!

panic: Post "http://127.0.0.1:8086/api/v2/query?org=user": context deadline exceeded (Client.Timeout exceeded while awaiting headers) [recovered]
    panic: exception recovered: Post "http://127.0.0.1:8086/api/v2/query?org=user": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
bufanyun commented 2 years ago

If you re-run the service after the timeout, it will be normal, and checking the status of influxdb is also running

vlastahajek commented 2 years ago

@bufanyun, if I understood correctly, by continuous data query you mean a loop in which you are repeatedly sending a query request to a server, right? If yes, the query can take a long time.

What you can do: 1) Increase timeout. Default timeout is 20s. You can increase it via options:

opts := influxdb2.DefaultOptions().SetHTTPRequestTimeout(40)
client := influxdb2.NewClientWithOptions("htp://localhost:8086", authToken, opts)

2) Don't panic on errors. Calling panic is part of the examples, but it is not recommended in production code. Good practice is retrying a request on an error. You can also add simple backoff loop:

for _, tm := range []time.Duration{2, 5, 10} {
    result, err := queryAPI.Query(context.Background(), query)
    if err == nil {
        // process result
        break
    } else {
        // wait a bit
        <-time.After(tm*time.Second)
    }
}
bufanyun commented 2 years ago

Well said, thank you so much!