我得到了一个在我之前的问题中提到的功能:
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 将返回一个指向 struct Rate_limit 的指针struct rate_limit
{
uint32_t max;
uint32_t milliseconds;
};
其中 max 是用户在毫秒值间隔内可以进行的最大点击次数。例如,如果 max = 400 且毫秒 = 200,则用户只能在 200 毫秒间隔内进行 400 次点击。当用户违反限制时,该功能应使用带有原型(prototype)的报告功能进行报告,如下所示。
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};
};
我创建了一个 map ,其中键是 user_id,值是 UserTrackingInfostd::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;
}
}
}
最佳答案
我会计算用户每天文秒的点击次数(因此不存储和检查点击期的开始)。优点是简单,但缺点是如果用户在一秒结束时开始点击,那么他说这一秒剩余的点击次数为 1000 次,下一秒的点击次数为 1000 次(例如,他可以每 1.1 次点击 2000 次)第二,但它只适用于第一秒,接下来的几秒会受到适当的限制)。所以:
std::map
中记录点击次数,更新时间为 O(logN),但 std::unordered_map
可能为 O(1) (需要散列实现)或 std::vector
(要求用户 ID 是连续的,但易于实现)std::set
中记录最后一秒活跃的用户(O(logN) 插入和删除) 或 std::unordered_set
(O(1) 插入和删除)在您的点击处理程序中,您首先检查是否开始了新的一秒(这很容易,检查您的时间戳是否在下一秒的时间戳之后)。如果是这样,则重置所有事件用户的计数(此事件用户跟踪是为了不浪费时间迭代整个
std::map
)。然后清除活跃用户。接下来处理实际的点击。从 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
}
关于c++ - 当用户超过按钮点击限制时报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62860807/