Timeout after 30 seconds when posting usage analytics (#1577)

This commit is contained in:
Tomas Touceda 2021-08-06 13:20:59 -03:00 committed by GitHub
parent 084fcdfec4
commit e63e690fdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -0,0 +1 @@
* When posting usage analytics, timeout after 30secs.

View file

@ -426,12 +426,18 @@ func trySendStatistics(ds fleet.Datastore, frequency time.Duration, url string)
if err != nil {
return err
}
req, err := http.Post(url, "application/json", bytes.NewBuffer(statsBytes))
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(statsBytes))
if err != nil {
return err
}
if req.StatusCode != http.StatusOK {
return errors.Errorf("Error posting to %s: %d", url, req.StatusCode)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("Error posting to %s: %d", url, resp.StatusCode)
}
return ds.RecordStatisticsSent()
}