Timeout a Function Call (with Goroutines & Channels)
·
223 words
·
2 minutes read
Some applications and programs can be very time sensitive - and they often need to return something in a timely fashion. However, it’s not always within our control to set a cut off point these operations. Go makes this process somewhat easier though through it’s use of goroutines and channels.
In the example below we execute LongRunningProcess
which we’ve given 3 seconds complete - but it contains code to sleep for 5, so it will never complete. We manage this by using a select
to listen on multiple channels, one we’ve created for our function and another one for our timeout.
|
|