|
41 | 41 | STOP_ASYNC_PRODUCER = -1
|
42 | 42 | ASYNC_STOP_TIMEOUT_SECS = 30
|
43 | 43 |
|
| 44 | +SYNC_FAIL_ON_ERROR_DEFAULT = True |
| 45 | + |
44 | 46 |
|
45 | 47 | def _send_upstream(queue, client, codec, batch_time, batch_size,
|
46 | 48 | req_acks, ack_timeout, retry_options, stop_event,
|
@@ -216,6 +218,9 @@ class Producer(object):
|
216 | 218 | defaults to 1 (local ack).
|
217 | 219 | ack_timeout (int, optional): millisecond timeout to wait for the
|
218 | 220 | configured req_acks, defaults to 1000.
|
| 221 | + sync_fail_on_error (bool, optional): whether sync producer should |
| 222 | + raise exceptions (True), or just return errors (False), |
| 223 | + defaults to True. |
219 | 224 | async (bool, optional): send message using a background thread,
|
220 | 225 | defaults to False.
|
221 | 226 | batch_send_every_n (int, optional): If async is True, messages are
|
@@ -258,6 +263,7 @@ def __init__(self, client,
|
258 | 263 | req_acks=ACK_AFTER_LOCAL_WRITE,
|
259 | 264 | ack_timeout=DEFAULT_ACK_TIMEOUT,
|
260 | 265 | codec=None,
|
| 266 | + sync_fail_on_error=SYNC_FAIL_ON_ERROR_DEFAULT, |
261 | 267 | async=False,
|
262 | 268 | batch_send=False, # deprecated, use async
|
263 | 269 | batch_send_every_n=BATCH_SEND_MSG_COUNT,
|
@@ -316,6 +322,8 @@ def cleanup(obj):
|
316 | 322 | obj.stop()
|
317 | 323 | self._cleanup_func = cleanup
|
318 | 324 | atexit.register(cleanup, self)
|
| 325 | + else: |
| 326 | + self.sync_fail_on_error = sync_fail_on_error |
319 | 327 |
|
320 | 328 | def send_messages(self, topic, partition, *msg):
|
321 | 329 | """
|
@@ -373,8 +381,10 @@ def _send_messages(self, topic, partition, *msg, **kwargs):
|
373 | 381 | messages = create_message_set([(m, key) for m in msg], self.codec, key)
|
374 | 382 | req = ProduceRequest(topic, partition, messages)
|
375 | 383 | try:
|
376 |
| - resp = self.client.send_produce_request([req], acks=self.req_acks, |
377 |
| - timeout=self.ack_timeout) |
| 384 | + resp = self.client.send_produce_request( |
| 385 | + [req], acks=self.req_acks, timeout=self.ack_timeout, |
| 386 | + fail_on_error=self.sync_fail_on_error |
| 387 | + ) |
378 | 388 | except Exception:
|
379 | 389 | log.exception("Unable to send messages")
|
380 | 390 | raise
|
|
0 commit comments