-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.py
33 lines (28 loc) · 1 KB
/
timeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import time
from copy import deepcopy
TIMEOUT = 30
PINGDELAY = 10
class timeout:
def __init__(self,timeout=TIMEOUT,pingdelay=PINGDELAY):
self.pings = []
self.timeout = timeout
self.pingdelay = pingdelay
def ping(self,product):
"""
Check if same product with same sizes was already pinged in the last 10 seconds if so timeout product for 30 seconds.
"""
for ping in self.pings:
if ping["product"] == product:
if ping["timeout"] >= time.time():
return False
if ping["lastpingtimeout"] >= time.time():
ping["timeout"] = time.time()+self.timeout
return False
ping["lastpingtimeout"] = time.time()+self.pingdelay
return True
self.pings.append({
"product":deepcopy(product),
"lastpingtimeout":time.time()+self.pingdelay,
"timeout":-1
})
return True