![]() ![]() When a curl call reaches a configured timeout threshold, should I ensure that the request is retried? A larger timeout is valid if the volume of transactions processed by the queue allows for it, for any given implementation. This value provides enough time for most integration requests to complete and is small enough to ensure that the asynchronous CPM queue does not fall behind, or become backlogged (in other words). In addition to specifying a timeout value in code, there are other considerations to make in terms of the potential to retry the call if the request times out.Ī curl call timeout of five seconds is highly recommended. The use of an appropriate timeout is required with use of PHP curl in a CPM customization. The function is called if all attempts failed: maximum number of attempts reached, or we got an exception which is considered not temporary (by callback function defined in setIsTemporaryException()).Process Designer, Custom Processes (CPM), Connect for PHP (CPHP) > setIsTemporaryException( function( \ Throwable $e): bool So let's take a look on the next example which contains this special logic: $request = new \ GuzzleHttp\ Psr7\ Request(" GET", " ") If it returns false it will immediately stop trying. If it returns true (error is temporary) then new attempt will be performed (except if maximum number of attempts reached). The RetryHelper allows you to define callback function which will be called after each unsuccessful attempt and will decide is this error (exception) is temporary or not. All 400-th statuses means that problem is on client side (wrong password, no access, wrong URL, etc.) Thereby we can divide all errors on "temporary" (which may go away on next try) and "permanent" (which will not disappear). Generally, you don't need to try again if you receive HTTP 4xx status code. ![]() For example, if you have an "authentication error", there's not much reason to try again and again with the same credentials. By default, it will retry on every error (exception), no matter what kind of error. Since this is the simplest example, it uses some default behaviour which we will override in the next examples. The exception thrown inside the anonymous function at the last attempt will be re-thrown to the top. If your Internet completely doesn't work or the website is down, once the maximum number of attempts is reached, the execution will be terminated. But if you have unstable Internet connection, it may take several attempts to get a response. In most cases this code will execute successfully on the first try, the return value of the anonymous function will be redirected to the return value of the execute() method and immediately saved in the $response variable. The second argument defines maximum number of attempts (in this example 10). In this example the code that may fail here is wrapped into anonymous function and passed as the first argument of execute() method. ![]() Return ( new \ GuzzleHttp\ Client())-> send( $request) Įcho $response-> getBody()-> getContents(). $response = ( new \gugglegum\ RetryHelper\ RetryHelper()) Here is the simplest example that tries to get response from an HTTP server using the GuzzleHttp package, up to 10 attempts: $request = new \ GuzzleHttp\ Psr7\ Request(" GET", " ") ![]() Therefore, if your code doesn't throw an exception on error (for example, if you use curl_exec() function which simply returns false on error), then you need to check return value and throw an exception inside this function. This method determines the error only by the exception thrown inside the anonymous function. A section of potentially problematic code should be wrapped in an anonymous function ( Closure, callable) and passed to the execute() method. It's quite flexibly configured through the use of callback functions and supports standardized PHP-FIG/PSR-3 logging. This simple package contains the RetryHelper class, which simplifies error handling, retries, delaying and logging. a request to a remote server), you may need some kind of error handling, retrying, make a delay between attempts, and stop if the maximum number of retries reached. When you perform some action that may not succeed on the first try (e.g. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |