compile

make

The installation

  1. Modify the HEADERINSTALLDIR and LIBINSTALLDIR variables in the makefile
  2. make install

Unit testing

  1. Make. For example, make demo

Introduction to the

Libwxfreq is a high performance frequency limited library. Through a few simple interfaces, it is possible to achieve a general configurable frequency limiting function. With other network frameworks, a common high performance frequency control service can be realized.

Features:

  1. Height is configurable. Statistics duration and rules are configurable.
  2. Any key type is supported.

The configuration file

  1. format

Meta information about appID, which specifies the statistics duration of APPPID. By default, the first statistical time is 60s, the second statistical time is 3600s, and the third statistical time is 86400s. You can manually change any statistical period to [0, 86400]

[appid]

5 = 30, 60, 90 // The statistical duration of the appID is 5 is 30s 60s 90s 25 = 10, and the statistical duration of the appID is 60 // 25 is 10s 60s 86400s

[match_rule_name] // Specifies the rule name. Match_appid = 5 // Matched appID block_level = 1 // Block level, Rule_type = user // Key type of the rule item = min_interval >= 4 // A specific threshold expression can be greater than or equal to

Min_interval indicates the statistics of the first statistics period, mid_interval indicates the statistics of the second statistics period, and max_interval indicates the statistics of the third statistics period.Copy the code
  1. example
[appid] 100 = 30, 60, 90 [rule1] match_appid = 100 item = min_interval >= 10 block_level = 1 rule_type = user [rule2] match_appid = 100 item =  mid_interval >= 15 block_level = 1 rule_type = user [rule3] match_appid = 100 item = max_interval >= 20 block_level = 1  rule_type = user [rule4] match_appid = 100 item = min_interval >= 100 block_level = 1 rule_type = ip [rule5] match_appid = 100 item = mid_interval >= 150 block_level = 1 rule_type = ip [rule6] match_appid = 100 item = max_interval >= 200 block_level = 1 rule_type = ipCopy the code

API interface

Set the rule profile

void SetRuleConfFile(const char *filename);

Register a new statistical dimension. Type_name Indicates the type name, whether zero_init flag needs to be initialized to 0 during initialization, key indicates the shared memory key, item_cnt indicates the size of the shared memory, and the maximum number of items that can be counted.

int RegisterNewShmStat(const char* type_name, bool zero_init, key_t key, unsigned int item_cnt);

System initialization

bool InitFreq();

Report a frequency to the system. Type_name indicates the statistical type, indicating the meaning of key. Key is the principal ID, appID is the service ID, and CNT is the number of times to be accumulated. For example, ReportAndCheck(“user”, 3402393864, 5, 1) is ReportAndCheck(“user”, 3402393864, 5, 1) to calculate the frequency of user 3402393864 on appID 5. ReportAndCheck(“IP”, 3402393864, 5, 1); ReportAndCheck(“IP”, 3402393864, 1); Because 202.204.105.8 is also 3402393864. Type_name specifies the meaning of key.

struct BlockResult ReportAndCheck(const char* type_name, const char *key, const uint32_t appid, const uint32_t cnt);

Only check whether the current frequency is medium

struct BlockResult OnlyCheck(const char* type_name, const char *key, const uint32_t appid);

Report it, don’t check it

int OnlyReport(const char* type_name, const char *key, const uint32_t appid, const uint32_t cnt);

Obtain the frequency statistics. If CNT is 0, obtain the current statistics. If CNT is not 0, it means that the statistics are accumulated before being obtained

struct FreqCache GetCache(const char* type_name, const char *key, const uint32_t appid, const uint32_t cnt = 0);

Add whiten operation, linger_time indicates the add whiten validity period

int AddWhite(const char* type_name, const char *key, const uint32_t appid, const uint32_t linger_time);

Deleting a Whitelist

int DeleteWhite(const char* type_name, const char *key, const uint32_t appid);

Manual interception

int AddBlock(const char* type_name, const char *key, const uint32_t appid, const uint32_t linger_time, const uint32_t block_level);

Manually deleting interception

int DeleteBlock(const char* type_name, const char *key, const uint32_t appid);

The default log writing method is/TMP /libwxfreq.log

void SetLogFunc(LogFunction logfunc);

Set the recovery whitelist/blocking method on restart, read from the local file/TMP /libwxfreq_db.$type_name by default

void SetLoadFunc(LoadFunc func);

Set the whitelist/plusintercept persistence method. The default file is/TMP /libwxfreq_db.$type_name

void SetDumpFunc(DumpFunc func);