Golang: How to use Ticker and Timer
Deployment platforms like Render have an issue, in the free instance they spin down with inactivity, which can delay requests by 50 seconds or more.
To keep your application running always you can do make a work around.
Ticker and Timer are some packages in Golang can help us keep our application running forever. Let’s discuss in details what can be use
But what is Ticker?
Ticker is a mechanism provided by time package. It sends a signal via a channel on a regular interval. Now how can we benefit from this?
We can put a ticker and after a gap of every 30 seconds hit on a endpoint of our application.
How to intialise a ticker?
Like other data structures in Golang, ticker is also initialised via a simple definition.
ticker := time.NewTicker(500 * time.Millisecond)
But how to write the code for this? To write the code we must know our requirements:-
1. We want our application to be running in background independently: what’s better than Goroutines?
2. We want our application to keep running forever in the background: What’s better than never ending For loop?