periodic_executor.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include "config.h"
  3. #include "periodic_executor_base.h"
  4. #include "public.h"
  5. #include <yt/yt/core/actions/callback.h>
  6. #include <yt/yt/core/actions/future.h>
  7. #include <yt/yt/core/misc/backoff_strategy.h>
  8. namespace NYT::NConcurrency {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. namespace NDetail {
  11. class TDefaultInvocationTimePolicy
  12. : private TPeriodicExecutorOptions
  13. {
  14. public:
  15. using TCallbackResult = void;
  16. using TOptions = TPeriodicExecutorOptions;
  17. explicit TDefaultInvocationTimePolicy(const TOptions& options);
  18. void ProcessResult();
  19. TInstant KickstartDeadline();
  20. bool IsEnabled();
  21. bool ShouldKickstart(const TOptions& newOptions);
  22. void SetOptions(TOptions newOptions);
  23. bool ShouldKickstart(const std::optional<TDuration>& period);
  24. void SetOptions(std::optional<TDuration> period);
  25. TInstant NextDeadline();
  26. bool IsOutOfBandProhibited();
  27. void Reset();
  28. };
  29. } // namespace NDetail
  30. //! Helps to perform certain actions periodically.
  31. class TPeriodicExecutor
  32. : public NDetail::TPeriodicExecutorBase<NDetail::TDefaultInvocationTimePolicy>
  33. {
  34. public:
  35. //! Initializes the instance.
  36. /*!
  37. * \note
  38. * We must call #Start to activate the instance.
  39. *
  40. * \param invoker Invoker used for wrapping actions.
  41. * \param callback Callback to invoke periodically.
  42. * \param options Period, splay, etc.
  43. */
  44. TPeriodicExecutor(
  45. IInvokerPtr invoker,
  46. TPeriodicCallback callback,
  47. NConcurrency::TPeriodicExecutorOptions options);
  48. TPeriodicExecutor(
  49. IInvokerPtr invoker,
  50. TPeriodicCallback callback,
  51. std::optional<TDuration> period = {});
  52. //! Changes execution period.
  53. void SetPeriod(std::optional<TDuration> period);
  54. private:
  55. using TBase = NDetail::TPeriodicExecutorBase<NDetail::TDefaultInvocationTimePolicy>;
  56. };
  57. DEFINE_REFCOUNTED_TYPE(TPeriodicExecutor)
  58. ////////////////////////////////////////////////////////////////////////////////
  59. } // namespace NYT::NConcurrency