There is an API that I need to periodically poll in order to check the status message.
ticker := time.NewTicker(time.Second * tickerWaitTimeSeconds)
defer ticker.Stop()
for range ticker.C {
res, err := r.client.Get(someURL)
if err != nil {
results <- err
return
}
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
results <- err
return
}
status := response.Data.Attributes.CompleteStatus
if status == "COMPLETED" {
results <- nil
return
}
if status == "ERROR" {
results <- ErrFailedJob
return
}
It has worked in quite a stable way so far, but there is one possible catch if I understand how tickers work correctly.
There is this constant tickerWaitTimeSeconds that is currently set to 2 seconds. The value was chosen in such a way that the request has enough time to succeed (it's not even close to 1 second, not to mention 2) and we don't spam the API.
I suspect, however, that if for some reason the request takes longer than tickerWaitTimeSeconds there might be more GET requests to the API than necessary.
Is my suspicion valid in this case? Probably, there is something wrong with my understanding of what really happens and the Get call blocks the ticker, but I doubt that's the case.