db_await.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <chrono>
  3. #include <cstdint>
  4. #include <functional>
  5. /// Waiting for the database at startup, bounded by WALL TIME.
  6. ///
  7. /// This exists because the previous version bounded the wait by an ATTEMPT
  8. /// COUNT - `for (i = 0; i < 30 && !db.connect(); ++i) sleep(1s)` - and that
  9. /// count carried an unstated assumption about how long one attempt costs.
  10. /// Database client 2.13.0 and earlier returned from connect() immediately on an
  11. /// unreachable server, because it only built the gRPC stub, so 30 attempts cost
  12. /// about 30 seconds. In 2.13.1 connect() WAITS up to its configured timeout
  13. /// (5 s for us) before answering, so the same loop costs up to 180 seconds.
  14. ///
  15. /// Our units are `Type=notify` with systemd's default `TimeoutStartSec` of 90
  16. /// seconds, so that loop would be SIGTERMed partway through. A clean
  17. /// "database not reachable" plus exit 1 - diagnosable, and restarted 3 seconds
  18. /// later - would become "start operation timed out", killed mid-loop, with
  19. /// restart churn. Not a rare path either: the package postinst starts the
  20. /// service while the database is still down on every upgrade.
  21. ///
  22. /// So the bound is a deadline. Swapping 30 for a smaller number would have been
  23. /// the same bug waiting for the next change in what an attempt costs.
  24. ///
  25. /// Pure and clock-injected so the property that broke - "returns within its
  26. /// budget however slow one attempt is" - can be tested without a database and
  27. /// without really sleeping.
  28. namespace svapi {
  29. /// Call `tryConnect` until it succeeds or `budget` has elapsed.
  30. ///
  31. /// Always attempts at least ONCE, even with a zero budget: a caller asking to
  32. /// connect wants one honest try, and a zero budget is a configuration choice
  33. /// about waiting rather than an instruction to skip the work.
  34. ///
  35. /// `sleepFor` is only called when there is budget left for it, so the function
  36. /// cannot overshoot by sleeping past its own deadline.
  37. inline bool awaitConnectable(const std::function<bool()>& tryConnect,
  38. const std::function<std::chrono::steady_clock::time_point()>& now,
  39. const std::function<void(std::chrono::milliseconds)>& sleepFor,
  40. std::chrono::milliseconds budget,
  41. std::chrono::milliseconds betweenAttempts) {
  42. const auto deadline = now() + budget;
  43. for (;;) {
  44. if (tryConnect()) return true;
  45. const auto remaining = deadline - now();
  46. // No time for another attempt's pause, so no point starting one: the
  47. // attempt itself may cost more than everything left.
  48. if (remaining <= betweenAttempts) return false;
  49. if (betweenAttempts > std::chrono::milliseconds::zero()) sleepFor(betweenAttempts);
  50. }
  51. }
  52. } // namespace svapi