slowpoke_actor.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <library/cpp/actors/core/actor_bootstrapped.h>
  3. namespace NActors {
  4. class TSlowpokeActor : public TActorBootstrapped<TSlowpokeActor> {
  5. const TDuration Duration;
  6. const TDuration SleepMin;
  7. const TDuration SleepMax;
  8. const TDuration RescheduleMin;
  9. const TDuration RescheduleMax;
  10. public:
  11. static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
  12. return NKikimrServices::TActivity::INTERCONNECT_COMMON;
  13. }
  14. TSlowpokeActor(TDuration duration, TDuration sleepMin, TDuration sleepMax, TDuration rescheduleMin, TDuration rescheduleMax)
  15. : Duration(duration)
  16. , SleepMin(sleepMin)
  17. , SleepMax(sleepMax)
  18. , RescheduleMin(rescheduleMin)
  19. , RescheduleMax(rescheduleMax)
  20. {}
  21. void Bootstrap(const TActorContext& ctx) {
  22. Become(&TThis::StateFunc, ctx, Duration, new TEvents::TEvPoisonPill);
  23. HandleWakeup(ctx);
  24. }
  25. void HandleWakeup(const TActorContext& ctx) {
  26. Sleep(RandomDuration(SleepMin, SleepMax));
  27. ctx.Schedule(RandomDuration(RescheduleMin, RescheduleMax), new TEvents::TEvWakeup);
  28. }
  29. static TDuration RandomDuration(TDuration min, TDuration max) {
  30. return min + TDuration::FromValue(RandomNumber<ui64>(max.GetValue() - min.GetValue() + 1));
  31. }
  32. STRICT_STFUNC(StateFunc,
  33. CFunc(TEvents::TSystem::PoisonPill, Die)
  34. CFunc(TEvents::TSystem::Wakeup, HandleWakeup)
  35. )
  36. };
  37. } // NActors