Line data Source code
1 : #ifndef NETWORKRETRYPOLICY_H
2 : #define NETWORKRETRYPOLICY_H
3 :
4 : #include <QObject>
5 : #include <QNetworkReply>
6 :
7 : /**
8 : * @brief Defines retry behavior.
9 : */
10 : class NetworkRetryPolicy
11 : {
12 : public:
13 : enum BackoffStrategy {
14 : Linear, // 1s, 2s, 3s, 4s...
15 : Exponential, // 1s, 2s, 4s, 8s...
16 : Fibonacci, // 1s, 2s, 3s, 5s, 8s...
17 : Fixed // Same delay every time
18 : };
19 :
20 : NetworkRetryPolicy();
21 :
22 :
23 : NetworkRetryPolicy(int maxRetries, int baseDelay, BackoffStrategy strategy, int maxDelay = 60000);
24 :
25 :
26 : static NetworkRetryPolicy forFeedUpdate();
27 : static NetworkRetryPolicy forFavicon();
28 : static NetworkRetryPolicy forCritical();
29 : static NetworkRetryPolicy noRetry();
30 :
31 : /**
32 : * @brief Whether or not we should try, try again.
33 : */
34 : bool isRetryable(QNetworkReply::NetworkError error) const;
35 :
36 : /**
37 : * @brief Returns retry delay.
38 : */
39 : int calculateDelay(int attemptNumber) const;
40 :
41 : /**
42 : * @brief Returns max retries.
43 : */
44 5 : int maxRetries() const { return maxNumRetries; }
45 :
46 : /**
47 : * @brief Check if retries should continue or give up.
48 : */
49 24 : bool shouldRetry(int currentAttempt) const { return currentAttempt < maxNumRetries; }
50 :
51 : /**
52 : * @brief Sets if we will retry on timeout errors.
53 : */
54 : void setRetryOnTimeout(bool retry) { shouldRetryOnTimeout = retry; }
55 :
56 : /**
57 : * @brief Sets if we will retry on DNS errors.
58 : */
59 : void setRetryOnDnsFailure(bool retry) { shouldRetryOnDnsFailure = retry; }
60 :
61 : /**
62 : * @brief Sets if we will retry on connection refused.
63 : */
64 : void setRetryOnConnectionRefused(bool retry) { shouldRetryOnConnectionRefused = retry; }
65 :
66 : private:
67 : int maxNumRetries;
68 : int defaultDelayMs;
69 : int maxDelayMs;
70 : BackoffStrategy strategy;
71 :
72 : bool shouldRetryOnTimeout;
73 : bool shouldRetryOnDnsFailure;
74 : bool shouldRetryOnConnectionRefused;
75 : bool shouldRetryOnServerError;
76 : };
77 :
78 : #endif // NETWORKRETRYPOLICY_H
|