!296 事件并发问题修复

Merge pull request !296 from zero-cyc/0527
This commit is contained in:
openharmony_ci
2022-05-28 02:09:36 +00:00
committed by Gitee
2 changed files with 4 additions and 7 deletions
+2
View File
@@ -17,6 +17,7 @@
#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_PUBLIC_MANAGER_H
#include <map>
#include <mutex>
#include <vector>
#include <stdint.h>
#include "singleton.h"
@@ -38,6 +39,7 @@ public:
bool CheckIsFloodAttack(pid_t appUid);
private:
std::mutex mutex_;
std::map<pid_t, std::vector<int64_t>> floodAttackAppStatistics_;
const uint32_t FLOOD_ATTACK_NUMBER_MAX = 20; // Frequency of decision
const int64_t FLOOD_ATTACK_INTERVAL_MAX = 5; // Period of decision (unit: millisecond)
+2 -7
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -31,18 +31,15 @@ PublishManager::~PublishManager()
bool PublishManager::CheckIsFloodAttack(pid_t appUid)
{
EVENT_LOGI("enter");
std::lock_guard<std::mutex> lock(mutex_);
bool isAttacked = false;
int64_t now = SystemTime::GetNowSysTime();
EVENT_LOGI("dispatch common event by app (uid = %{publish}d) at now = %{public}" PRId64, appUid, now);
auto iter = floodAttackAppStatistics_.find(appUid);
if (iter == floodAttackAppStatistics_.end()) {
floodAttackAppStatistics_[appUid].emplace_back(now);
return isAttacked;
}
// Remove expired record
auto iterVec = iter->second.begin();
while (iterVec != iter->second.end()) {
@@ -52,13 +49,11 @@ bool PublishManager::CheckIsFloodAttack(pid_t appUid)
break;
}
}
if (iter->second.size() + 1 > FLOOD_ATTACK_NUMBER_MAX) {
EVENT_LOGW("CES was maliciously attacked by app (uid = %{publish}d)", appUid);
isAttacked = true;
}
iter->second.emplace_back(now);
return isAttacked;
}
} // namespace EventFwk