I am trying to write a Go application that periodically polls a REST endpoint exposed by a PHP application. The Go polling application reads the payload into a struct and does further processing. I am looking for some recommendations for starting the implementation.
Polling is the process of repeatedly hitting the same endpoint looking for new data. We don't like doing this (its wasteful), vendors don't like us doing it (again, its wasteful) and users dislike it (they have to wait a maximum interval to trigger on new data).
Polling requests are made by a client, while webhook requests are made by a server. Webhooks are also automatically triggered when an event occurs, whereas polling is set up to run at fixed intervals and runs whether there is a new event or not.
Constant polling of an endpoint is wasteful in terms of resources committed to the action from the developer, in terms of the traffic seen by the vendors, and in terms of actual result to effort.
The Polling API is used to retrieve the reporting data from a request. The Polling API endpoint will respond to successful requests with compressed gzip. The response must be uncompressed to retrieve the data.
Simplest way would be to use a Ticker:
ticker := time.NewTicker(time.Second * 1).C
go func() {
for {
select {
case <- ticker:
response,_ := http.Get("http://...")
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
response.Body.Close()
}
}
}()
time.Sleep(time.Second * 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With