In C# when we want to cause delay between the execution of two procedures we have two options:
Thread.Sleep(time)
which is blockingawait Task.Delay(time)
which is non-blocking
In other words, Thread.Sleep(time)
blocks the current thread for the specified time while await Task.Delay(time)
suspends the executing
thread (allowing the OS thread scheduler to select and run more threads) and then it resumes it after the specified time.
In Go, there is time.Sleep(time)
method which is known to be a blocking method. Being new to Go, I have this question, assuming I have a thousand of go routines (since go has it's own thread scheduler instead of relying on the OS scheduler, this should not be a problem) and I would like to implement a retry pattern in each one of these go routines in such a way that:
FOR 5 Times {
IF Request Fails
THEN
time.Sleep(time.Second * 30)
ELSE
RETURN
}
Would it be safe to use time.Sleep(time)
in this case? Or is there a better way?
Edit:
I am not asking if time.Sleep(time)
is blocking!! I have no I idea why my question is duplicate of that post!