Created
January 31, 2023 01:01
-
-
Save haynesgt/c73686213d816be7d39457f2268a7f8d to your computer and use it in GitHub Desktop.
pubsub ordering key testing
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
from random import randint | |
from google.cloud.pubsub_v1 import SubscriberClient, PublisherClient | |
from google.cloud.pubsub_v1.types import PublisherOptions | |
from google.pubsub_v1.types.pubsub import ( | |
AcknowledgeRequest, | |
ModifyAckDeadlineRequest, | |
PubsubMessage, | |
PullRequest, | |
PullResponse, | |
) | |
publisher_client = PublisherClient(publisher_options=PublisherOptions( | |
enable_message_ordering=True | |
)) | |
topic = "projects/klue-dev-310020/topics/gavin-test-topic" | |
def publish(): | |
publisher_client.publish( | |
topic=topic, | |
data=b"abc123", | |
ordering_key=f"{randint(0, 10)}" | |
) | |
subscriber_client = SubscriberClient() | |
subscription = "projects/klue-dev-310020/subscriptions/gavin-test-sub" | |
def fetch(): | |
pull_response: PullResponse = subscriber_client.pull( | |
request=PullRequest( | |
subscription=subscription, | |
max_messages=10, | |
) | |
) | |
print(len(pull_response.received_messages)) | |
ack_ids = [msg.ack_id for msg in pull_response.received_messages] | |
if ack_ids: | |
subscriber_client.acknowledge( | |
AcknowledgeRequest( | |
subscription=subscription, | |
ack_ids=ack_ids, | |
) | |
) | |
return pull_response | |
for i in range(0, 3000): publish() | |
# Run this in many terminals simultaneously for best results | |
responses = [fetch() for i in range(0, 100)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment