| #include <utility> |
| 6 |
| 7 | // Rebuilt from running requests on each scheduling pass, so cancellation, |
| 8 | // retries and DC migration cannot leak reservations. |
| 9 | class DownloadRequestBudget { |
| 10 | public: |
| 11 | explicit DownloadRequestBudget(bool premium) : limit((premium ? 16u : 2u) * 1024 * 1024) { |
| 12 | } |
| 13 |
| 14 | void account(uint32_t dc, bool small, uint32_t bytes) { |
| 15 | used[{dc, small}] += bytes; |
| 16 | } |
| 17 |
| 18 | bool acquire(uint32_t dc, bool small, uint32_t bytes) { |
| 19 | if (bytes == 0) { |
| 20 | return true; // Control requests must not wait behind file data. |
| 21 | } |
| 22 | auto ¤t = used[{dc, small}]; |
| 23 | if (bytes > limit || current > limit - bytes) { |
| 24 | return false; |
| 25 | } |
| 26 | current += bytes; |
| 27 | return true; |
| 28 | } |
| 29 |
| 30 | private: |
| 31 | const uint32_t limit; |
| 32 | std::map<std::pair<uint32_t, bool>, uint64_t> used; |
| 33 | }; |
| 34 |