|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | + |
| 5 | +from pynumaflow.proto.common import nack_options_pb2 |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class NackOptions: |
| 10 | + """Per-message redelivery options for a nack. |
| 11 | +
|
| 12 | + Args: |
| 13 | + delay: the redelivery delay in milliseconds. |
| 14 | + max_deliveries: the maximum number of redelivery attempts. |
| 15 | + reason: a human-readable reason for nacking the message. |
| 16 | + nack_map: a generic map of string key-value pairs used to pass |
| 17 | + properties/configurations for nacking back to the source (e.g. for |
| 18 | + sources like SQS, Pulsar or JetStream that support NACK). |
| 19 | + """ |
| 20 | + |
| 21 | + delay: int | None = None |
| 22 | + max_deliveries: int | None = None |
| 23 | + reason: str | None = None |
| 24 | + nack_map: dict[str, str] = field(default_factory=dict) |
| 25 | + |
| 26 | + def _to_proto(self) -> nack_options_pb2.NackOptions: |
| 27 | + return nack_options_pb2.NackOptions( |
| 28 | + reason=self.reason, |
| 29 | + max_deliveries=self.max_deliveries, |
| 30 | + delay=self.delay, |
| 31 | + nack_map=self.nack_map, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def _nack_options_to_proto( |
| 36 | + opts: NackOptions | None, |
| 37 | +) -> nack_options_pb2.NackOptions | None: |
| 38 | + if opts is None: |
| 39 | + return None |
| 40 | + return opts._to_proto() |
| 41 | + |
| 42 | + |
| 43 | +def _nack_options_from_proto( |
| 44 | + proto: nack_options_pb2.NackOptions, |
| 45 | +) -> NackOptions: |
| 46 | + return NackOptions( |
| 47 | + delay=proto.delay if proto.HasField("delay") else None, |
| 48 | + max_deliveries=proto.max_deliveries if proto.HasField("max_deliveries") else None, |
| 49 | + reason=proto.reason if proto.HasField("reason") else None, |
| 50 | + # proto3 map fields do not support HasField; an unset map is simply empty. |
| 51 | + nack_map=dict(proto.nack_map), |
| 52 | + ) |
0 commit comments