LootLocker Unreal SDK 10.4.0
Game backend SDK for Unreal Engine
Loading...
Searching...
No Matches
LootLockerRateLimiter.h
Go to the documentation of this file.
1// Copyright (c) 2021 LootLocker
2
3#pragma once
4
5#include "CoreMinimal.h"
6
30class LOOTLOCKERSDK_API FLootLockerRateLimiter
31{
32public:
33 // -------------------------------------------------------------------------
34 // Algorithm constants (all match the Unity SDK values)
35 // -------------------------------------------------------------------------
36
38 static constexpr int32 TripWireTimeFrameSeconds = 60;
39
41 static constexpr int32 MaxRequestsPerTripWireTimeFrame = 280;
42
44 static constexpr int32 SecondsPerBucket = 5;
45
51 static constexpr float AllowXPercentOfTripWireMaxForMovingAverage = 0.8f;
52
54 static constexpr int32 CountMovingAverageAcrossNTripWireTimeFrames = 3;
55
56 // -------------------------------------------------------------------------
57 // Derived constants
58 // -------------------------------------------------------------------------
59
61 static constexpr int32 BucketsPerTimeFrame = TripWireTimeFrameSeconds / SecondsPerBucket;
62
64 static constexpr int32 RateLimitMovingAverageBucketCount = CountMovingAverageAcrossNTripWireTimeFrames * BucketsPerTimeFrame;
65
70 static constexpr int32 MaxRequestsPerBucketOnMovingAverage =
71 (MaxRequestsPerTripWireTimeFrame * 4) / (BucketsPerTimeFrame * 5);
72
73 // -------------------------------------------------------------------------
74 // Lifecycle
75 // -------------------------------------------------------------------------
76
78 virtual ~FLootLockerRateLimiter() = default;
79
84 void Reset();
85
86 // -------------------------------------------------------------------------
87 // Public API
88 // -------------------------------------------------------------------------
89
98
104
105protected:
106 // -------------------------------------------------------------------------
107 // Testability hook
108 // -------------------------------------------------------------------------
109
114 virtual FDateTime GetTimeNow() const;
115
116 // -------------------------------------------------------------------------
117 // State (protected for test-subclass access)
118 // -------------------------------------------------------------------------
119
121 bool bEnableRateLimiter = true;
122
127 bool bFirstRequestSent = false;
128
130 int32 Buckets[RateLimitMovingAverageBucketCount];
131
133 int32 LastBucket = -1;
134
137
139 int32 TotalRequestsInBuckets = 0;
140
142 int32 TotalRequestsInBucketsInTripWireTimeFrame = 0;
143
145 bool bIsRateLimited = false;
146
149
150private:
156 int32 MoveCurrentBucket(const FDateTime& Now);
157};
Client-side rate limiter for the LootLocker HTTP execution queue.
Definition LootLockerRateLimiter.h:31
int32 GetSecondsLeftOfRateLimit() const
Returns the approximate number of seconds until the rate limit clears.
void Reset()
Resets all rate-limiting state to its initial values.
FDateTime RateLimitResolvesAt
Wall-clock time at which the current rate limit is expected to clear.
Definition LootLockerRateLimiter.h:148
FDateTime LastBucketChangeTime
Wall-clock time at which the current bucket became active.
Definition LootLockerRateLimiter.h:136
virtual ~FLootLockerRateLimiter()=default
virtual bool AddRequestAndCheckIfRateLimitHit()
Records one outgoing request and returns whether the rate limit is currently active.
virtual FDateTime GetTimeNow() const
Returns the current wall-clock time used for bucket advancement.