LootLocker Unreal SDK 10.7.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
55
56 // -------------------------------------------------------------------------
57 // Derived constants
58 // -------------------------------------------------------------------------
59
62
65
70 static constexpr int32 MaxRequestsPerBucketOnMovingAverage =
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
122
127 bool bFirstRequestSent = false;
128
131
133 int32 LastBucket = -1;
134
137
140
143
145 bool bIsRateLimited = false;
146
149
150private:
156 int32 MoveCurrentBucket(const FDateTime& Now);
157};
static constexpr float AllowXPercentOfTripWireMaxForMovingAverage
Moving-average threshold expressed as a fraction of the tripwire maximum.
Definition LootLockerRateLimiter.h:51
int32 GetSecondsLeftOfRateLimit() const
Returns the approximate number of seconds until the rate limit clears.
bool bEnableRateLimiter
When false, AddRequestAndCheckIfRateLimitHit() always returns false.
Definition LootLockerRateLimiter.h:121
static constexpr int32 TripWireTimeFrameSeconds
Length of the tripwire measurement window in seconds.
Definition LootLockerRateLimiter.h:38
static constexpr int32 CountMovingAverageAcrossNTripWireTimeFrames
Number of tripwire time frames covered by the moving-average window.
Definition LootLockerRateLimiter.h:54
static constexpr int32 BucketsPerTimeFrame
Number of buckets that span one tripwire time frame (= 60 / 5 = 12).
Definition LootLockerRateLimiter.h:61
int32 TotalRequestsInBucketsInTripWireTimeFrame
Running total of requests in the buckets that fall within the current tripwire time frame.
Definition LootLockerRateLimiter.h:142
bool bIsRateLimited
True while requests are being rejected due to an active rate limit.
Definition LootLockerRateLimiter.h:145
bool bFirstRequestSent
Set to true once the first request has been processed.
Definition LootLockerRateLimiter.h:127
static constexpr int32 RateLimitMovingAverageBucketCount
Total number of buckets in the circular array (= 3 * 12 = 36).
Definition LootLockerRateLimiter.h:64
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
static constexpr int32 SecondsPerBucket
Duration of each bucket in seconds.
Definition LootLockerRateLimiter.h:44
FDateTime LastBucketChangeTime
Wall-clock time at which the current bucket became active.
Definition LootLockerRateLimiter.h:136
static constexpr int32 MaxRequestsPerTripWireTimeFrame
Maximum requests allowed within a single TripWireTimeFrameSeconds window.
Definition LootLockerRateLimiter.h:41
virtual ~FLootLockerRateLimiter()=default
static constexpr int32 MaxRequestsPerBucketOnMovingAverage
Per-bucket average that triggers the moving-average rate limit.
Definition LootLockerRateLimiter.h:70
int32 TotalRequestsInBuckets
Running total of all requests across every bucket in the array.
Definition LootLockerRateLimiter.h:139
int32 LastBucket
Index of the most-recently-written bucket (-1 = not yet started).
Definition LootLockerRateLimiter.h:133
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.
int32 Buckets[RateLimitMovingAverageBucketCount]
Circular array of per-bucket request counts.
Definition LootLockerRateLimiter.h:130