forked from jamesward/easyracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
195 lines (138 loc) · 5.12 KB
/
main.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from typing import Optional
import httpx
import trio
def url(port: int, scenario: int, param: Optional[str] = None):
base_url = f'http://localhost:{port}/{scenario}'
if param:
return base_url + f'?{param}'
return base_url
# Note: Request creation code is intentionally not shared across scenarios
async def race(*async_fns):
if not async_fns:
raise ValueError("must pass at least one argument")
winner = None
async def jockey(async_fn, cancel_scope):
try:
nonlocal winner
print("trying")
winner = await async_fn()
print("winner", winner)
cancel_scope.cancel()
except BaseException as e:
print(f"{e=}")
# pass
async with trio.open_nursery() as tasks:
for async_fn in async_fns:
tasks.start_soon(jockey, async_fn, tasks.cancel_scope)
return winner
async def scenario1(port: int):
async with httpx.AsyncClient() as client:
async def req():
response = await client.get(url(port, 1))
return response.text
return await race(req, req)
async def scenario2(port: int):
async with httpx.AsyncClient() as client:
async def req():
response = await client.get(url(port, 2))
return response.text
return await race(req, req)
# currently not working due to some limit of 1016 concurrent connections
async def scenario3(port: int):
with trio.move_on_after(30): # timeout to prevent this from hanging
limits = httpx.Limits(max_connections=None)
async with httpx.AsyncClient(limits=limits) as client:
async def req():
response = await client.get(url(port, 3))
return response.text
return await race(*[req for _ in range(10_000)])
async def scenario4(port: int):
async with httpx.AsyncClient() as client:
async def req():
print("req")
response = await client.get(url(port, 4))
print(f"{response=}")
return response.text
async def req_with_timeout():
with trio.fail_after(1):
return await req()
return await race(req_with_timeout, req)
async def scenario5(port: int):
async with httpx.AsyncClient() as client:
async def req():
response = await client.get(url(port, 5))
if response.status_code != 200:
raise Exception("invalid response")
return response.text
return await race(req, req)
async def scenario6(port: int):
async with httpx.AsyncClient() as client:
async def req():
response = await client.get(url(port, 6))
if response.status_code != 200:
raise Exception("invalid response")
return response.text
return await race(req, req, req)
async def scenario7(port: int):
async with httpx.AsyncClient() as client:
async def req():
response = await client.get(url(port, 7))
return response.text
async def hedge_req():
await trio.sleep(3)
return await req()
return await race(req, hedge_req)
async def scenario8(port: int):
async with httpx.AsyncClient() as client:
async def req(param: str):
response = await client.get(url(port, 8, param))
if response.status_code != 200:
raise Exception("invalid response")
return response.text
async def open_req():
return await req("open")
async def use_req(my_id: str):
return await req(f'use={my_id}')
async def close_req(my_id: str):
return await req(f'close={my_id}')
async def closeable_req():
my_id = await open_req()
try:
resp = await use_req(my_id)
finally:
await close_req(my_id)
return resp
return await race(closeable_req, closeable_req)
async def scenario9(port: int):
async with httpx.AsyncClient() as client:
send_channel, receive_channel = trio.open_memory_channel(10)
async def req():
response = await client.get(url(port, 9))
if response.status_code == 200:
await send_channel.send(response.text)
async with send_channel:
async with trio.open_nursery() as nursery:
[nursery.start_soon(req) for _ in range(10)]
async with receive_channel:
return "".join([letter async for letter in receive_channel])
async def main():
result1 = await scenario1(8080)
print(result1)
result2 = await scenario2(8080)
print(result2)
# result3 = await scenario3(8080)
# print(result3)
result4 = await scenario4(8080)
print(result4)
result5 = await scenario5(8080)
print(result5)
result6 = await scenario6(8080)
print(result6)
result7 = await scenario7(8080)
print(result7)
result8 = await scenario8(8080)
print(result8)
result9 = await scenario9(8080)
print(result9)
if __name__ == "__main__":
trio.run(main)