Skip to content

Commit

Permalink
feat: return singleton success future for exactly-once methods in Mes…
Browse files Browse the repository at this point in the history
…sage (#608)

* Return singleton success future for exactly-once methods in subscriber.Message
  • Loading branch information
pradn committed Mar 9, 2022
1 parent a91bed8 commit 253ced2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
24 changes: 15 additions & 9 deletions google/cloud/pubsub_v1/subscriber/message.py
Expand Up @@ -40,6 +40,9 @@
attributes: {}
}}"""

_SUCCESS_FUTURE = futures.Future()
_SUCCESS_FUTURE.set_result(AcknowledgeStatus.SUCCESS)


def _indent(lines: str, prefix: str = " ") -> str:
"""Indent some text.
Expand Down Expand Up @@ -291,12 +294,13 @@ def ack_with_response(self) -> "futures.Future":
pubsub_v1.subscriber.exceptions.AcknowledgeError exception
will be thrown.
"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None
time_to_ack = math.ceil(time.time() - self._received_timestamp)
self._request_queue.put(
requests.AckRequest(
Expand Down Expand Up @@ -390,12 +394,13 @@ def modify_ack_deadline_with_response(self, seconds: int) -> "futures.Future":
will be thrown.
"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None

self._request_queue.put(
requests.ModAckRequest(
Expand Down Expand Up @@ -451,12 +456,13 @@ def nack_with_response(self) -> "futures.Future":
will be thrown.
"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None

self._request_queue.put(
requests.NackRequest(
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_message.py
Expand Up @@ -156,6 +156,7 @@ def test_ack_with_response_exactly_once_delivery_disabled():
)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.AckRequest)


Expand Down Expand Up @@ -205,6 +206,7 @@ def test_modify_ack_deadline_with_response_exactly_once_delivery_disabled():
requests.ModAckRequest(ack_id="bogus_ack_id", seconds=60, future=None)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.ModAckRequest)


Expand Down Expand Up @@ -242,6 +244,7 @@ def test_nack_with_response_exactly_once_delivery_disabled():
)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.NackRequest)


Expand Down

0 comments on commit 253ced2

Please sign in to comment.