-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_utils.py
77 lines (64 loc) · 2.04 KB
/
common_utils.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ----------- Update ulimit -----------
try:
import resource
except:
resource = None
def set_ulimit():
if resource is None:
return
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
limit = hard - 1
for _ in range(1000):
try:
resource.setrlimit(resource.RLIMIT_NOFILE, (limit, hard))
break
except:
limit = limit // 2
# Eviction policies definition
class EvictionCfg:
EvictAny = "any"
EvictLRU = "lru"
EvictLFU = "lfu"
EvictFIFO = "fifo"
EvictNone = None
max_size_in_mb = 0
max_number_of_items = 0
invalidate_after_seconds = 0
def __init__(
self,
policy,
max_size_in_mb=0,
max_number_of_items=0,
invalidate_after_seconds=0,
):
self.policy = policy
self.max_size_in_mb = max_size_in_mb
self.max_number_of_items = max_number_of_items
self.invalidate_after_seconds = invalidate_after_seconds
if self.policy not in [
EvictionCfg.EvictAny,
EvictionCfg.EvictFIFO,
EvictionCfg.EvictLRU,
EvictionCfg.EvictLFU,
EvictionCfg.EvictNone,
]:
raise Exception("Invalid eviction policy")
if self.policy == EvictionCfg.EvictNone:
if (
self.max_size_in_mb
or self.max_number_of_items
or self.invalidate_after_seconds
):
raise Exception(
"EvictNone policy cannot have max_size_in_mb, max_number_of_items or invalidate_after_seconds configured"
)
if self.policy in {
EvictionCfg.EvictLRU,
EvictionCfg.EvictLFU,
EvictionCfg.EvictAny,
EvictionCfg.EvictFIFO,
}:
if not self.max_number_of_items and not self.max_size_in_mb:
raise Exception(
"EvictLRU, EvictLFU and EvictAny policies must have either max_size_in_mb or max_number_of_items configured"
)