This repository was archived by the owner on May 5, 2022. It is now read-only.
forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_consumer.py
More file actions
109 lines (91 loc) · 3.31 KB
/
Copy pathevent_consumer.py
File metadata and controls
109 lines (91 loc) · 3.31 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
from __future__ import absolute_import
import errno
from threading import Thread
import jsonpickle
import requests
from requests.packages.urllib3.exceptions import ProtocolError
from ldclient.interfaces import EventConsumer
from ldclient.util import _headers
from ldclient.util import log
class EventConsumerImpl(Thread, EventConsumer):
def __init__(self, event_queue, config):
Thread.__init__(self)
self._session = requests.Session()
self.daemon = True
self._config = config
self._queue = event_queue
self._running = True
def run(self):
log.info("Starting event consumer")
self._running = True
while self._running:
try:
self.send()
except Exception:
log.exception(
'Unhandled exception in event consumer')
def stop(self):
self._running = False
def flush(self):
self._queue.join()
def send_batch(self, events):
def do_send(should_retry):
# noinspection PyBroadException
try:
if isinstance(events, dict):
body = [events]
else:
body = events
json_body = jsonpickle.encode(body, unpicklable=False)
log.debug('Sending events payload: ' + json_body)
hdrs = _headers(self._config.sdk_key)
uri = self._config.events_uri
r = self._session.post(uri,
headers=hdrs,
timeout=(self._config.connect_timeout, self._config.read_timeout),
data=json_body)
r.raise_for_status()
except ProtocolError as e:
if e.args is not None and len(e.args) > 1 and e.args[1] is not None:
inner = e.args[1]
if inner.errno is not None and inner.errno == errno.ECONNRESET and should_retry:
log.warning(
'ProtocolError exception caught while sending events. Retrying.')
do_send(False)
else:
log.exception(
'Unhandled exception in event consumer. Analytics events were not processed.')
except:
log.exception(
'Unhandled exception in event consumer. Analytics events were not processed.')
try:
do_send(True)
finally:
for _ in events:
self._queue.task_done()
def send(self):
events = self.next()
if len(events) == 0:
return
else:
self.send_batch(events)
def next(self):
q = self._queue
items = []
item = self.next_item()
if item is None:
return items
items.append(item)
while len(items) < self._config.events_upload_max_batch_size and not q.empty():
item = self.next_item()
if item:
items.append(item)
return items
def next_item(self):
q = self._queue
# noinspection PyBroadException
try:
item = q.get(block=True, timeout=5)
return item
except Exception:
return None