Perf(license): mutex + write-suppression for licenses.ini hot path
licenses.ini was hit on every heartbeat -- 5s x clients x ~8 SetStr per
auth -- with no concurrency protection. Two consequences:
1. 100 concurrent online would saturate the file (~160 writes/sec,
full-file rewrite each via WritePrivateProfileString).
2. Concurrent SetPendingRenewal / DecrementPendingQuota with no lock
occasionally clobbered freshly-set renewal quotas (reported by
user as "preset renewal silently disappears").
Add LicensesIniMutex() (Meyers singleton recursive_mutex, exposed in
CPasswordDlg.h so both CPasswordDlg.cpp and CLicenseDlg.cpp share it)
and wrap all 15 functions that touch licenses.ini.
Rewrite UpdateLicenseActivity around g_activityCache (in-memory state
keyed by "SN|IP|machine"): skip the entire write path when nothing
changed and the 30s LastActiveTime throttle window hasn't expired.
Passcode/HMAC are only flushed on actual change (renewal path); IP
list is only rewritten when the yyMMdd timestamp would roll a day.
Measured impact (local 2-client baseline):
before: 0.60 writes/sec (4 writes per heartbeat cluster)
after: 0.07 writes/sec (one write per client per 30s throttle)
Extrapolated to the 100-online target:
before: ~160 writes/sec (saturation)
after: ~3.3 writes/sec (100 clients / 30s throttle window)
Race elimination is the more important win: PendingQuota's
read-modify-write is now atomic, so the "preset renewal disappears"
race is closed.
Notes from audit (these landed during the same iteration):
- Cache key is (SN, IP, machine), not SN alone. A single SN can be
shared by 100+ end machines in bulk-license deployments, so a
per-SN cache flips on every heartbeat and defeats suppression.
Per-(SN, IP, machine) throttling is what makes the 100/30 model
actually hold; an SN-only key reproduced the original ~0.7 writes/s.
- DeleteLicense invalidates the per-SN activity cache via
InvalidateLicenseActivityCache() (prefix scan since one SN maps to
many cache entries). Without this, cache hits after delete would
skip the auto-recreate path and leave the section permanently
missing.
- OnLicenseViewIPs: m_ListLicense.SetItemText moved outside the lock
so the critical section only covers disk I/O.
This commit was merged in pull request #2.
This commit is contained in:
@@ -14,11 +14,65 @@
|
||||
#include "InputDlg.h"
|
||||
#include "FrpsForSubDlg.h"
|
||||
#include "UIBranding.h"
|
||||
#include <unordered_map>
|
||||
#include <ctime>
|
||||
|
||||
// 外部函数声明
|
||||
extern std::vector<std::string> splitString(const std::string& str, char delimiter);
|
||||
extern std::string GetFirstMasterIP(const std::string& master);
|
||||
|
||||
// ---- licenses.ini 并发与写抑制基础设施 (P1) ----
|
||||
// 见 CPasswordDlg.h 中 LicensesIniMutex() 注释。这里给出实例。
|
||||
std::recursive_mutex& LicensesIniMutex()
|
||||
{
|
||||
static std::recursive_mutex m;
|
||||
return m;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// UpdateLicenseActivity 的写抑制缓存:仅当字段实际变化或节流过期时才落盘。
|
||||
//
|
||||
// ⚠️ Cache key 是 "SN|IP|machine" 三元组而非单 SN,因为同一 SN 可能被多个客户端
|
||||
// 共用(团购授权场景:上百台机器共用一个序列号)。若按 SN 索引,多客户端的
|
||||
// (IP, machine) 会在 cache 里反复互相覆盖 → ipChanged 几乎每次都为 true →
|
||||
// 写抑制完全失效(实测从 0.6 次/秒只降到 0.7 次/秒)。
|
||||
//
|
||||
// 5s 心跳 × 100 客户端,每客户端独立 30s 节流后 → 100/30 ≈ 3.3 次落盘/秒。
|
||||
// Passcode/HMAC 是 per-SN 的,按本结构会在每个客户端的 entry 里冗余存一份,
|
||||
// 续期换码时所有客户端会各自触发一次重写(写入同一新值),冗余但无害。
|
||||
struct LicenseActivityCache {
|
||||
time_t lastFlushTime = 0; // 上次实际落盘的 epoch 秒
|
||||
std::string lastPasscode; // 上次写入 ini 的 Passcode
|
||||
std::string lastHMAC; // 上次写入 ini 的 HMAC
|
||||
std::string lastLocation; // 上次写入 ini 的 Location
|
||||
std::string lastIPWriteDate; // 上次写 IP 列表时的日期 yyMMdd
|
||||
};
|
||||
// Key 格式:"SN|IP|machine"。IP/machine 可能为空(ctx == null 路径),
|
||||
// 此时 key 形如 "SN||" —— 该路径自成一类节流域,互不干扰。
|
||||
std::unordered_map<std::string, LicenseActivityCache> g_activityCache;
|
||||
|
||||
// 30 秒节流窗口:LastActiveTime 最多 30 秒落盘一次(即便其它字段未变)。
|
||||
// UI 显示的"最后活跃"最多延迟 30 秒,业务可接受。
|
||||
constexpr int LAST_ACTIVE_THROTTLE_SECONDS = 30;
|
||||
}
|
||||
|
||||
// 由 DeleteLicense 等"在 cache 视野外修改了 disk"的路径调用,清掉某 SN 名下所有
|
||||
// (IP, machine) entry,强制下次 UpdateLicenseActivity 走 firstTime 路径重建 section。
|
||||
// Cache key 形如 "SN|IP|machine",同一 SN 可能对应多个 entry(多客户端共用授权),
|
||||
// 必须按前缀遍历清除。
|
||||
void InvalidateLicenseActivityCache(const std::string& deviceID)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
const std::string prefix = deviceID + "|";
|
||||
for (auto it = g_activityCache.begin(); it != g_activityCache.end(); ) {
|
||||
if (it->first.compare(0, prefix.size(), prefix) == 0) {
|
||||
it = g_activityCache.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CPasswordDlg 对话框
|
||||
|
||||
IMPLEMENT_DYNAMIC(CPasswordDlg, CDialogEx)
|
||||
@@ -105,6 +159,7 @@ bool SaveLicenseInfo(const std::string& deviceID, const std::string& passcode,
|
||||
const std::string& authorization,
|
||||
const std::string& frpConfig)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
|
||||
@@ -146,6 +201,7 @@ bool SaveLicenseInfo(const std::string& deviceID, const std::string& passcode,
|
||||
bool LoadLicenseInfo(const std::string& deviceID, std::string& passcode,
|
||||
std::string& hmac, std::string& remark)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
|
||||
@@ -161,6 +217,7 @@ bool LoadLicenseInfo(const std::string& deviceID, std::string& passcode,
|
||||
// 加载授权的 FRP 配置
|
||||
std::string LoadLicenseFrpConfig(const std::string& deviceID)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
return cfg.GetStr(deviceID, "FrpConfig", "");
|
||||
@@ -169,6 +226,7 @@ std::string LoadLicenseFrpConfig(const std::string& deviceID)
|
||||
// 加载授权的 Authorization(用于 V2 授权返回给第一层)
|
||||
std::string LoadLicenseAuthorization(const std::string& deviceID)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
return cfg.GetStr(deviceID, "Authorization", "");
|
||||
@@ -177,6 +235,7 @@ std::string LoadLicenseAuthorization(const std::string& deviceID)
|
||||
// 更新授权的 Authorization(V2 续期时更新)
|
||||
bool UpdateLicenseAuthorization(const std::string& deviceID, const std::string& authorization)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
|
||||
@@ -313,59 +372,115 @@ static int GetIPCount(const std::string& ipListStr)
|
||||
return (int)ipList.size();
|
||||
}
|
||||
|
||||
// 更新授权活跃信息
|
||||
// 更新授权活跃信息(带写抑制)
|
||||
//
|
||||
// 设计要点:心跳每 5 秒触发一次本函数;同样的 SN 在稳态下绝大多数字段不会变化。
|
||||
// 朴素实现每次心跳都做 6-8 次 SetStr (整文件重写),5 秒就是一轮全文件 I/O 风暴,
|
||||
// 100 在线时会饱和。本实现引入 in-memory 缓存 g_activityCache:
|
||||
// - 字段未变化 + LastActiveTime 节流窗口(30 秒)内 → 直接 return,零 I/O
|
||||
// - 字段变化(passcode/HMAC/IP/Location 任一)→ 仅写变化字段
|
||||
// - 节流过期 → 只写 LastActiveTime(轻量刷新)
|
||||
// - IP 列表中的时间戳是日级精度(yyMMdd),跨天必须重写一次以刷新日期
|
||||
//
|
||||
// 注意:仅在落盘成功后才更新 cache,保证 cache 永远反映"磁盘上当前值"。
|
||||
bool UpdateLicenseActivity(const std::string& deviceID, const std::string& passcode,
|
||||
const std::string& hmac, const std::string& ip,
|
||||
const std::string& location, const std::string& machineName)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
|
||||
// Cache key 是 (SN, IP, machine) 三元组 —— 同 SN 多客户端共用授权时各自独立节流。
|
||||
// 若同 SN 不同 (ip, machine) 共用一个 cache entry,IP 字段会在不同客户端间反复
|
||||
// 翻转,每次心跳都判定为 ipChanged → 写抑制完全失效。
|
||||
const std::string cacheKey = deviceID + "|" + ip + "|" + machineName;
|
||||
auto& cache = g_activityCache[cacheKey];
|
||||
time_t now = time(nullptr);
|
||||
|
||||
// 计算今日日期串(yyMMdd),用于和 IP 列表时间戳比对
|
||||
SYSTEMTIME st;
|
||||
GetLocalTime(&st);
|
||||
char today[8];
|
||||
sprintf_s(today, "%02d%02d%02d", st.wYear % 100, st.wMonth, st.wDay);
|
||||
|
||||
// —— 决策阶段:判断本次心跳是否真的需要落盘 ——
|
||||
// 注意:因 cacheKey 已经包含 (IP, machine),不同的客户端会落到不同 entry,
|
||||
// 所以不再需要在字段比对中处理 IP/machine 变化 —— 那种"变化"其实是 cache miss。
|
||||
const bool firstTime = (cache.lastFlushTime == 0);
|
||||
const bool passcodeChanged = (passcode != cache.lastPasscode);
|
||||
const bool hmacChanged = (hmac != cache.lastHMAC);
|
||||
const bool ipDayChanged = !ip.empty() && !cache.lastIPWriteDate.empty() &&
|
||||
std::string(today) != cache.lastIPWriteDate;
|
||||
const bool locationChanged = !location.empty() && (location != cache.lastLocation);
|
||||
const bool throttleExpired = (now - cache.lastFlushTime >= LAST_ACTIVE_THROTTLE_SECONDS);
|
||||
|
||||
if (!firstTime && !passcodeChanged && !hmacChanged
|
||||
&& !ipDayChanged && !locationChanged && !throttleExpired) {
|
||||
// 100% cache 命中:本客户端的所有字段都与上次落盘一致且节流未过期
|
||||
return true;
|
||||
}
|
||||
|
||||
// —— 落盘阶段:仅写真正需要写的字段 ——
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
|
||||
// 检查该授权是否存在
|
||||
// 检查该授权是否存在(注意:此处仍需读磁盘,因为我们不缓存"是否存在"的事实)
|
||||
std::string existingPasscode = cfg.GetStr(deviceID, "Passcode", "");
|
||||
if (existingPasscode.empty()) {
|
||||
// 授权不存在,但验证成功了,说明是既往授权,自动添加记录
|
||||
const bool isNewRecord = existingPasscode.empty();
|
||||
|
||||
if (isNewRecord) {
|
||||
// 授权不存在但验证成功 —— 既往授权自动加入
|
||||
cfg.SetStr(deviceID, "SerialNumber", deviceID);
|
||||
cfg.SetStr(deviceID, "Passcode", passcode);
|
||||
cfg.SetStr(deviceID, "HMAC", hmac);
|
||||
cfg.SetStr(deviceID, "Remark", "既往授权自动加入");
|
||||
cfg.SetStr(deviceID, "Status", LICENSE_STATUS_ACTIVE); // 新记录默认为有效
|
||||
cfg.SetStr(deviceID, "Status", LICENSE_STATUS_ACTIVE);
|
||||
} else {
|
||||
// 授权已存在,更新 passcode(续期后 passcode 会变化)
|
||||
cfg.SetStr(deviceID, "Passcode", passcode);
|
||||
cfg.SetStr(deviceID, "HMAC", hmac);
|
||||
// 已存在:只在 passcode/hmac 实际变化时才写(续期场景才会变)
|
||||
if (firstTime || passcodeChanged) {
|
||||
cfg.SetStr(deviceID, "Passcode", passcode);
|
||||
}
|
||||
if (firstTime || hmacChanged) {
|
||||
cfg.SetStr(deviceID, "HMAC", hmac);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新最后活跃时间
|
||||
SYSTEMTIME st;
|
||||
GetLocalTime(&st);
|
||||
// LastActiveTime:走到这里就更新(节流过期或字段变化都需要刷新)
|
||||
char timeStr[32];
|
||||
sprintf_s(timeStr, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
|
||||
cfg.SetStr(deviceID, "LastActiveTime", timeStr);
|
||||
|
||||
// 如果是新添加的记录,设置创建时间
|
||||
if (existingPasscode.empty()) {
|
||||
if (isNewRecord) {
|
||||
cfg.SetStr(deviceID, "CreateTime", timeStr);
|
||||
}
|
||||
|
||||
// 更新 IP 列表(追加新 IP 或更新已有 IP 的时间戳)
|
||||
// 格式: IP(机器名)|yyMMdd
|
||||
if (!ip.empty()) {
|
||||
// IP 列表:本客户端首次 或 同客户端跨天 才重写(UpdateIPList 会在 disk 上合并)
|
||||
if (!ip.empty() && (firstTime || ipDayChanged)) {
|
||||
std::string existingIPList = cfg.GetStr(deviceID, "IP", "");
|
||||
std::string newIPList = UpdateIPList(existingIPList, ip, machineName);
|
||||
cfg.SetStr(deviceID, "IP", newIPList);
|
||||
cache.lastIPWriteDate = today;
|
||||
}
|
||||
if (!location.empty()) {
|
||||
|
||||
if (!location.empty() && (firstTime || locationChanged)) {
|
||||
cfg.SetStr(deviceID, "Location", location);
|
||||
}
|
||||
|
||||
// —— 同步缓存(必须在落盘成功后)——
|
||||
cache.lastFlushTime = now;
|
||||
cache.lastPasscode = passcode;
|
||||
cache.lastHMAC = hmac;
|
||||
if (!location.empty()) {
|
||||
cache.lastLocation = location;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查授权是否已被撤销
|
||||
bool IsLicenseRevoked(const std::string& deviceID)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _lock(LicensesIniMutex());
|
||||
std::string iniPath = GetLicensesPath();
|
||||
config cfg(iniPath);
|
||||
std::string status = cfg.GetStr(deviceID, "Status", LICENSE_STATUS_ACTIVE);
|
||||
|
||||
Reference in New Issue
Block a user