-
Notifications
You must be signed in to change notification settings - Fork 9
/
load_balancer.py
270 lines (200 loc) · 6 KB
/
load_balancer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import asyncio, socket
import functools
import random, time
afi = socket.AF_INET
rr = random.random
import logging
from colors import *
from commons_static import *
from aiorun import run
async def ever():
while 1:
yield 1
afut = asyncio.Future
good_upstreams = {}
bad_upstreams = {}
class UpStream:
def __init__(self, h, p):
self.h = h
self.p = p
self.s = f'{h}:{p}'
self.lasthit = time.monotonic()
self.accumulator = 0
self.bad()
def accumulate(self, t):
self.accumulator = (self.accumulator + t) * 0.95
def good(self):
self.available = True
self.lasthit = time.monotonic() + self.accumulator*.1
ss = self.s
good_upstreams[ss] = self
if ss in bad_upstreams:
del bad_upstreams[ss]
def bad(self):
self.available = False
self.lasthit = time.monotonic() + self.accumulator*.1
ss = self.s
bad_upstreams[ss] = self
if ss in good_upstreams:
del good_upstreams[ss]
async def update_availability(self):
while 1:
if self.available == False:
k = await self.open()
if k is None:
print_err(f'failed to reach upstream {self.s}')
continue
print_info(f'upstream ok {self.s}')
ur, uw = k
await closew(uw)
else:
await asleep(0.2*rr())
async def open(self):
try:
conn = await make_conn(self.h, self.p)
except ConnectionRefusedError as e:
print_err('CRE', e)
self.bad()
return None
else:
self.good()
return conn
upstreams = [
UpStream('127.0.0.1', 5000),
UpStream('127.0.0.1', 5001),
# UpStream('127.0.0.1', 5002),
# UpStream('127.0.0.1', 5003),
]
def make_conn(h,p):
return asyncio.open_connection(
h, p, family=afi,
)
async def update_statuses():
await asyncio.gather(*(i.update_availability() for i in upstreams))
def get_one_upstream():
lgu = list(good_upstreams.values())
lgu.sort(key=lambda u:u.lasthit)
lu = len(lgu)
if lu:
return lgu[0]
lbu = list(bad_upstreams.values())
lbu.sort(key=lambda u:u.lasthit)
lu = len(lbu)
if lu:
return lbu[0]
return None
q = []
async def put_conn_in_q():
while 1:
if len(q) >= 40:
await asleep(0.05)
continue
upstream = get_one_upstream()
if upstream is None:
await asleep(0)
continue
conn = await upstream.open()
if conn is None:
continue
q.append((upstream, conn))
# print_info(f'-> q:{len(q)}')
def get_conn_from_q():
# print_down(f'<- q:{len(q)}')
while len(q):
first = q.pop(0)
upst, conn = first
r,w = conn
if r.at_eof() or w.is_closing():
print_err(f'<- q:{len(q)} (drop closed conn)')
continue
else:
return first
return None
asleep = asyncio.sleep
async def stream(r, w):
rateof = r.at_eof
rread = r.read
wdrain = w.drain
wwrite = w.write
nbytes = 0
firstline = None
while not rateof():
data = await rread(800000)
# qprint(f'{note} {len(data)} bytes')
if data:
wwrite(data)
await wdrain()
nbytes+=len(data)
if firstline is None:
idx = data.find(b'\r\n')
if idx<100:
firstline = data[:idx].decode('utf-8')
else:
firstline = '(toolong)'
# qprint(f'{firstline}')
await closew(w)
# qprint(f'{note} {nbytes:8d}')
return nbytes, firstline
conn_counter = 0
async def closew(dw):
dw.write_eof()
dw.close()
await dw.wait_closed()
async def tcp_lb_server(nretries=10):
async def client_conn_cb(dr, dw):
global conn_counter
conn_counter+=1
try:
ts = time.monotonic()
addr = dw.get_extra_info('peername')
ads = f'{addr[0]}:{addr[1]}'
retry_counter = 0
while True:
retry_counter+=1
if retry_counter > nretries:
print_err(f'#{conn_counter} exceeded max retries')
await closew(dw)
return False
elif retry_counter>1:
print_up(f'#{conn_counter} attempt #{retry_counter}')
await asleep(0.01*1.5**retry_counter*rr())
k = get_conn_from_q()
if k is None:
print_err(f'#{conn_counter} attempt #{retry_counter} no upstream available')
continue
upstream, conn = k
ur, uw = conn
break
up = stream(dr, uw)
down = stream(ur, dw)
cc = colored_info(f'#{str(conn_counter)}')
cc2 = (f'#{str(conn_counter)}')
qprint(cc,
f'{time_iso_now()} {ads} --> {upstream.h}:{upstream.p}',
colored_up(f'{upstream.accumulator:.3f}'))
(upb, ufl), (dnb, dfl) = await asyncio.gather(up, down)
elap = time.monotonic() - ts
qprint(cc2,
colored_up(f'{upb:7d} sent'),
colored_info(ufl),
colored_down(f'{dnb:7d} rcvd'),
colored_err(f'{int(elap*1000):5d} ms'),
colored_info(dfl))
upstream.accumulate(elap)
except Exception as e:
print_err('ccc',e)
server = await asyncio.start_server(
client_conn_cb,
host=lhost,
port=lport,
)
async with server:
await server.serve_forever()
lhost, lport = '0.0.0.0', 5100
async def main():
await asyncio.gather(
update_statuses(),
put_conn_in_q(),
tcp_lb_server(),
)
run(main(), stop_on_unhandled_errors=True)