forked from influxdata/influxdb-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrx_playground.py
189 lines (138 loc) · 5.48 KB
/
rx_playground.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
import datetime
import time
from random import random
from threading import current_thread
import reactivex as rx
from reactivex import operators as ops, GroupedObservable
from reactivex.scheduler import ThreadPoolScheduler
from reactivex.subject import Subject
class _WriterKey(object):
def __init__(self, key):
self.key = key
def __hash__(self) -> int:
return hash((self.key, self.key))
def __eq__(self, o: object) -> bool:
return isinstance(o, self.__class__) and self.key == o.key and self.key == o.key
def __str__(self) -> str:
return 'key[\'{}\']'.format(self.key)
class _Notification(object):
def __init__(self, data, exception=None):
self.data = data
self.exception = exception
pass
def __str__(self) -> str:
return '_Notification[status:\'{}\', \'{}\']' \
.format("failed" if self.exception else "success", self.data)
class _RxWriter(object):
success_count = 0
failed_count = 0
raise_retry_exception = 0
def __init__(self) -> None:
self._subject = Subject()
self._scheduler = ThreadPoolScheduler(max_workers=1)
obs = self._subject.pipe(ops.observe_on(self._scheduler))
self._disposable = obs \
.pipe(ops.window_with_time_or_count(count=5, timespan=datetime.timedelta(milliseconds=10_000)),
ops.flat_map(lambda x: self._window_to_group(x)),
ops.map(mapper=lambda x: self._retryable(data=x, delay=self._jitter_delay(jitter_interval=1000))),
ops.merge_all()) \
.subscribe(self._result, self._error, self._on_complete)
pass
def close(self):
self.__del__()
def __del__(self):
if self._subject:
self._subject.on_completed()
self._subject.dispose()
self._subject = None
while not self._disposable.is_disposed:
time.sleep(0.1)
if self._disposable:
self._disposable = None
pass
def _window_to_group(self, value):
return value.pipe(
ops.to_iterable(),
ops.map(lambda x: rx.from_iterable(x).pipe(
ops.group_by(_group_by), ops.map(_group_to_batch), ops.merge_all())),
ops.merge_all())
def _retryable(self, data: str, delay: datetime.timedelta):
return rx.of(data).pipe(
ops.delay(duetime=delay, scheduler=self._scheduler),
ops.map(lambda x: self._http(x)),
ops.catch(handler=lambda exception, source: self._retry_handler(exception, source, data)),
)
def _http(self, data: str):
if "gamma" in data:
print('bad request[{}]: {}'.format(current_thread().name, data))
raise Exception('unexpected token: {}'.format(data))
pass
if "alpha" in data:
if self.raise_retry_exception < 2:
self.raise_retry_exception += 1
print('server is temporarily unavailable to accept writes[{}]: {}'.format(current_thread().name, data))
raise Exception('server is temporarily unavailable to accept writes: {}'.format(data))
else:
print("server is OK: {}".format(datetime.datetime.now()))
pass
print("http[" + current_thread().name + "]: " + data)
return _Notification(data=data)
def write(self, data: str):
print("write[" + current_thread().name + "]")
self._subject.on_next(data)
pass
def _result(self, data: _Notification):
print("result[" + current_thread().name + "]: " + str(data))
if data.exception:
self.failed_count += 1
else:
self.success_count += 1
pass
def _error(self, error):
print(error)
def _on_complete(self):
self._disposable.dispose()
print("on complete")
def _jitter_delay(self, jitter_interval=0):
_jitter = datetime.timedelta(milliseconds=random() * jitter_interval)
print('jitter: {}'.format(_jitter))
return _jitter
def _retry_handler(self, exception, source, data):
print('retry_handler: {}, source: {}'.format(exception, source))
if "server is temporarily" in str(exception):
print("RETRY!!!: {}".format(datetime.datetime.now()))
return self._retryable(data, delay=datetime.timedelta(seconds=2))
notification = _Notification(exception=exception, data=data)
return rx.just(notification)
def _create_batch(group: GroupedObservable):
return lambda xs: '{}: {}'.format(str(group.key), ', '.join(xs))
def _group_by(v):
# print("_group_by[" + current_thread().name + "]")
return _WriterKey(v[0])
def _group_to_batch(group: GroupedObservable):
return group.pipe(ops.to_iterable(),
ops.map(list),
ops.map(_create_batch(group)))
rxWriter = _RxWriter()
print("\n== init[" + current_thread().name + "] ==\n")
rxWriter.write("alpha")
rxWriter.write("beta")
rxWriter.write("brick")
rxWriter.write("racket")
rxWriter.write("bat")
rxWriter.write("apple")
rxWriter.write("gamma")
time.sleep(2)
rxWriter.write("apricot")
rxWriter.write("root")
rxWriter.write("delta")
rxWriter.write("double")
rxWriter.write("backpack")
rxWriter.write("giant")
rxWriter.write("balloon")
print("\n== finish writing ==\n")
time.sleep(5)
print("\n== close ==\n")
rxWriter.close()
print("\n== finished ==\n")
print('success: {}, failed: {}'.format(rxWriter.success_count, rxWriter.failed_count))