aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/slowpoke_actor.h
blob: 4b02e5da48cc73b55d8065bac039704a54ea5431 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include <library/cpp/actors/core/actor_bootstrapped.h>

namespace NActors {

    class TSlowpokeActor : public TActorBootstrapped<TSlowpokeActor> {
        const TDuration Duration;
        const TDuration SleepMin;
        const TDuration SleepMax;
        const TDuration RescheduleMin;
        const TDuration RescheduleMax;

    public:
        static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
            return NKikimrServices::TActivity::INTERCONNECT_COMMON;
        }

        TSlowpokeActor(TDuration duration, TDuration sleepMin, TDuration sleepMax, TDuration rescheduleMin, TDuration rescheduleMax)
            : Duration(duration)
            , SleepMin(sleepMin)
            , SleepMax(sleepMax)
            , RescheduleMin(rescheduleMin)
            , RescheduleMax(rescheduleMax)
        {}

        void Bootstrap(const TActorContext& ctx) {
            Become(&TThis::StateFunc, ctx, Duration, new TEvents::TEvPoisonPill);
            HandleWakeup(ctx);
        }

        void HandleWakeup(const TActorContext& ctx) {
            Sleep(RandomDuration(SleepMin, SleepMax));
            ctx.Schedule(RandomDuration(RescheduleMin, RescheduleMax), new TEvents::TEvWakeup);
        }

        static TDuration RandomDuration(TDuration min, TDuration max) {
            return min + TDuration::FromValue(RandomNumber<ui64>(max.GetValue() - min.GetValue() + 1));
        }

        STRICT_STFUNC(StateFunc,
            CFunc(TEvents::TSystem::PoisonPill, Die)
            CFunc(TEvents::TSystem::Wakeup, HandleWakeup)
        )
    };

} // NActors