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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2026 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
//
// https://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.
use Subscribe;
use ClientBuilder;
use Transport;
use crateClientBuilderResult as BuilderResult;
use Arc;
/// A Subscriber client for the [Cloud Pub/Sub] API.
///
/// Use this client to receive messages from a [pull subscription] on a topic.
///
/// # Example
/// ```
/// # use google_cloud_pubsub::client::Subscriber;
/// # async fn sample() -> anyhow::Result<()> {
/// let client = Subscriber::builder().build().await?;
/// let mut stream = client
/// .subscribe("projects/my-project/subscriptions/my-subscription")
/// .build();
/// while let Some((m, h)) = stream.next().await.transpose()? {
/// println!("Received message m={m:?}");
/// h.ack();
/// }
/// # Ok(()) }
/// ```
///
/// # Ordered Delivery
///
/// The subscriber returns messages in order if [ordered delivery] is enabled on
/// the subscription. The client provides the same guarantees as the service.
///
/// For more details on how the service works, see:
///
/// - [Considerations when using ordered delivery][considerations]
/// - [Google Cloud Pub/Sub Ordered Delivery][medium]
///
/// [considerations]: https://docs.cloud.google.com/pubsub/docs/ordering#considerations_when_using_ordered_messaging
/// [medium]: https://medium.com/google-cloud/google-cloud-pub-sub-ordered-delivery-1e4181f60bc8
/// [ordered delivery]: https://docs.cloud.google.com/pubsub/docs/ordering
///
/// # Exactly-once Delivery
///
/// The subscriber supports [exactly-once] delivery.
///
/// If you enable exactly-once delivery for a subscription, your application
/// can be opinionated about the delivery type, by destructuring the handler
/// into its [`Handler::ExactlyOnce`][eo-branch] branch.
///
/// ```
/// use google_cloud_pubsub::subscriber::MessageStream;
/// use google_cloud_pubsub::subscriber::handler::Handler;
/// async fn exactly_once_stream(mut stream: MessageStream) -> anyhow::Result<()> {
/// while let Some((m, Handler::ExactlyOnce(h))) = stream.next().await.transpose()? {
/// println!("Received message m={m:?}");
///
/// // Await the result of the ack. Typically you would not block the loop
/// // with an `await` point like this.
/// h.confirmed_ack().await?;
/// }
/// unreachable!("Oops, my subscription must have at-least-once semantics")
/// }
/// ```
///
/// You should not change the delivery type of a subscription midstream. If you
/// do, the subscriber will honor the delivery setting at the time each message
/// was received.
///
/// [exactly-once]: https://docs.cloud.google.com/pubsub/docs/exactly-once-delivery
///
/// # Configuration
///
/// To configure a `Subscriber` use the `with_*` methods in the type returned by
/// [builder()][Subscriber::builder]. The default configuration should work for
/// most applications. Common configuration changes include:
///
/// * [with_endpoint()]: by default this client uses the global default endpoint
/// (`https://pubsub.googleapis.com`). Applications using regional endpoints
/// or running in restricted networks (e.g. a network configured with
/// [Private Google Access with VPC Service Controls]) may want to override
/// this default.
/// * [with_credentials()]: by default this client uses
/// [Application Default Credentials]. Applications using custom
/// authentication may need to override this default.
///
/// # Pooling and Cloning
///
/// `Subscriber` holds a connection pool internally, it is advised to
/// create one and then reuse it. You do not need to wrap `Subscriber` in
/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
/// internally.
///
/// [application default credentials]: https://cloud.google.com/docs/authentication#adc
/// [cloud pub/sub]: https://docs.cloud.google.com/pubsub/docs/overview
/// [eo-branch]: crate::subscriber::handler::Handler::ExactlyOnce
/// [private google access with vpc service controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
/// [pull subscription]: https://docs.cloud.google.com/pubsub/docs/pull
/// [with_endpoint()]: ClientBuilder::with_endpoint
/// [with_credentials()]: ClientBuilder::with_credentials