forked from dpkp/kafka-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from kafka import * | ||
|
||
from kafka.green.producer import _Producer, _SimpleProducer, _KeyedProducer | ||
from kafka.green.conn import _KafkaConnection | ||
from kafka.green.client import _KafkaClient | ||
|
||
Producer=_Producer | ||
SimpleProducer=_SimpleProducer | ||
KeyedProducer=_KeyedProducer | ||
KafkaConnection=_KafkaConnection | ||
KafkaClient=_KafkaClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from kafka.client import KafkaClient, DEFAULT_SOCKET_TIMEOUT_SECONDS | ||
|
||
from .conn import _KafkaConnection | ||
|
||
class _KafkaClient(KafkaClient): | ||
|
||
def __init__(self, hosts, client_id=KafkaClient.CLIENT_ID, | ||
timeout=DEFAULT_SOCKET_TIMEOUT_SECONDS): | ||
super(_KafkaClient, self).__init__(hosts=hosts, client_id=client_id, timeout=timeout) | ||
|
||
def copy(self): | ||
# have to override this since copy.deepcopy cannot serialize | ||
# a gevent.socket | ||
return _KafkaClient(self.hosts, self.client_id, self.timeout) | ||
|
||
def _get_conn(self, host, port): | ||
"Get or create a connection to a broker using host and port" | ||
host_key = (host, port) | ||
|
||
if host_key not in self.conns: | ||
self.conns[host_key] = _KafkaConnection( | ||
host, | ||
port, | ||
timeout=self.timeout | ||
) | ||
|
||
return self.conns[host_key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import gevent.socket as socket | ||
import logging | ||
|
||
from kafka.conn import KafkaConnection | ||
|
||
log = logging.getLogger("kafka") | ||
|
||
class _KafkaConnection(KafkaConnection): | ||
""" | ||
Gevent version of kafka.KafkaConnection class. Uses | ||
gevent.socket instead of socket.socket. | ||
""" | ||
def __init__(self, host, port, timeout=10): | ||
super(_KafkaConnection, self).__init__(host, port, timeout) | ||
|
||
def reinit(self): | ||
""" | ||
Re-initialize the socket connection | ||
""" | ||
log.debug("Reinitializing socket connection for %s:%d" % (self.host, self.port)) | ||
|
||
if self._sock: | ||
self.close() | ||
|
||
try: | ||
self._sock = socket.create_connection((self.host, self.port), self.timeout) | ||
except socket.error: | ||
log.exception('Unable to connect to kafka broker at %s:%d' % (self.host, self.port)) | ||
self._raise_connection_error() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from kafka.producer.base import Producer, _send_upstream, STOP_ASYNC_PRODUCER | ||
from kafka.producer.simple import SimpleProducer | ||
from kafka.producer.keyed import KeyedProducer | ||
|
||
import gevent | ||
from gevent.queue import Queue | ||
|
||
|
||
class _ProducerMixin(object): | ||
|
||
def _setup_async(self, batch_send_every_t, batch_send_every_n): | ||
self.queue = Queue() # Messages are sent through this queue | ||
self.proc = gevent.spawn(_send_upstream, | ||
self.queue, | ||
self.client.copy(), | ||
self.codec, | ||
batch_send_every_t, | ||
batch_send_every_n, | ||
self.req_acks, | ||
self.ack_timeout) | ||
|
||
def stop(self, timeout=1): | ||
if self.async: | ||
self.queue.put((STOP_ASYNC_PRODUCER, None)) | ||
self.proc.join(timeout) | ||
if self.proc.dead is False: | ||
self.proc.kill() | ||
|
||
|
||
class _Producer(_ProducerMixin, Producer): | ||
pass | ||
|
||
|
||
class _SimpleProducer(_ProducerMixin, SimpleProducer): | ||
pass | ||
|
||
|
||
class _KeyedProducer(_ProducerMixin, KeyedProducer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters