/
publisher_client.py
182 lines (159 loc) · 6.37 KB
/
publisher_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from concurrent.futures import Future
from typing import Optional, Mapping, Union
from google.api_core.client_options import ClientOptions
from google.auth.credentials import Credentials
from google.cloud.pubsub_v1.types import BatchSettings
from google.cloud.pubsublite.cloudpubsub.internal.make_publisher import (
make_publisher,
make_async_publisher,
)
from google.cloud.pubsublite.cloudpubsub.internal.multiplexed_async_publisher_client import (
MultiplexedAsyncPublisherClient,
)
from google.cloud.pubsublite.cloudpubsub.internal.multiplexed_publisher_client import (
MultiplexedPublisherClient,
)
from google.cloud.pubsublite.cloudpubsub.publisher_client_interface import (
PublisherClientInterface,
AsyncPublisherClientInterface,
)
from google.cloud.pubsublite.internal.constructable_from_service_account import (
ConstructableFromServiceAccount,
)
from google.cloud.pubsublite.internal.require_started import RequireStarted
from google.cloud.pubsublite.internal.wire.make_publisher import (
DEFAULT_BATCHING_SETTINGS as WIRE_DEFAULT_BATCHING,
)
from google.cloud.pubsublite.types import TopicPath
from overrides import overrides
class PublisherClient(PublisherClientInterface, ConstructableFromServiceAccount):
"""
A PublisherClient publishes messages similar to Google Pub/Sub.
Any publish failures are unlikely to succeed if retried.
Must be used in a `with` block or have __enter__() called before use.
"""
_impl: PublisherClientInterface
_require_stared: RequireStarted
DEFAULT_BATCHING_SETTINGS = WIRE_DEFAULT_BATCHING
"""
The default batching settings for a publisher client.
"""
def __init__(
self,
per_partition_batching_settings: Optional[BatchSettings] = None,
credentials: Optional[Credentials] = None,
transport: str = "grpc_asyncio",
client_options: Optional[ClientOptions] = None,
):
"""
Create a new PublisherClient.
Args:
per_partition_batching_settings: The settings for publish batching. Apply on a per-partition basis.
credentials: If provided, the credentials to use when connecting.
transport: The transport to use. Must correspond to an asyncio transport.
client_options: The client options to use when connecting. If used, must explicitly set `api_endpoint`.
"""
self._impl = MultiplexedPublisherClient(
lambda topic: make_publisher(
topic=topic,
per_partition_batching_settings=per_partition_batching_settings,
credentials=credentials,
client_options=client_options,
transport=transport,
)
)
self._require_stared = RequireStarted()
@overrides
def publish(
self,
topic: Union[TopicPath, str],
data: bytes,
ordering_key: str = "",
**attrs: Mapping[str, str]
) -> "Future[str]":
self._require_stared.require_started()
return self._impl.publish(
topic=topic, data=data, ordering_key=ordering_key, **attrs
)
@overrides
def __enter__(self):
self._require_stared.__enter__()
self._impl.__enter__()
return self
@overrides
def __exit__(self, exc_type, exc_value, traceback):
self._impl.__exit__(exc_type, exc_value, traceback)
self._require_stared.__exit__(exc_type, exc_value, traceback)
class AsyncPublisherClient(
AsyncPublisherClientInterface, ConstructableFromServiceAccount
):
"""
An AsyncPublisherClient publishes messages similar to Google Pub/Sub, but must be used in an
async context. Any publish failures are unlikely to succeed if retried.
Must be used in an `async with` block or have __aenter__() awaited before use.
"""
_impl: AsyncPublisherClientInterface
_require_stared: RequireStarted
DEFAULT_BATCHING_SETTINGS = WIRE_DEFAULT_BATCHING
"""
The default batching settings for a publisher client.
"""
def __init__(
self,
per_partition_batching_settings: Optional[BatchSettings] = None,
credentials: Optional[Credentials] = None,
transport: str = "grpc_asyncio",
client_options: Optional[ClientOptions] = None,
):
"""
Create a new AsyncPublisherClient.
Args:
per_partition_batching_settings: The settings for publish batching. Apply on a per-partition basis.
credentials: If provided, the credentials to use when connecting.
transport: The transport to use. Must correspond to an asyncio transport.
client_options: The client options to use when connecting. If used, must explicitly set `api_endpoint`.
"""
self._impl = MultiplexedAsyncPublisherClient(
lambda topic: make_async_publisher(
topic=topic,
per_partition_batching_settings=per_partition_batching_settings,
credentials=credentials,
client_options=client_options,
transport=transport,
)
)
self._require_stared = RequireStarted()
@overrides
async def publish(
self,
topic: Union[TopicPath, str],
data: bytes,
ordering_key: str = "",
**attrs: Mapping[str, str]
) -> str:
self._require_stared.require_started()
return await self._impl.publish(
topic=topic, data=data, ordering_key=ordering_key, **attrs
)
@overrides
async def __aenter__(self):
self._require_stared.__enter__()
await self._impl.__aenter__()
return self
@overrides
async def __aexit__(self, exc_type, exc_value, traceback):
await self._impl.__aexit__(exc_type, exc_value, traceback)
self._require_stared.__exit__(exc_type, exc_value, traceback)