-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
kafka.py
70 lines (58 loc) · 2.54 KB
/
kafka.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
'''
Copyright (C) 2018-2019 Bryant Moscon - bmoscon@gmail.com
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
import json
import logging
from cryptofeed.defines import L2_BOOK, L3_BOOK, TRADES, TICKER
from cryptostore.engines import StorageEngines
from cryptostore.aggregator.cache import Cache
from cryptostore.aggregator.util import book_flatten
LOG = logging.getLogger('cryptostore')
class Kafka(Cache):
def __init__(self, ip, port, flush=False):
self.conn = {}
self.ip = ip
self.port = port
self.ids = {}
if flush:
kafka = StorageEngines['confluent_kafka.admin']
ac = kafka.admin.AdminClient({'bootstrap.servers': f"{ip}:{port}"})
topics = list(ac.list_topics().topics.keys())
for topic, status in ac.delete_topics(topics).items():
try:
status.result()
LOG.info("Topic %s deleted", topic)
except Exception as e:
LOG.warning("Failed to delete topic %s: %s", topic, e)
def _conn(self, key):
if key not in self.conn:
self.ids[key] = None
kafka = StorageEngines.confluent_kafka
self.conn[key] = kafka.Consumer({'bootstrap.servers': f"{self.ip}:{self.port}",
'client.id': f'cryptostore-{key}',
'enable.auto.commit': False,
'group.id': f'cryptofeed-{key}',
'max.poll.interval.ms': 3000000})
self.conn[key].subscribe([key])
return self.conn[key]
def read(self, exchange, dtype, pair):
key = f'{dtype}-{exchange}-{pair}'
data = self._conn(key).consume(1000000, timeout=0.5)
LOG.info("%s: Read %d messages from Kafka", key, len(data))
ret = []
for message in data:
self.ids[key] = message
update = json.loads(message.value().decode('utf8'))
if dtype in {L2_BOOK, L3_BOOK}:
update = book_flatten(update, update['timestamp'], update['delta'])
ret.extend(update)
if dtype in {TRADES, TICKER}:
ret.append(update)
return ret
def delete(self, exchange, dtype, pair):
key = f'{dtype}-{exchange}-{pair}'
LOG.info("%s: Committing offset %d", key, self.ids[key].offset())
self._conn(key).commit(message=self.ids[key])
self.ids[key] = None