Using a for loop : Manually code the iteration yourself.
The simplest way to poll in golang is to do it in a for loop like this...
The above methodoloy is obvious - we simply create a for loop with a "sleep" condition. However there is something annoying here. What if we want to actually add a timeout ? Well.. then we have to have a different if/else construct for the timeout...
Using Channels : go to sleep until a timeout trips a channel io.
So, a more direct way to accomplish the same polling is to create two channels, one for a timeout, the other for incremental updates. NOTE This is a different code snippet, but it should be clear how the strategy is different. The key thing to note is that our for loop has a select block with cases in it. And those cases just wait on channels that we create above (see deletionStart and tick).
Using wait.Poll() : use higher level polling construct and just write a boolean function
Finally, quinton-hoole has showed me there is yet a third way to do this sort of polling - use the explicit poll method.



No comments:
Post a Comment