我有一个在之前的问题中提到的功能: Function to calculate how often users are pressing a button
void onNewButtonPress(int64_t nanoseconds_timestamp, int32_t user_id);
每次具有user_id的用户单击按钮时,都会调用此函数。 其中nanoseconds_timestamp参数是自纪元以来的时间(以纳秒为单位)。
我的任务是为每个用户进行速率限制检查。每个用户都有自己的点击率限制。 可以使用以下功能检索每个用户的速率限制:
const struct rate_limit* getLimit(uint32_t userId);
getlimit将返回一个指向结构Rate_limit的指针
struct rate_limit
{
uint32_t max;
uint32_t milliseconds;
};
其中max是用户可以在毫秒值间隔内进行的最大点击次数。例如,如果max = 400,毫秒= 200,则用户只能在200毫秒的间隔内进行400次点击。
当用户违反限制时,该功能应使用带有原型的报告功能报告如下。
void report(uint32_t user_id);
人们,您将如何检测到用户何时超出其限制。以下是我的评论解决方案。我仍然相信,可能会有更聪明,更好的解决方案,并且希望听到您的意见。
我的实现细节如下
我创建了一个结构,其中将包含有关每个用户历史记录的信息。
struct UserTrackingInfo
{
/* Limit which is returned when a user clicks for the first time */
const rate_limit* limit;
/* Variable which will get incremented each time a user is making a click */
uint32_t breachCount{0};
/* Timestamp in nanoseconds of when breachPeriod started. Whenever a user clicks for the first time
* this timestamp will be initialized
* Time will be sliced in breach periods whose duration is equal to the max of the rate_limit */
uint64_t breachPeriodStartTs{0};
};
我创建了一个地图,其中的键是user_id,值是UserTrackingInfo
std::map<int32_t, struct UserTrackingInfo > tInfo;
这是我建议的onNewButtonPress函数的实现。
void onNewButtonPress(uint64_t& nanoseconds_timestamp, int32_t user_id)
{
auto &tref = tInfo[user_id];
if (tref.breachPeriodStartTs == 0){
/* if a user hasnt clicked before, get the limit for the user and initialize a breach period start time stamp */
tref.limit = getLimit(user_id);
tref.breachPeriodStartTs = nanoseconds_timestamp;
}
else
{
/* Increment how many times used clicked a button */
tref.breachCount++;
/* Get number of ns passed since the time when last period started */
int64_t passed = nanoseconds_timestamp - tref.breachPeriodStartTs;
/* If we reached a limit, report it */
if (passed < (tref.limit->milliseconds * 1000)){
if (tref.breachCount > tref.limit->max){
report(user_id);
}
/* we dont start a new period yet */
}
else{
/* If the limit hasnt been reached yet */
/* User may have pressed after the end of the breach period. Or he didnt make any clicks for the duration of a couple of breach periods */
/* Find number of breach measure periods that may have been missed */
uint64_t num_periods = passed / (tref.limit->milliseconds * 1000);
/* Get duration of the passed periods in nanoseconds */
uint64_t num_periods_ns = num_periods * (tref.limit->milliseconds * 1000);
/* Set the the start time of the current breach measure period */
/* and reset breachCount */
tref.breachPeriodStartTs = tref.breachPeriodStartTs + num_periods_ns;
tref.breachCount = 1;
}
}
}
答案 0 :(得分:0)
我将计算用户在天文秒中的点击次数(因此,无需存储和检查点击时间段的开始)。优点是简单,但缺点是,如果用户在一秒钟的末尾开始单击,则他说剩余的第二秒为1000次点击,下一秒为1000次(例如,他每1.1秒钟可以进行2000次点击)秒,但仅在第一秒有效,接下来的秒数将受到适当限制)。所以:
std::map
中的点击计数,将其更新为O(logN),但使用std::unordered_map
(需要哈希实现)或std::vector
(需要用户)则为O(1) id是连续的,但易于实现)std::set
(O(logN)插入和删除)或std::unordered_set
(O(1)插入和删除)中记录最后一秒哪些用户处于活动状态在点击处理程序中,您首先要检查是否有新的秒针开始(很容易,请检查您的时间戳记是否在下一秒的时间戳记之后)。如果是这样,则重置所有活动用户的计数(此活动用户跟踪是为了避免浪费时间迭代整个std::map
)。然后清除活动用户。
接下来处理实际点击。从地图上获取一条记录并更新计数,检查是否超出了限制,如果超过,则进行报告。
请注意,处理下一秒的开始是一项繁重的操作,在处理下一秒之前,不会处理用户单击(如果您有许多活动用户,可能会导致延迟)。为避免这种情况,您应该创建一个新函数来处理新的秒数,并为新的秒数开始创建一些事件,而不是在点击处理程序中处理所有事件。
struct ClicksTrackingInfo {
uint32_t clicksCount;
uint32_t clicksQuotaPerSec;
};
using UserId_t = int32_t; // user id type
std::set<UserId_t> activeUsers; // users who ever clicked in this second -- O(logN) to update
std::map<UserId_t, ClicksTrackingInfo> clicksPerCurrentSecond; // clicks counts -- O(logN) to update
uint64_t nextSecondTs; // holds nanosecond for beginning of the next second, e.g. if it's a 15th second then nextSecondTs == 16*1000*1000*1000
void onNewButtonPress(uint64_t& nanosecondsTimestamp, UserId_t userId)
{
// first find out if a new second started
if (nanosecondsTimestamp > nextSecondTs) {
nextSecondTs = (nanosecondsTimestamp / 1000000000 + 1) * 1000000000; // leave only seconds part of next second
// clear all records from active users of last second
for (auto userId : activeUsers) {
clicksPerCurrentSecond[userId].clicksCount = 0;
}
activeUsers.clear(); // new second, no users yet
}
// now process current click by the user
auto& [clicksCount, clicksLimit] = clicksPerCurrentSecond[userId]; // C++17 syntax
++clicksCount;
if (clicksCount > clicksLimit) {
report(userId);
// return; // possibly deny this click
}
activeUsers.emplace(userId); // remember this user was active in current second
// doClick(); // do the click action
}