-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_hierarchy.py
More file actions
110 lines (92 loc) · 3.48 KB
/
example_hierarchy.py
File metadata and controls
110 lines (92 loc) · 3.48 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from bucketflow.hierarchical import HierarchicalTokenBucket, create_bucket_hierarchy
import time
def simple_hierarchy_example():
"""
Demonstrate a simple hierarchy with a root bucket and child buckets.
"""
print("Simple Hierarchy Example")
# Create a root bucket with 100 tokens, filling at 10 tokens per second
root = HierarchicalTokenBucket(capacity=100, fill_rate=10, name="root")
# Create child buckets
user1 = HierarchicalTokenBucket(capacity=20, fill_rate=2, parent=root, name="user1")
user2 = HierarchicalTokenBucket(capacity=30, fill_rate=3, parent=root, name="user2")
# Create nested child bucket
api1 = HierarchicalTokenBucket(capacity=10, fill_rate=1, parent=user2, name="api1")
print(f"Root tokens: {root.tokens}")
print(f"User1 tokens: {user1.tokens}")
print(f"User2 tokens: {user2.tokens}")
print(f"API1 tokens: {api1.tokens}")
print("\nConsuming 15 tokens from user1...")
success = user1.consume(15)
print(f"Success: {success}")
print(f"Root tokens after: {root.tokens}")
print(f"User1 tokens after: {user1.tokens}")
print("\nConsuming 8 tokens from api1...")
success = api1.consume(8)
print(f"Success: {success}")
print(f"Root tokens after: {root.tokens}")
print(f"User2 tokens after: {user2.tokens}")
print(f"API1 tokens after: {api1.tokens}")
# Try to consume more than available at the lowest level
print("\nTrying to consume 5 more tokens from api1 (should fail)...")
success = api1.consume(5, block=False)
print(f"Success: {success}")
# Try with blocking
print("\nTrying to consume 5 more tokens from api1 with blocking...")
start = time.time()
success = api1.consume(5, block=True)
end = time.time()
print(f"Success: {success}")
print(f"Blocked for {end - start:.2f} seconds")
print(f"API1 tokens after: {api1.tokens}")
def factory_example():
"""
Demonstrate creating a hierarchy using the factory function.
"""
print("\nFactory Function Example")
# Define a hierarchy configuration
config = {
"name": "global",
"capacity": 100,
"fill_rate": 10,
"children": [
{
"name": "service1",
"capacity": 40,
"fill_rate": 4,
"children": [
{
"name": "endpoint1",
"capacity": 15,
"fill_rate": 1.5
},
{
"name": "endpoint2",
"capacity": 25,
"fill_rate": 2.5
}
]
},
{
"name": "service2",
"capacity": 60,
"fill_rate": 6
}
]
}
# Create the hierarchy
buckets = create_bucket_hierarchy(config)
# Use the buckets
print("Available buckets:", ", ".join(buckets.keys()))
# Consume from an endpoint
endpoint = buckets["endpoint1"]
print(f"\nConsuming 10 tokens from {endpoint.name}...")
success = endpoint.consume(10)
print(f"Success: {success}")
# Check tokens at each level
print(f"Global tokens: {buckets['global'].tokens}")
print(f"Service1 tokens: {buckets['service1'].tokens}")
print(f"Endpoint1 tokens: {buckets['endpoint1'].tokens}")
if __name__ == "__main__":
simple_hierarchy_example()
factory_example()