Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 34ec7c0

Browse filesBrowse files
committed
无redis时使用内存做频率限制
1 parent 09e2888 commit 34ec7c0
Copy full SHA for 34ec7c0

1 file changed

+19-4Lines changed: 19 additions & 4 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎feapder/utils/tools.py‎

Copy file name to clipboardExpand all lines: feapder/utils/tools.py
+19-4Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def get_cookies(response):
225225
def get_cookies_from_str(cookie_str):
226226
"""
227227
>>> get_cookies_from_str("key=value; key2=value2; key3=; key4=")
228-
"{'key': 'value', 'key2': 'value2', 'key3': '', 'key4': ''}"
228+
{'key': 'value', 'key2': 'value2', 'key3': '', 'key4': ''}
229229
230230
Args:
231231
cookie_str: key=value; key2=value2; key3=; key4=
@@ -2255,6 +2255,7 @@ def re_def_supper_class(obj, supper_class):
22552255

22562256

22572257
###################
2258+
rate_limit_record = {}
22582259

22592260

22602261
def is_in_rate_limit(rate_limit, *key):
@@ -2269,10 +2270,24 @@ def is_in_rate_limit(rate_limit, *key):
22692270

22702271
msg_md5 = get_md5(*key)
22712272
key = "rate_limit:{}".format(msg_md5)
2272-
if get_redisdb().get(key):
2273-
return True
2273+
try:
2274+
if get_redisdb().get(key):
2275+
return True
2276+
2277+
get_redisdb().set(key, time.time(), ex=rate_limit)
2278+
except redis.exceptions.ConnectionError as e:
2279+
# 使用内存做频率限制
2280+
global rate_limit_record
2281+
2282+
if key not in rate_limit_record:
2283+
rate_limit_record[key] = time.time()
2284+
return False
2285+
2286+
if time.time() - rate_limit_record.get(key) < rate_limit:
2287+
return True
2288+
else:
2289+
rate_limit_record[key] = time.time()
22742290

2275-
get_redisdb().set(key, time.time(), ex=rate_limit)
22762291
return False
22772292

22782293

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.