Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
rate_controller.h
1 #ifndef SOTA_CLIENT_TOOLS_RATE_CONTROLLER_H_
2 #define SOTA_CLIENT_TOOLS_RATE_CONTROLLER_H_
3 
4 #include <chrono>
5 
6 /**
7  * Control the rate of outgoing requests.
8  * This receives signals from the network layer when a request finishes of the form (start time, end time, success).
9  * It generates controls for the network layer in the form of:
10  * MaxConcurrency - The current estimate of the number of parallel requests that can be opened
11  * Sleep() - The number of seconds to sleep before sending the next request. 0.0 if MaxConcurrency is > 1
12  * Failed() - A boolean indicating that the server is broken, and to report an error up to the user.
13  * The congestion control is loosely based on the original TCP AIMD scheme. Better performance might be available by
14  * Stealing ideas from the later TCP conjection control algorithms
15  */
17  public:
18  using clock = std::chrono::steady_clock;
19  explicit RateController(int concurrency_cap = 30);
20  RateController(const RateController&) = delete;
21  RateController operator=(const RateController&) = delete;
22 
23  void RequestCompleted(clock::time_point start_time, clock::time_point end_time, bool succeeded);
24 
25  int MaxConcurrency() const;
26 
27  clock::duration GetSleepTime() const;
28 
29  bool ServerHasFailed() const;
30 
31  private:
32  /**
33  * After sleeping this long and still getting a 500 error, assume the
34  * server has failed permanently
35  */
36  static const clock::duration kMaxSleepTime;
37 
38  /**
39  * After getting a failure with a concurrency of 1, sleep for this long
40  * before retrying. Following retries grow exponentially to kMaxSleepTime.
41  */
42  static const clock::duration kInitialSleepTime;
43 
44  const int concurrency_cap_;
45  /**
46  * After making a change to the system, we wait a full round-trip time to
47  * see any effects of the change. This is the last time that an change was
48  * made, and only requests that started after this time will be considered
49  * to be 'new' information.
50  */
51  clock::time_point last_concurrency_update_;
52  int max_concurrency_{1};
53  clock::duration sleep_time_{0};
54 
55  void CheckInvariants() const;
56 };
57 
58 #endif // SOTA_CLIENT_TOOLS_RATE_CONTROLLER_H_
RateController
Control the rate of outgoing requests.
Definition: rate_controller.h:16