This is a creation in Article, where the information may have evolved or changed. I believe you've had the time to invoke the Backoff algorithm for the API interface. In the Go language, there are [Github.com/cenkalti/backoff] (Https://github.com/cenkalti/backoff), [Github.com/jpillora/backoff] ( Https://github.com/jpillora/backoff), and other libraries can be used. I have used [Github.com/cenkalti/backoff] (Https://github.com/cenkalti/backoff), but there is one thing that confuses me: it requires you to add closures to the operation, forcing input as func () The form of error. For example, when you need a function that can be automatically retried (as in the following MyFunc function), return 3 values and an error that you need and scope wits. ' Govar A, B, C Resultbackoff.retry (func () error {var err errora, B, c, err = MyFunc (ARG, ...) Return err}, Backoffobject) "In terms of performance, this may be negligible, but the scope is completely messed up, this time you have to be careful when assigning values do not use: =, but use = to ensure that the correct value is obtained. This is the main reason why I wrote [Github.com/lestrrat-go/backoff] (Github.com/lestrrat-go/backoff) library. Using this library, you will have to write more boilerplate code (the functions that the library needs to execute: newexponential (), Start (), etc.), but you do not need to implement a closure, and I find it more in line with the Go style. Calculating the Backoff duration is still difficult, if you have a better algorithm, please let me know. Using the [Github.com/lestrrat-go/backoff] (Github.com/lestrrat-go/backoff) library, you first create a policy object: "' gopolicy: = Backoff. Newexponential (...) The incoming parameter also affects the policy, such as configuring the maximum number of retries or not retrying. Policy objects can be reused by multiple consumers. The Backoff object is actually created by invoking the Start () method of the policy object: "' Gob, Cancel: = policy. Start (CTX) "This method receives a context object, so you can terminate the Backoff algorithm through the release operation within the parent scope. Cancel is used to free resources when the Backoff object B is no longer needed. The Backoff object B contains two methods, done (), and Next (). They all return a pipeline variable that can notify us of the event. When the Backoff algorithm stops, done () becomes readable: (The condition of the stop) includes the parent context being canceled causing the backoff algorithm to be canceled or a condition to occur (for example, when we have retried the number of times specified by the maxretries parameter). Next () becomes readable when the call has been made enough. In the case of exponential fallback, after calling Next (), you may have to wait for 1, 2, 4, 8, 16 ... (multiplied by the base interval). Using these methods, your Backoff method will look like this: "' Gofunc myfuncwithretries (CTX context. Context, ...) (RESULT1, RESULT2, RESULT3, error) {b, Cancel: = policy. Start (CTX) defer cancel () for {ret1, Ret2, Ret3, err: = MyFunc (...) If Err = = Nil {//Successreturn Ret1, Ret2, Ret3, nil}select {case <-b.done (): Return nil, nil, nil, errors. New (' All attempts failed ') case <-b.next ()://Continue to beginning of the A For loop, execute MyFunc again}} "The disadvantage of using this method Is that you need more boilerplate code. The advantage is that you no longer need strange scoping techniques, and the operation becomes more straightforward. I think it's a matter of taste, so you should choose a library that fits your tastes or habits. It happens to be the library I want and hopefully it will help you. Happy programming!
via:https://medium.com/@lestrrat/yak-shaving-with-backoff-libraries-in-go-80240f0aa30c
Author: Daisuke Maki Translator: gogeof proofreading: Rxcai
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
417 Reads