Retry Library
Temporary errors or self-correcting problems are common in interfacing when working with unreliable external resources. For instance, dealing with timeouts, unreliable database and web connections, network issues etc. — these can often be resolved by retrying after a delay.
The retry library is simple to use. The retry.call()
function retries the specified function call whenever an error occurs. It takes various arguments:
func - the function to be called
argN - zero or multiple arguments for the function call
retry - number of retries
pause - pause between retries (in seconds)
errorfunc - optionally include an error function to invoke custom response error handling
funcname - optionally include a function call name to be logged for troubleshooting
The function returns either the usual response of the func called or a single error if an error occurs. An informational log is created containing the attempt and error details on each attempt.
For example, here the we are calling the function DoInsert with 10 retries and a pause of five seconds between each call:
local R, Message = retry.call{func=DoInsert, arg1=T, arg2=D, retry=10, pause=5, errorfunc=myError}
Note: By default if the function returns false it is treated as a "fatal" error and the error will be re-thrown which will (usually) stop the channel.
You can import this library to add resiliency to your interfaces. See how to Import a Library.