|
| 1 | +from concurrent.futures.thread import ThreadPoolExecutor |
1 | 2 | from typing import Optional, Mapping, Set, AsyncIterator
|
2 | 3 | from uuid import uuid4
|
3 | 4 |
|
4 | 5 | from google.api_core.client_options import ClientOptions
|
5 | 6 | from google.auth.credentials import Credentials
|
6 |
| - |
| 7 | +from google.cloud.pubsub_v1.subscriber.futures import StreamingPullFuture |
7 | 8 | from google.cloud.pubsublite.cloudpubsub.flow_control_settings import FlowControlSettings
|
8 | 9 | from google.cloud.pubsublite.cloudpubsub.internal.ack_set_tracker_impl import AckSetTrackerImpl
|
9 | 10 | from google.cloud.pubsublite.cloudpubsub.internal.assigning_subscriber import PartitionSubscriberFactory, \
|
10 | 11 | AssigningSubscriber
|
11 | 12 | from google.cloud.pubsublite.cloudpubsub.internal.single_partition_subscriber import SinglePartitionSubscriber
|
| 13 | +import google.cloud.pubsublite.cloudpubsub.internal.subscriber_impl as cps_subscriber |
12 | 14 | from google.cloud.pubsublite.cloudpubsub.message_transformer import MessageTransformer, DefaultMessageTransformer
|
13 | 15 | from google.cloud.pubsublite.cloudpubsub.nack_handler import NackHandler, DefaultNackHandler
|
14 |
| -from google.cloud.pubsublite.cloudpubsub.subscriber import AsyncSubscriber |
| 16 | +from google.cloud.pubsublite.cloudpubsub.subscriber import AsyncSubscriber, MessageCallback |
15 | 17 | from google.cloud.pubsublite.endpoints import regional_endpoint
|
16 | 18 | from google.cloud.pubsublite.internal.wire.assigner import Assigner
|
17 | 19 | from google.cloud.pubsublite.internal.wire.assigner_impl import AssignerImpl
|
|
20 | 22 | from google.cloud.pubsublite.internal.wire.gapic_connection import GapicConnectionFactory
|
21 | 23 | from google.cloud.pubsublite.internal.wire.merge_metadata import merge_metadata
|
22 | 24 | from google.cloud.pubsublite.internal.wire.pubsub_context import pubsub_context
|
23 |
| -from google.cloud.pubsublite.internal.wire.subscriber_impl import SubscriberImpl |
| 25 | +import google.cloud.pubsublite.internal.wire.subscriber_impl as wire_subscriber |
24 | 26 | from google.cloud.pubsublite.partition import Partition
|
25 | 27 | from google.cloud.pubsublite.paths import SubscriptionPath
|
26 | 28 | from google.cloud.pubsublite.routing_metadata import subscription_routing_metadata
|
@@ -63,14 +65,14 @@ def subscribe_connection_factory(requests: AsyncIterator[SubscribeRequest]):
|
63 | 65 | def cursor_connection_factory(requests: AsyncIterator[StreamingCommitCursorRequest]):
|
64 | 66 | return cursor_client.streaming_commit_cursor(requests, metadata=list(final_metadata.items()))
|
65 | 67 |
|
66 |
| - wire_subscriber = SubscriberImpl( |
| 68 | + subscriber = wire_subscriber.SubscriberImpl( |
67 | 69 | InitialSubscribeRequest(subscription=str(subscription), partition=partition.value),
|
68 | 70 | _DEFAULT_FLUSH_SECONDS, GapicConnectionFactory(subscribe_connection_factory))
|
69 | 71 | committer = CommitterImpl(
|
70 | 72 | InitialCommitCursorRequest(subscription=str(subscription), partition=partition.value),
|
71 | 73 | _DEFAULT_FLUSH_SECONDS, GapicConnectionFactory(cursor_connection_factory))
|
72 | 74 | ack_set_tracker = AckSetTrackerImpl(committer)
|
73 |
| - return SinglePartitionSubscriber(wire_subscriber, flow_control_settings, ack_set_tracker, nack_handler, |
| 75 | + return SinglePartitionSubscriber(subscriber, flow_control_settings, ack_set_tracker, nack_handler, |
74 | 76 | message_transformer)
|
75 | 77 |
|
76 | 78 | return factory
|
@@ -124,3 +126,46 @@ def make_async_subscriber(
|
124 | 126 | metadata, per_partition_flow_control_settings,
|
125 | 127 | nack_handler, message_transformer)
|
126 | 128 | return AssigningSubscriber(assigner, partition_subscriber_factory)
|
| 129 | + |
| 130 | + |
| 131 | +def make_subscriber( |
| 132 | + subscription: SubscriptionPath, |
| 133 | + per_partition_flow_control_settings: FlowControlSettings, |
| 134 | + callback: MessageCallback, |
| 135 | + nack_handler: Optional[NackHandler] = None, |
| 136 | + message_transformer: Optional[MessageTransformer] = None, |
| 137 | + fixed_partitions: Optional[Set[Partition]] = None, |
| 138 | + executor: Optional[ThreadPoolExecutor] = None, |
| 139 | + credentials: Optional[Credentials] = None, |
| 140 | + client_options: Optional[ClientOptions] = None, |
| 141 | + metadata: Optional[Mapping[str, str]] = None) -> StreamingPullFuture: |
| 142 | + """ |
| 143 | + Make a Pub/Sub Lite Subscriber. |
| 144 | +
|
| 145 | + Args: |
| 146 | + subscription: The subscription to subscribe to. |
| 147 | + per_partition_flow_control_settings: The flow control settings for each partition subscribed to. Note that these |
| 148 | + settings apply to each partition individually, not in aggregate. |
| 149 | + callback: The callback to call with each message. |
| 150 | + nack_handler: An optional handler for when nack() is called on a Message. The default will fail the client. |
| 151 | + message_transformer: An optional transformer from Pub/Sub Lite messages to Cloud Pub/Sub messages. |
| 152 | + fixed_partitions: A fixed set of partitions to subscribe to. If not present, will instead use auto-assignment. |
| 153 | + executor: The executor to use for user callbacks. If not provided, will use the default constructed |
| 154 | + ThreadPoolExecutor. If provided a single threaded executor, messages will be ordered per-partition, but take care |
| 155 | + that the callback does not block for too long as it will impede forward progress on all partitions. |
| 156 | + credentials: The credentials to use to connect. GOOGLE_DEFAULT_CREDENTIALS is used if None. |
| 157 | + client_options: Other options to pass to the client. Note that if you pass any you must set api_endpoint. |
| 158 | + metadata: Additional metadata to send with the RPC. |
| 159 | +
|
| 160 | + Returns: |
| 161 | + A StreamingPullFuture, managing the subscriber's lifetime. |
| 162 | + """ |
| 163 | + underlying = make_async_subscriber( |
| 164 | + subscription, per_partition_flow_control_settings, nack_handler, message_transformer, fixed_partitions, credentials, |
| 165 | + client_options, metadata) |
| 166 | + if executor is None: |
| 167 | + executor = ThreadPoolExecutor() |
| 168 | + subscriber = cps_subscriber.SubscriberImpl(underlying, callback, executor) |
| 169 | + future = StreamingPullFuture(subscriber) |
| 170 | + subscriber.__enter__() |
| 171 | + return future |
0 commit comments