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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
use std::{
convert::TryFrom,
future::Future,
mem,
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll},
time::Duration,
};
use futures::{
future::{self, Either, FutureExt, TryFutureExt},
pin_mut,
stream::{self, Stream},
StreamExt,
};
use pin_project::pin_project;
use tokio::{
sync::{mpsc, oneshot, Notify},
time::error::Elapsed as TimeoutElapsed,
};
use tonic::metadata::MetadataValue;
use tracing::{debug, trace_span, Instrument};
use crate::{
grpc::{Body, BoxBody, Bytes, GrpcService, StdError},
pubsub::{api, PubSubRetryCheck},
retry_policy::{exponential_backoff, ExponentialBackoff, RetryOperation, RetryPolicy},
};
/// The maximum deadline supported by the RPC service
const MAX_DEADLINE_SEC: u32 = 600;
/// The maximum number of acks or modacks in a single request
// 2500 as used in the go lib
// https://github.com/googleapis/google-cloud-go/blob/94d040898cc9e85fdac76560765b01cfd019d0b4/pubsub/iterator.go#L44-L52
const MAX_ACK_BATCH_SIZE: usize = 2500;
config_default! {
/// Configuration for a [streaming subscription](super::SubscriberClient::stream_subscription)
/// request
#[derive(Debug, Clone, Copy, Eq, PartialEq, serde::Deserialize)]
pub struct StreamSubscriptionConfig {
/// See [`StreamingPullRequest.stream_ack_deadline_seconds`](api::StreamingPullRequest::stream_ack_deadline_seconds)
#[serde(with = "humantime_serde")]
@default(Duration::from_secs(10), "StreamSubscriptionConfig::default_stream_ack_deadline")
pub stream_ack_deadline: Duration,
/// See [`StreamingPullRequest.max_outstanding_messages`](api::StreamingPullRequest::max_outstanding_messages)
@default(1000, "StreamSubscriptionConfig::default_max_outstanding_messages")
pub max_outstanding_messages: i64,
/// See [`StreamingPullRequest.max_outstanding_bytes`](api::StreamingPullRequest::max_outstanding_bytes)
@default(0, "StreamSubscriptionConfig::default_max_outstanding_bytes")
pub max_outstanding_bytes: i64,
/// Deprecated, subsumed by `max_outstanding_messages`
//TODO(0.12.0) remove deprecated field
#[deprecated]
@default(0, "StreamSubscriptionConfig::default_ack_channel_capacity")
pub ack_channel_capacity: usize,
}
}
/// An error encountered when issuing acks, nacks, or modifications from an
/// [`AcknowledgeToken`](AcknowledgeToken)
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
#[error("failed to ack/nack/modify")]
pub struct AcknowledgeError(#[source] AckErr);
#[derive(Debug, Clone, thiserror::Error)]
enum AckErr {
#[error("error in background task; check primary pull stream for errors")]
BackgroundTaskPanic,
#[error(transparent)]
Request(tonic::Status),
}
// TODO(0.12.0) remove eq/partialeq, use `matches!` in tests instead
impl PartialEq for AckErr {
fn eq(&self, other: &AckErr) -> bool {
use AckErr::*;
match (self, other) {
(BackgroundTaskPanic, BackgroundTaskPanic) => true,
(Request(status_a), Request(status_b)) => status_a.code() == status_b.code(),
_ => false,
}
}
}
impl Eq for AckErr {}
/// An error encountered when issuing deadline modifications with
/// [`AcknowledgeToken::modify_deadline`](AcknowledgeToken::modify_deadline)
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum ModifyAcknowledgeError {
/// An error occurred in delivering the modification request
#[error(transparent)]
Modify(#[from] AcknowledgeError),
/// The requested deadline was not within the permitted range
#[error("deadline must be between 0 and {MAX_DEADLINE_SEC} seconds, given {seconds}")]
InvalidDeadline {
/// The number of seconds requested for the deadline
seconds: u32,
},
}
#[derive(Debug)]
struct AckRouter {
// These channels are unbounded in a technical sense; however in practice there is a bound to
// the number of outstanding messages which pubsub will issue to a streaming caller. This will
// implicitly limit the ack/nack channel sizes.
//
// However modacks are not limited, as well as acks/nacks if a user sets both
// max_outstanding_bytes and max_outstanding_messages to zero (unbounded). Then it's up to the
// user to impose backpressure by awaiting the returned futures
acks: mpsc::UnboundedSender<TokenFeedback<String>>,
nacks: mpsc::UnboundedSender<TokenFeedback<String>>,
modacks: mpsc::UnboundedSender<TokenFeedback<ModAck>>,
}
struct ModAck {
id: String,
deadline: i32,
}
/// A token with an associated message produced by the [`StreamSubscription`](StreamSubscription)
/// stream, used to control that message's re-delivery within the message queue.
#[derive(Debug)]
pub struct AcknowledgeToken {
id: String,
router: Arc<AckRouter>,
delivery_attempt: i32,
}
impl AcknowledgeToken {
/// Acknowledge the corresponding message as received, so that the message service will stop
/// attempting to deliver it to subscribers.
///
/// Note that acknowledgements may not arrive to the service within the deadline (or at all),
/// so this is only a best-effort means of preventing re-delivery.
///
/// The returned future will complete once the acknowledge request has been sent. It is not
/// necessary to wait for the future's completion however; calling this function will initiate
/// an acknowledgement, which will finish even without awaiting the future. This can be useful
/// for callers that don't need explicit ack settlement and prefer to save latency.
pub fn ack(self) -> impl Future<Output = Result<(), AcknowledgeError>> + Send {
TokenFeedback::send(&self.router.acks, self.id)
}
/// Negatively acknowledge the corresponding message, requesting that the message service
/// re-deliver it to subscribers.
///
/// This may be useful if the message consumer encounters an error while processing the
/// message.
///
/// The returned future need not be awaited, see [`AcknowledgeToken::ack`]
pub fn nack(self) -> impl Future<Output = Result<(), AcknowledgeError>> + Send {
TokenFeedback::send(&self.router.nacks, self.id)
}
/// Modify the acknowledgement deadline of the corresponding message, so that re-delivery is
/// not attempted until the given time elapses (unless an ack/nack is sent).
///
/// The message's deadline will be set to the given number of seconds after the service
/// receives the modify request. Note that this could be shorter than the message's prior
/// deadline, if for example the [subscription-level
/// deadline](StreamSubscriptionConfig::stream_ack_deadline) is longer. A deadline of
/// 0 seconds is equivalent to a [`nack`](AcknowledgeToken::nack) and will make the message
/// immediately available for re-delivery. The maximum deadline accepted by the service is 600
/// seconds.
///
/// The returned future need not be awaited, see [`AcknowledgeToken::ack`]
pub fn modify_deadline(
&mut self,
seconds: u32,
) -> impl Future<Output = Result<(), ModifyAcknowledgeError>> + Send {
if seconds > MAX_DEADLINE_SEC {
return Either::Left(future::ready(Err(
ModifyAcknowledgeError::InvalidDeadline { seconds },
)));
}
Either::Right(
TokenFeedback::send(
&self.router.modacks,
ModAck {
id: self.id.clone(),
deadline: seconds as i32,
},
)
.map_err(ModifyAcknowledgeError::Modify),
)
}
/// The approximate number of times that Cloud Pub/Sub has attempted to deliver the associated
/// message to a subscriber.
///
/// See [`delivery_attempt`](api::ReceivedMessage::delivery_attempt)
pub fn delivery_attempt(&self) -> i32 {
self.delivery_attempt
}
}
// Each ack token's ack/nack/modack calls are carried by this struct to the background task that
// polls the mpsc channels. That task will then notify the token of the request's completion by sending
// a result back over the given oneshot channel
struct TokenFeedback<T> {
payload: T,
completion: oneshot::Sender<Result<(), AcknowledgeError>>,
}
impl<T: Send> TokenFeedback<T> {
fn send(
channel: &mpsc::UnboundedSender<TokenFeedback<T>>,
payload: T,
) -> impl Future<Output = Result<(), AcknowledgeError>> + Send {
let (completion, listener) = oneshot::channel();
// send the payload over the channel synchronously. After this, the caller could drop the
// returned future and the work would still happen (barring errors/panics)
let send_result = channel.send(Self {
completion,
payload,
});
// now create the future to actually wait on the outcome
async move {
match send_result {
Ok(()) => match listener.await {
// if the background task completed a request with our payload, then
// we're ready to return a normal case to the user. Note this might still be a
// failed response, but the request completed a trip to the pubsub service
Ok(server_response) => return server_response,
Err(oneshot::error::RecvError { .. }) => {}
},
Err(mpsc::error::SendError { .. }) => {}
}
// Either SendError or RecvError imply that the other end of the channel was dropped.
// This means the background handler has stopped; if there are still senders open, that
// should only happen if a panic happened in that task
Err(AcknowledgeError(AckErr::BackgroundTaskPanic))
}
}
}
/// Wait for acks to arrive over the given channel, and send them to the server via the acknowledge
/// grpc method.
async fn handle_acks<S, R>(
mut client: api::subscriber_client::SubscriberClient<S>,
subscription: String,
mut acks: mpsc::UnboundedReceiver<TokenFeedback<String>>,
mut retry_policy: R,
) where
S: GrpcService<BoxBody> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<StdError>,
S::ResponseBody: Body<Data = Bytes> + Send + 'static,
<S::ResponseBody as Body>::Error: Into<StdError> + Send,
R: RetryPolicy<(), tonic::Status> + Send + 'static,
R::RetryOp: Send + 'static,
<R::RetryOp as RetryOperation<(), tonic::Status>>::Sleep: Send + 'static,
{
let mut batch = Vec::new();
loop {
let fetch_count = acks.recv_many(&mut batch, MAX_ACK_BATCH_SIZE).await;
if fetch_count == 0 {
// all senders dropped, pull stream must have closed
break;
}
let request = api::AcknowledgeRequest {
subscription: subscription.clone(),
ack_ids: batch
.iter_mut()
.map(|TokenFeedback { payload, .. }| mem::take(payload))
.collect(),
};
let mut retry_op = retry_policy.new_operation();
let response = 'retry: loop {
match client.acknowledge(request.clone()).await {
Ok(response) => break Ok(response.into_inner()),
Err(err) => match retry_op.check_retry(&(), &err) {
None => break Err(AcknowledgeError(AckErr::Request(err))),
Some(backoff) => {
backoff.await;
continue 'retry;
}
},
}
};
let mut listeners = batch
.drain(..)
.map(|TokenFeedback { completion, .. }| completion);
// peel off the first to avoid cloning in the common single-message case
let first = listeners.next().expect("fetched > 0");
// Send failures can only happen if the receiver was dropped.
// That's benign, the user isn't listening for ack responses. Ignore such failures
for listener in listeners {
let _ = listener.send(response.clone());
}
let _ = first.send(response);
}
}
/// much like handle_acks except includes ack_deadline_seconds and calls modify_ack_deadline.
// unfortunately hard to unify the two without proper async closures (or macros i guess)
async fn handle_nacks<S, R>(
mut client: api::subscriber_client::SubscriberClient<S>,
subscription: String,
mut nacks: mpsc::UnboundedReceiver<TokenFeedback<String>>,
mut retry_policy: R,
) where
S: GrpcService<BoxBody> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<StdError>,
S::ResponseBody: Body<Data = Bytes> + Send + 'static,
<S::ResponseBody as Body>::Error: Into<StdError> + Send,
R: RetryPolicy<(), tonic::Status> + Send + 'static,
R::RetryOp: Send + 'static,
<R::RetryOp as RetryOperation<(), tonic::Status>>::Sleep: Send + 'static,
{
let mut batch = Vec::new();
loop {
let fetch_count = nacks.recv_many(&mut batch, MAX_ACK_BATCH_SIZE).await;
if fetch_count == 0 {
break;
}
let request = api::ModifyAckDeadlineRequest {
subscription: subscription.clone(),
// zero seconds implies nack
ack_deadline_seconds: 0,
ack_ids: batch
.iter_mut()
.map(|TokenFeedback { payload, .. }| mem::take(payload))
.collect(),
};
let mut retry_op = retry_policy.new_operation();
let response = 'retry: loop {
match client.modify_ack_deadline(request.clone()).await {
Ok(response) => break Ok(response.into_inner()),
Err(err) => match retry_op.check_retry(&(), &err) {
None => break Err(AcknowledgeError(AckErr::Request(err))),
Some(backoff) => {
backoff.await;
continue 'retry;
}
},
}
};
let mut listeners = batch
.drain(..)
.map(|TokenFeedback { completion, .. }| completion);
let first = listeners.next().expect("fetched > 0");
for listener in listeners {
let _ = listener.send(response.clone());
}
let _ = first.send(response);
}
}
async fn handle_modacks<S, R>(
mut client: api::subscriber_client::SubscriberClient<S>,
subscription: String,
mut modacks: mpsc::UnboundedReceiver<TokenFeedback<ModAck>>,
mut retry_policy: R,
) where
S: GrpcService<BoxBody> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<StdError>,
S::ResponseBody: Body<Data = Bytes> + Send + 'static,
<S::ResponseBody as Body>::Error: Into<StdError> + Send,
R: RetryPolicy<(), tonic::Status> + Send + 'static,
R::RetryOp: Send + 'static,
<R::RetryOp as RetryOperation<(), tonic::Status>>::Sleep: Send + 'static,
{
let mut batch = Vec::new();
loop {
let fetch_count = modacks.recv_many(&mut batch, MAX_ACK_BATCH_SIZE).await;
if fetch_count == 0 {
break;
}
// Unlike acks and nacks, each modack can have a different deadline. The request schema
// specifies a single deadline for all tokens in its batch. To resolve these two
// constraints, sort the batch into sections of identical deadlines, and send a batched
// request for each section.
//
// This is sorted in descending order because the below code pulls off the end. We want the
// shortest deadlines sent to the server first, on the theory that shorter deadlines imply
// tighter timing (though maybe it's irrelevant, deadlines are seconds while requests are
// hopefully millis)
batch.sort_by_key(|entry| std::cmp::Reverse(entry.payload.deadline));
// take sections off the end to avoid shifting values down as they're drained
while let Some(last_entry) = batch.last() {
let ack_deadline_seconds = last_entry.payload.deadline;
let section_start =
batch.partition_point(|entry| entry.payload.deadline > ack_deadline_seconds);
let request = api::ModifyAckDeadlineRequest {
subscription: subscription.clone(),
ack_deadline_seconds,
ack_ids: batch[section_start..]
.iter_mut()
.map(|TokenFeedback { payload, .. }| mem::take(&mut payload.id))
.collect(),
};
let mut retry_op = retry_policy.new_operation();
let response = 'retry: loop {
match client.modify_ack_deadline(request.clone()).await {
Ok(response) => break Ok(response.into_inner()),
Err(err) => match retry_op.check_retry(&(), &err) {
None => break Err(AcknowledgeError(AckErr::Request(err))),
Some(backoff) => {
backoff.await;
continue 'retry;
}
},
}
};
let mut listeners = batch
.drain(section_start..)
.map(|TokenFeedback { completion, .. }| completion);
let first = listeners.next().expect("fetched > 0");
for listener in listeners {
let _ = listener.send(response.clone());
}
let _ = first.send(response);
}
}
}
/// Create the initial StreamingPullRequest which must include a Subscription string and a unique
/// client_id.
fn create_initial_streaming_pull_request(
subscription: String,
client_id: String,
config: &StreamSubscriptionConfig,
) -> api::StreamingPullRequest {
api::StreamingPullRequest {
subscription,
client_id,
stream_ack_deadline_seconds: i32::try_from(config.stream_ack_deadline.as_secs())
.expect("ack deadline seconds should fit in i32"),
max_outstanding_messages: config.max_outstanding_messages,
max_outstanding_bytes: config.max_outstanding_bytes,
..Default::default()
}
}
/// Create the Nth StreamingPullRequest message (i.e. 2nd or later).
fn create_subsequent_streaming_pull_request(
config: &StreamSubscriptionConfig,
) -> api::StreamingPullRequest {
api::StreamingPullRequest {
// Even though this was set on the first request, it is reset on every subsequent request.
// Here we "reset" it to the same value.
stream_ack_deadline_seconds: i32::try_from(config.stream_ack_deadline.as_secs())
.expect("ack deadline seconds should fit in i32"),
..Default::default()
}
}
/// Create an indefinite request stream of StreamingPullRequests. The first request initializes a new
/// gRPC stream and subsequent messages will keep the connection alive.
///
/// The returned pair includes a stop indicator: after the value is dropped, the stream will stop
/// yielding elements and terminate.
fn create_streaming_pull_request_stream(
subscription: String,
client_id: String,
config: StreamSubscriptionConfig,
) -> (impl Stream<Item = api::StreamingPullRequest>, impl Drop) {
let stop_check = Arc::new(Notify::new());
struct StopOnDrop(Arc<Notify>);
impl Drop for StopOnDrop {
fn drop(&mut self) {
self.0.notify_one();
}
}
let stopper = StopOnDrop(Arc::clone(&stop_check));
let stream = async_stream::stream! {
// start by issuing the first request
yield create_initial_streaming_pull_request(subscription, client_id, &config);
let should_stop = stop_check.notified();
pin_mut!(should_stop);
// Periodically send requests to keep the grpc connection active. This can help in cases
// where messages aren't being actively read (e.g. processing messages takes a long time).
//
// This does not send acks back over the stream, instead opting for explicit ack requests
// to have better feedback over ack completion/success/failure.
loop {
match tokio::time::timeout(Duration::from_secs(30), should_stop.as_mut()).await {
Ok(()) => break,
Err(TimeoutElapsed { .. }) => {
yield create_subsequent_streaming_pull_request(&config);
}
}
}
};
(stream, stopper)
}
/// The stream returned by the
/// [`stream_subscription`](crate::pubsub::SubscriberClient::stream_subscription) function
#[pin_project]
pub struct StreamSubscription<
S = crate::grpc::DefaultGrpcImpl,
R = ExponentialBackoff<PubSubRetryCheck>,
> {
state: StreamState<S, R>,
// reserve the right to be !Unpin in the future without a major version bump
_p: std::marker::PhantomPinned,
}
/// To allow some builder-like setup after the StreamSubscription is created but before it is used,
/// this enum holds the initialized state before switching to streaming.
///
/// Builder methods (e.g. [`StreamSubscription::with_retry_policy`]) should take `self` by value,
/// so that these options can only be changed before streaming actually begins. Any calls to
/// `poll_next` require `Pin<&mut Self>` which then prohibits moving `self`, so no builder methods
/// could be called after streaming
enum StreamState<S, R> {
Initialized {
client: [api::subscriber_client::SubscriberClient<S>; 4],
subscription: String,
config: StreamSubscriptionConfig,
retry_policy: R,
},
Transition,
Streaming(
// TODO(type_alias_impl_trait) don't box
stream::BoxStream<'static, Result<(AcknowledgeToken, api::PubsubMessage), tonic::Status>>,
),
}
impl<S> StreamSubscription<S> {
pub(super) fn new(
client: [api::subscriber_client::SubscriberClient<S>; 4],
subscription: String,
config: StreamSubscriptionConfig,
) -> Self {
StreamSubscription {
state: StreamState::Initialized {
client,
subscription,
config,
retry_policy: ExponentialBackoff::new(
PubSubRetryCheck::default(),
Self::default_retry_configuration(),
),
},
_p: std::marker::PhantomPinned,
}
}
/// The default configuration values used for retrying connections to the PubSub streaming pull
/// RPC
pub fn default_retry_configuration() -> exponential_backoff::Config {
// values pulled from java lib
// https://github.com/googleapis/java-pubsub/blob/d969e8925edc3401e6eb534699ce0351a5f0b20b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/StreamingSubscriberConnection.java#L70
exponential_backoff::Config {
initial_interval: Duration::from_millis(100),
max_interval: Duration::from_secs(10),
multiplier: 2.0,
..Default::default()
}
}
}
impl<S, OldR> StreamSubscription<S, OldR> {
/// Set the [`RetryPolicy`] to use for this streaming subscription.
///
/// The stream will be reconnected if the policy indicates that an encountered
/// [`Status`](tonic::Status) error should be retried
// Because `poll_next` requires `Pin<&mut Self>`, this function cannot be called after the
// stream has started because it moves `self`. That means that the retry policy can only be
// changed before the polling starts, and is fixed from that point on
pub fn with_retry_policy<R>(self, new_retry_policy: R) -> StreamSubscription<S, R>
where
R: RetryPolicy<(), tonic::Status>,
{
use StreamState::Initialized;
StreamSubscription {
state: match self.state {
Initialized {
client,
subscription,
config,
retry_policy: _old,
} => Initialized {
client,
subscription,
config,
retry_policy: new_retry_policy,
},
_ => unreachable!(
"state only changes in `poll_next`, which can't be called while `self` is \
movable"
),
},
_p: self._p,
}
}
}
impl<S, R> Stream for StreamSubscription<S, R>
where
S: GrpcService<BoxBody> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<StdError>,
S::ResponseBody: Body<Data = Bytes> + Send + 'static,
<S::ResponseBody as Body>::Error: Into<StdError> + Send,
R: RetryPolicy<(), tonic::Status> + Send + 'static,
R::RetryOp: Send + 'static,
<R::RetryOp as RetryOperation<(), tonic::Status>>::Sleep: Send + 'static,
{
type Item = Result<(AcknowledgeToken, api::PubsubMessage), tonic::Status>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
use StreamState::{Initialized, Streaming, Transition};
let this = self.project();
// switch between states using this loop + match + return
loop {
return match this.state {
Initialized { .. } => {
// after checking the state in a borrow, get ownership by switching to the
// transition state
match std::mem::replace(this.state, Transition) {
Initialized {
client,
subscription,
config,
retry_policy,
} => {
*this.state = Streaming(Box::pin(stream_from_client(
client,
subscription,
config,
retry_policy,
)));
continue;
}
_ => unreachable!("just checked state"),
}
}
Transition => {
unreachable!("transition state should be transient and not witnessable")
}
Streaming(stream) => stream.poll_next_unpin(cx),
};
}
}
}
/// Create a stream of PubSub results
///
/// The stream will internally reconnect on error if the given retry policy indicates the
/// error is retriable
fn stream_from_client<S, R>(
clients: [api::subscriber_client::SubscriberClient<S>; 4],
subscription: String,
config: StreamSubscriptionConfig,
retry_policy: R,
) -> impl Stream<Item = Result<(AcknowledgeToken, api::PubsubMessage), tonic::Status>> + Send + 'static
where
S: GrpcService<BoxBody> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<StdError>,
S::ResponseBody: Body<Data = Bytes> + Send + 'static,
<S::ResponseBody as Body>::Error: Into<StdError> + Send,
R: RetryPolicy<(), tonic::Status> + Send + 'static,
R::RetryOp: Send + 'static,
<R::RetryOp as RetryOperation<(), tonic::Status>>::Sleep: Send + 'static,
{
let subscription_meta =
MetadataValue::try_from(&subscription).expect("valid subscription metadata");
// the client id is used for stream reconnection on error
let client_id = uuid::Uuid::new_v4().to_string();
let mut retry_policy = ArcRetry::new(retry_policy);
let [mut client, ack_client, nack_client, modack_client] = clients;
async_stream::stream! {
let mut retry_op = None;
let (acks, acks_rx) = mpsc::unbounded_channel();
let (nacks, nacks_rx) = mpsc::unbounded_channel();
let (modacks, modacks_rx) = mpsc::unbounded_channel();
let ack_router = Arc::new(AckRouter { acks, nacks, modacks });
// spawn the ack processing in the background. These should continue to process even
// when messages are not being pulled.
let ack_processor = tokio::spawn(future::join3(
handle_acks(ack_client, subscription.clone(), acks_rx, retry_policy.clone()),
handle_nacks(nack_client, subscription.clone(), nacks_rx, retry_policy.clone()),
handle_modacks(modack_client, subscription.clone(), modacks_rx, retry_policy.clone()),
))
.unwrap_or_else(|join_err| std::panic::resume_unwind(join_err.into_panic()))
.map(|((), (), ())| ());
pin_mut!(ack_processor);
'reconnect: loop {
let (request_stream, stream_drop_stopper) = create_streaming_pull_request_stream(
subscription.clone(),
client_id.clone(),
config,
);
debug!(message="connecting streaming pull stream", %subscription, %client_id);
let mut error = match client
.streaming_pull(request_stream)
.await
.map(|response| response.into_inner())
{
Err(err) => err,
Ok(mut message_stream) => 'read: loop {
// check if the background processor encountered any panics;
// if not, try to read messages from the stream.
let msg = message_stream.next().instrument(trace_span!("sub_stream_pull"));
pin_mut!(msg);
let next = future::poll_fn(|cx| match ack_processor.as_mut().poll(cx) {
Poll::Ready(()) => unreachable!("shouldn't complete while stream is active"),
Poll::Pending => msg.as_mut().poll(cx)
});
match next.await {
// If the stream is out of elements, some connection must have been closed.
// However PubSub docs say StreamingPull always terminates with an error,
// so this normal end-of-stream shouldn't happen, and instead should fall
// to the error branch.
//
// Here we assume some other part of the stack ended the connection, and
// therefore attempting to reconnect (with retry/backoff) is the right
// resolution
None => break 'read tonic::Status::aborted("unexpected end of stream"),
// if there's an error in reading, break to the error handler and check
// whether to retry
Some(Err(err)) => break 'read err,
// otherwise, we got a successful response
Some(Ok(response)) => {
// If we were in a retry loop, declare it complete
retry_op = None;
for message in response.received_messages {
let ack_token = AcknowledgeToken {
id: message.ack_id,
router: Arc::clone(&ack_router),
delivery_attempt: message.delivery_attempt,
};
let message = match message.message {
Some(msg) => msg,
None => break 'read tonic::Status::internal(
"message should be populated by RPC server"
),
};
yield Ok((ack_token, message));
}
continue 'read;
}
}
}
};
std::mem::drop(stream_drop_stopper);
debug!(%client_id, "Stream ended");
// if either the streaming connection or a stream element produces an error,
// the error will arrive here.
// check if this error can be recovered by reconnecting the stream.
let should_retry = retry_op
.get_or_insert_with(|| retry_policy.new_operation())
.check_retry(&(), &error);
match should_retry {
// if the retry policy determines a retry is possible, sleep for the
// given backoff and then try reconnecting
Some(backoff_sleep) => {
backoff_sleep.instrument(trace_span!("backoff_sleep")).await;
continue 'reconnect;
}
// if the policy does not provide a sleep, then it determined that the
// operation is terminal (or the retries have been exhausted). Yield
// the error, and then exit
None => {
error.metadata_mut().insert("subscription", subscription_meta);
yield Err(error);
break 'reconnect;
}
}
}
}
}
// Generic `R: RetryPolicy` doesn't impl Clone? fine! i'll build my own
// TODO(0.12.0) require `R: Clone` on stream instead of these shenanigans
struct ArcRetry<R> {
inner: Arc<Mutex<R>>,
}
impl<R> ArcRetry<R> {
fn new(r: R) -> Self {
Self {
inner: Arc::new(Mutex::new(r)),
}
}
}
impl<R> Clone for ArcRetry<R> {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl<R, T, E> RetryPolicy<T, E> for ArcRetry<R>
where
R: RetryPolicy<T, E>,
{
type RetryOp = R::RetryOp;
fn new_operation(&mut self) -> Self::RetryOp {
self.inner.lock().unwrap().new_operation()
}
}
#[cfg(test)]
mod test {
use super::*;
use std::sync::Mutex;
use tonic::Code;
#[test]
fn token_send() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
let (send, mut recv) = mpsc::unbounded_channel();
let mut fut = TokenFeedback::send(&send, "hello world").boxed();
// without any poll, the item should already be sent over the channel
let TokenFeedback {
payload,
completion,
} = recv.try_recv().expect("send should be synchronous");
assert_eq!(payload, "hello world");
// the future should now be waiting for a response on the completion channel
assert!(matches!(fut.as_mut().poll(&mut cx), Poll::Pending));
completion.send(Ok(())).expect("oneshot is open");
assert!(matches!(fut.as_mut().poll(&mut cx), Poll::Ready(Ok(()))));
// setup another send to witness an error response
let mut fut = TokenFeedback::send(&send, "abc123").boxed();
let TokenFeedback { completion, .. } = recv.try_recv().expect("send should be synchronous");
assert!(matches!(fut.as_mut().poll(&mut cx), Poll::Pending));
completion
.send(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
.expect("oneshot is open");
assert!(matches!(
fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
}
#[test]
fn token_wait() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
let (send, mut recv) = mpsc::unbounded_channel();
let mut fut = TokenFeedback::send(&send, "hello world").boxed();
let TokenFeedback { completion, .. } = recv.try_recv().expect("send should be synchronous");
// if the completion gets dropped, the waiting future should resolve with an error
std::mem::drop(completion);
assert!(matches!(
fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
// that also applies if the future is already polling
let mut fut = TokenFeedback::send(&send, "hello world").boxed();
let TokenFeedback { completion, .. } = recv.try_recv().expect("send should be synchronous");
assert!(matches!(fut.as_mut().poll(&mut cx), Poll::Pending));
std::mem::drop(completion);
assert!(matches!(
fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
// start a send which will encounter a receiver drop
let mut fut = TokenFeedback::send(&send, "hello world").boxed();
assert!(matches!(fut.as_mut().poll(&mut cx), Poll::Pending));
std::mem::drop(recv);
assert!(matches!(
fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
// a send started after the receiver drop should also fail
let mut fut = TokenFeedback::send(&send, "hello world").boxed();
assert!(matches!(
fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
}
#[test]
fn ack_handling() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
#[derive(Default, Clone)]
struct MockSubscriberServer {
acks: Arc<Mutex<Vec<api::AcknowledgeRequest>>>,
injected_errors: Arc<Mutex<Vec<tonic::Status>>>,
}
#[tonic::codegen::async_trait]
impl api::subscriber_server::Subscriber for MockSubscriberServer {
async fn acknowledge(
&self,
request: tonic::Request<api::AcknowledgeRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
self.acks.lock().unwrap().push(request.into_inner());
let mut errs = self.injected_errors.lock().unwrap();
if errs.is_empty() {
Ok(tonic::Response::new(()))
} else {
Err(errs.remove(0))
}
}
}
let (ack_send, recv) = mpsc::unbounded_channel();
let server = MockSubscriberServer::default();
let take_server_acks = || server.acks.lock().unwrap().drain(..).collect::<Vec<_>>();
let mut ack_handler = handle_acks(
api::subscriber_client::SubscriberClient::new(
api::subscriber_server::SubscriberServer::new(server.clone()),
),
"test-subscription".into(),
recv,
TestRetryPolicy { max_retries: 0 },
)
.boxed();
// simple single ack case
let mut ack_fut = TokenFeedback::send(&ack_send, "ack-id1".into()).boxed();
// drive ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// check that the server got the request
assert_eq!(
take_server_acks(),
vec![api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id1".into()],
}]
);
// and that the ack token got its response
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// send multiple acks before the handler polls again
let mut ack_fut = future::join3(
TokenFeedback::send(&ack_send, "ack-id2".into()),
TokenFeedback::send(&ack_send, "ack-id3".into()),
TokenFeedback::send(&ack_send, "ack-id4".into()),
)
.boxed();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// check that the server got a single buffered request
assert_eq!(
take_server_acks(),
vec![api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id2".into(), "ack-id3".into(), "ack-id4".into()],
}]
);
// and that all the futures got responses
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready((Ok(()), Ok(()), Ok(())))
));
// send multiple acks again
let mut ack_fut = future::join3(
TokenFeedback::send(&ack_send, "ack-id5".into()),
TokenFeedback::send(&ack_send, "ack-id6".into()),
TokenFeedback::send(&ack_send, "ack-id7".into()),
)
.boxed();
// however this time inject an error response from the server
server
.injected_errors
.lock()
.unwrap()
.push(tonic::Status::aborted("injected-error"));
// drive the ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(
take_server_acks(),
vec![api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id5".into(), "ack-id6".into(), "ack-id7".into()],
}]
);
// the ack tokens should each get back the error
let ack_responses = ack_fut.as_mut().poll(&mut cx);
match ack_responses {
Poll::Ready((
Err(AcknowledgeError(AckErr::Request(status1))),
Err(AcknowledgeError(AckErr::Request(status2))),
Err(AcknowledgeError(AckErr::Request(status3))),
)) if (
(status1.code(), status2.code(), status3.code()),
(status1.message(), status2.message(), status3.message()),
) == (
(Code::Aborted, Code::Aborted, Code::Aborted),
("injected-error", "injected-error", "injected-error"),
) => {}
_ => panic!("unexpected future output {ack_responses:?}"),
};
// if more than the batch limit is submitted, the handler will send multiple requests
let futs = (0..(MAX_ACK_BATCH_SIZE + 2))
.map(|i| TokenFeedback::send(&ack_send, format!("mass-ack{i}")))
.collect::<Vec<_>>();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
let server_acks = take_server_acks();
assert_eq!(server_acks.len(), 2);
assert_eq!(
server_acks[0].ack_ids,
(0..MAX_ACK_BATCH_SIZE)
.map(|i| format!("mass-ack{i}"))
.collect::<Vec<_>>()
);
const _SANITY_CHECK: [(); 2500] = [(); MAX_ACK_BATCH_SIZE];
assert_eq!(
server_acks[1].ack_ids,
vec!["mass-ack2500".to_owned(), "mass-ack2501".to_owned()]
);
// all the futures should get their success response
for fut in futs {
assert!(matches!(
fut.boxed().as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
// the handler future can complete after the sender side is dropped.
// however it must first flush any acks still in the queue.
let mut ack_fut = TokenFeedback::send(&ack_send, "ack-id99".into()).boxed();
std::mem::drop(ack_send);
assert!(take_server_acks().is_empty()); // sanity check
assert!(matches!(
ack_handler.as_mut().poll(&mut cx),
Poll::Ready(())
));
assert_eq!(
take_server_acks(),
vec![api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id99".into()]
}]
);
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
// copy-paste of ack handler. practically identical functionality
#[test]
fn nack_handling() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
#[derive(Default, Clone)]
struct MockSubscriberServer {
acks: Arc<Mutex<Vec<api::ModifyAckDeadlineRequest>>>,
injected_errors: Arc<Mutex<Vec<tonic::Status>>>,
}
#[tonic::codegen::async_trait]
impl api::subscriber_server::Subscriber for MockSubscriberServer {
async fn modify_ack_deadline(
&self,
request: tonic::Request<api::ModifyAckDeadlineRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
self.acks.lock().unwrap().push(request.into_inner());
let mut errs = self.injected_errors.lock().unwrap();
if errs.is_empty() {
Ok(tonic::Response::new(()))
} else {
Err(errs.remove(0))
}
}
}
let (ack_send, recv) = mpsc::unbounded_channel();
let server = MockSubscriberServer::default();
let take_server_acks = || server.acks.lock().unwrap().drain(..).collect::<Vec<_>>();
let mut ack_handler = handle_nacks(
api::subscriber_client::SubscriberClient::new(
api::subscriber_server::SubscriberServer::new(server.clone()),
),
"test-subscription".into(),
recv,
TestRetryPolicy { max_retries: 0 },
)
.boxed();
// simple single ack case
let mut ack_fut = TokenFeedback::send(&ack_send, "ack-id1".into()).boxed();
// drive ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// check that the server got the request
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id1".into()],
ack_deadline_seconds: 0,
}]
);
// and that the ack token got its response
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// send multiple acks before the handler polls again
let mut ack_fut = future::join3(
TokenFeedback::send(&ack_send, "ack-id2".into()),
TokenFeedback::send(&ack_send, "ack-id3".into()),
TokenFeedback::send(&ack_send, "ack-id4".into()),
)
.boxed();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// check that the server got a single buffered request
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id2".into(), "ack-id3".into(), "ack-id4".into()],
ack_deadline_seconds: 0,
}]
);
// and that all the futures got responses
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready((Ok(()), Ok(()), Ok(())))
));
// send multiple acks again
let mut ack_fut = future::join3(
TokenFeedback::send(&ack_send, "ack-id5".into()),
TokenFeedback::send(&ack_send, "ack-id6".into()),
TokenFeedback::send(&ack_send, "ack-id7".into()),
)
.boxed();
// however this time inject an error response from the server
server
.injected_errors
.lock()
.unwrap()
.push(tonic::Status::aborted("injected-error"));
// drive the ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id5".into(), "ack-id6".into(), "ack-id7".into()],
ack_deadline_seconds: 0,
}]
);
// the ack tokens should each get back the error
let ack_responses = ack_fut.as_mut().poll(&mut cx);
match ack_responses {
Poll::Ready((
Err(AcknowledgeError(AckErr::Request(status1))),
Err(AcknowledgeError(AckErr::Request(status2))),
Err(AcknowledgeError(AckErr::Request(status3))),
)) if (
(status1.code(), status2.code(), status3.code()),
(status1.message(), status2.message(), status3.message()),
) == (
(Code::Aborted, Code::Aborted, Code::Aborted),
("injected-error", "injected-error", "injected-error"),
) => {}
_ => panic!("unexpected future output {ack_responses:?}"),
};
// if more than the batch limit is submitted, the handler will send multiple requests
let futs = (0..(MAX_ACK_BATCH_SIZE + 2))
.map(|i| TokenFeedback::send(&ack_send, format!("mass-ack{i}")))
.collect::<Vec<_>>();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
let server_acks = take_server_acks();
assert_eq!(server_acks.len(), 2);
assert_eq!(
server_acks[0].ack_ids,
(0..MAX_ACK_BATCH_SIZE)
.map(|i| format!("mass-ack{i}"))
.collect::<Vec<_>>()
);
const _SANITY_CHECK: [(); 2500] = [(); MAX_ACK_BATCH_SIZE];
assert_eq!(
server_acks[1].ack_ids,
vec!["mass-ack2500".to_owned(), "mass-ack2501".to_owned()]
);
// all the futures should get their success response
for fut in futs {
assert!(matches!(
fut.boxed().as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
// the handler future can complete after the sender side is dropped.
// however it must first flush any acks still in the queue.
let mut ack_fut = TokenFeedback::send(&ack_send, "ack-id99".into()).boxed();
std::mem::drop(ack_send);
assert!(take_server_acks().is_empty()); // sanity check
assert!(matches!(
ack_handler.as_mut().poll(&mut cx),
Poll::Ready(())
));
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id99".into()],
ack_deadline_seconds: 0,
}]
);
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
// *NOT* a (direct) copy-paste of ack handler, accounting for multiple deadlines. still mostly
// a copy though...
#[test]
fn modack_handling() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
#[derive(Default, Clone)]
struct MockSubscriberServer {
acks: Arc<Mutex<Vec<api::ModifyAckDeadlineRequest>>>,
injected_errors: Arc<Mutex<Vec<tonic::Status>>>,
}
#[tonic::codegen::async_trait]
impl api::subscriber_server::Subscriber for MockSubscriberServer {
async fn modify_ack_deadline(
&self,
request: tonic::Request<api::ModifyAckDeadlineRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
self.acks.lock().unwrap().push(request.into_inner());
let mut errs = self.injected_errors.lock().unwrap();
if errs.is_empty() {
Ok(tonic::Response::new(()))
} else {
Err(errs.remove(0))
}
}
}
let (ack_send, recv) = mpsc::unbounded_channel();
let server = MockSubscriberServer::default();
let take_server_acks = || server.acks.lock().unwrap().drain(..).collect::<Vec<_>>();
let mut ack_handler = handle_modacks(
api::subscriber_client::SubscriberClient::new(
api::subscriber_server::SubscriberServer::new(server.clone()),
),
"test-subscription".into(),
recv,
TestRetryPolicy { max_retries: 0 },
)
.boxed();
// simple single ack case
let mut ack_fut = TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id1".into(),
deadline: 1,
},
)
.boxed();
// drive ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// check that the server got the request
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id1".into()],
ack_deadline_seconds: 1,
}]
);
// and that the ack token got its response
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// send multiple acks before the handler polls again.
// note these have varying deadlines. The handler may only batch together acks with the same
// deadline
let mut ack_fut = future::join5(
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id2".into(),
deadline: 2,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id3".into(),
deadline: 5,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id4".into(),
deadline: 2,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id5".into(),
deadline: 5,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id6".into(),
deadline: 1,
},
),
)
.boxed();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// the server should have gotten separate requests for each deadline, in increasing
// order by deadline.
assert_eq!(
take_server_acks(),
vec![
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id6".into()],
ack_deadline_seconds: 1,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id2".into(), "ack-id4".into()],
ack_deadline_seconds: 2,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id3".into(), "ack-id5".into()],
ack_deadline_seconds: 5,
},
]
);
// check that all the futures got responses
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready((Ok(()), Ok(()), Ok(()), Ok(()), Ok(())))
));
// send multiple acks again
let mut ack_fut = future::join3(
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id7".into(),
deadline: 1,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id8".into(),
deadline: 1,
},
),
TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id9".into(),
deadline: 1,
},
),
)
.boxed();
// however this time inject an error response from the server
server
.injected_errors
.lock()
.unwrap()
.push(tonic::Status::aborted("injected-error"));
// drive the ack handler
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id7".into(), "ack-id8".into(), "ack-id9".into()],
ack_deadline_seconds: 1,
},]
);
// the ack tokens should each get back the error
let ack_responses = ack_fut.as_mut().poll(&mut cx);
match ack_responses {
Poll::Ready((
Err(AcknowledgeError(AckErr::Request(status1))),
Err(AcknowledgeError(AckErr::Request(status2))),
Err(AcknowledgeError(AckErr::Request(status3))),
)) if (
(status1.code(), status2.code(), status3.code()),
(status1.message(), status2.message(), status3.message()),
) == (
(Code::Aborted, Code::Aborted, Code::Aborted),
("injected-error", "injected-error", "injected-error"),
) => {}
_ => panic!("unexpected future output {ack_responses:?}"),
};
// if more than the batch limit is submitted, the handler will send multiple requests.
let futs = (0..(MAX_ACK_BATCH_SIZE + 2))
.map(|i| {
TokenFeedback::send(
&ack_send,
ModAck {
id: format!("mass-ack{i}"),
deadline: 1,
},
)
})
.collect::<Vec<_>>();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
let server_acks = take_server_acks();
assert_eq!(server_acks.len(), 2);
assert_eq!(
server_acks[0].ack_ids,
(0..MAX_ACK_BATCH_SIZE)
.map(|i| format!("mass-ack{i}"))
.collect::<Vec<_>>()
);
const _SANITY_CHECK: [(); 2500] = [(); MAX_ACK_BATCH_SIZE];
assert_eq!(
server_acks[1].ack_ids,
vec!["mass-ack2500".to_owned(), "mass-ack2501".to_owned()]
);
// all the futures should get their success response
for fut in futs {
assert!(matches!(
fut.boxed().as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
// the handler future can complete after the sender side is dropped.
// however it must first flush any acks still in the queue.
let mut ack_fut = TokenFeedback::send(
&ack_send,
ModAck {
id: "ack-id99".into(),
deadline: 2,
},
)
.boxed();
std::mem::drop(ack_send);
assert!(take_server_acks().is_empty()); // sanity check
assert!(matches!(
ack_handler.as_mut().poll(&mut cx),
Poll::Ready(())
));
assert_eq!(
take_server_acks(),
vec![api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack-id99".into()],
ack_deadline_seconds: 2,
}]
);
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
}
#[tokio::test]
async fn streaming_reqs_stop_drop() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
let (stream, stop_drop) = create_streaming_pull_request_stream(
"test-subscription".into(),
"test-client".into(),
StreamSubscriptionConfig::default(),
);
let mut stream = stream.boxed();
// first call always yield the first element
assert!(matches!(
stream.as_mut().poll_next(&mut cx),
Poll::Ready(Some(api::StreamingPullRequest { .. }))
));
// then a periodic element waits for time
tokio::time::pause();
assert!(matches!(stream.as_mut().poll_next(&mut cx), Poll::Pending));
tokio::time::advance(Duration::from_secs(31)).await;
assert!(matches!(
stream.as_mut().poll_next(&mut cx),
Poll::Ready(Some(api::StreamingPullRequest { .. }))
));
// pending on the next one
assert!(matches!(stream.as_mut().poll_next(&mut cx), Poll::Pending));
// however dropping the notifier should wake the stream and end it
std::mem::drop(stop_drop);
assert!(matches!(
stream.as_mut().poll_next(&mut cx),
Poll::Ready(None)
));
}
// panics in the background ack-handler task should be forwarded _somewhere_
// ideally it would be to the issuing ack tokens, but it's much easier to pass to the message
// streamer
#[tokio::test]
async fn background_panic_forwarded() {
use std::panic;
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
#[derive(Default, Clone)]
struct MockSubscriberServer {}
#[tonic::codegen::async_trait]
impl api::subscriber_server::Subscriber for MockSubscriberServer {
async fn acknowledge(
&self,
_request: tonic::Request<api::AcknowledgeRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
panic!("injected test panic");
}
async fn streaming_pull(
&self,
_request: tonic::Request<tonic::Streaming<api::StreamingPullRequest>>,
) -> std::result::Result<
tonic::Response<tonic::codegen::BoxStream<api::StreamingPullResponse>>,
tonic::Status,
> {
// send one message in order to provide an ack token. after that, produce no
// additional messages but don't end the stream, just hang basically
Ok(tonic::Response::new(
async_stream::stream! {
yield Ok(api::StreamingPullResponse {
received_messages: vec![api::ReceivedMessage {
ack_id: "ack1".into(),
delivery_attempt: 1,
message: Some(api::PubsubMessage {
data: vec![0u8; 16].into(),
..Default::default()
})
}],
..Default::default()
});
future::pending::<()>().await;
}
.boxed(),
))
}
}
let server = MockSubscriberServer::default();
let mut stream = stream_from_client(
std::array::from_fn(|_| {
api::subscriber_client::SubscriberClient::new(
api::subscriber_server::SubscriberServer::new(server.clone()),
)
}),
"test-subscription".into(),
StreamSubscriptionConfig::default(),
TestRetryPolicy { max_retries: 0 },
)
.boxed();
// pull the first message out to get an ack token
let ack_token = match stream.as_mut().poll_next(&mut cx) {
Poll::Ready(Some(Ok((ack_token, _message)))) => ack_token,
other => panic!("unexpected stream value {other:?}"),
};
// the stream is otherwise empty for now
assert!(matches!(stream.as_mut().poll_next(&mut cx), Poll::Pending));
// start an ack from the first message's token. this should trigger a panic in the
// background ack-handler task once it calls the mocked `acknowledge` function
let mut ack_fut = ack_token.ack().boxed();
// give the tokio test runtime an opportunity to run background tasks
tokio::task::yield_now().await;
// the stream's poll should now forward that panic to the caller
match panic::catch_unwind(panic::AssertUnwindSafe(|| {
stream.as_mut().poll_next(&mut cx)
})) {
Ok(poll) => panic!("stream did not panic when expected, instead produced {poll:?}"),
Err(panic_cause) => match panic_cause.downcast::<&'static str>() {
Ok(text) => assert_eq!(*text, "injected test panic"),
Err(_) => panic!("unexpected panic contents"),
},
}
// and the ack future is informed that an error occurred
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Err(AcknowledgeError(AckErr::BackgroundTaskPanic)))
));
}
// check that acks/nacks/modacks are retried on non-terminal errors
#[test]
fn ack_retry() {
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
#[derive(Default, Clone)]
struct MockSubscriberServer {
acks: Arc<Mutex<Vec<api::AcknowledgeRequest>>>,
modacks: Arc<Mutex<Vec<api::ModifyAckDeadlineRequest>>>,
injected_errors: Arc<Mutex<Vec<tonic::Status>>>,
}
#[tonic::codegen::async_trait]
impl api::subscriber_server::Subscriber for MockSubscriberServer {
async fn acknowledge(
&self,
request: tonic::Request<api::AcknowledgeRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
self.acks.lock().unwrap().push(request.into_inner());
let mut errs = self.injected_errors.lock().unwrap();
if errs.is_empty() {
Ok(tonic::Response::new(()))
} else {
Err(errs.remove(0))
}
}
async fn modify_ack_deadline(
&self,
request: tonic::Request<api::ModifyAckDeadlineRequest>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
self.modacks.lock().unwrap().push(request.into_inner());
let mut errs = self.injected_errors.lock().unwrap();
if errs.is_empty() {
Ok(tonic::Response::new(()))
} else {
Err(errs.remove(0))
}
}
}
let retry_policy = TestRetryPolicy { max_retries: 2 };
let server = MockSubscriberServer::default();
let take_server_acks = || server.acks.lock().unwrap().drain(..).collect::<Vec<_>>();
let take_server_modacks = || server.modacks.lock().unwrap().drain(..).collect::<Vec<_>>();
let client = api::subscriber_client::SubscriberClient::new(
api::subscriber_server::SubscriberServer::new(server.clone()),
);
{
let (ack_send, recv) = mpsc::unbounded_channel();
let mut ack_handler = handle_acks(
client.clone(),
"test-subscription".into(),
recv,
retry_policy.clone(),
)
.boxed();
let mut ack_fut = TokenFeedback::send(&ack_send, "ack1".into()).boxed();
// inject 2 errors which the ack handler should retry
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
]);
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
// two errors should show 3 total requests
assert_eq!(
take_server_acks(),
vec![
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack1".into()]
},
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack1".into()]
},
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack1".into()]
}
]
);
// and the ack should be successful in the end
assert!(matches!(
ack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// exceeding the error count should still yield the error in the end
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("3 is too many, give up"),
]);
let mut ack_fut = TokenFeedback::send(&ack_send, "ack2".into()).boxed();
assert!(matches!(ack_handler.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(
take_server_acks(),
vec![
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack2".into()]
},
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack2".into()]
},
api::AcknowledgeRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["ack2".into()]
}
]
);
// the ack should be informed that the operation failed
let ack_response = ack_fut.as_mut().poll(&mut cx);
match ack_response {
Poll::Ready(Err(AcknowledgeError(AckErr::Request(status))))
if (status.code(), status.message())
== (Code::Unavailable, "3 is too many, give up") => {}
_ => panic!("unexpected future output {ack_response:?}"),
};
}
// do all the same again for nacks
{
let (nack_send, recv) = mpsc::unbounded_channel();
let mut nack_handler = handle_nacks(
client.clone(),
"test-subscription".into(),
recv,
retry_policy.clone(),
)
.boxed();
let mut nack_fut = TokenFeedback::send(&nack_send, "nack1".into()).boxed();
// inject 2 errors which the nack handler should retry
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
]);
assert!(matches!(nack_handler.as_mut().poll(&mut cx), Poll::Pending));
// two errors should show 3 total requests
assert_eq!(
take_server_modacks(),
vec![
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack1".into()],
ack_deadline_seconds: 0,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack1".into()],
ack_deadline_seconds: 0,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack1".into()],
ack_deadline_seconds: 0,
},
]
);
// and the nack should be successful in the end
assert!(matches!(
nack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// exceeding the error count should still yield the error in the end
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("3 is too many, give up"),
]);
let mut nack_fut = TokenFeedback::send(&nack_send, "nack2".into()).boxed();
assert!(matches!(nack_handler.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(
take_server_modacks(),
vec![
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack2".into()],
ack_deadline_seconds: 0,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack2".into()],
ack_deadline_seconds: 0,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["nack2".into()],
ack_deadline_seconds: 0,
},
]
);
// the nack should be informed that the operation failed
let nack_response = nack_fut.as_mut().poll(&mut cx);
match nack_response {
Poll::Ready(Err(AcknowledgeError(AckErr::Request(status))))
if (status.code(), status.message())
== (Code::Unavailable, "3 is too many, give up") => {}
_ => panic!("unexpected future output {nack_response:?}"),
};
}
// do all the same again for modacks
{
let (modack_send, recv) = mpsc::unbounded_channel();
let mut modack_handler = handle_modacks(
client.clone(),
"test-subscription".into(),
recv,
retry_policy.clone(),
)
.boxed();
let mut modack_fut = TokenFeedback::send(
&modack_send,
ModAck {
id: "modack1".into(),
deadline: 2,
},
)
.boxed();
// inject 2 errors which the modack handler should retry
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
]);
assert!(matches!(
modack_handler.as_mut().poll(&mut cx),
Poll::Pending
));
// two errors should show 3 total requests
assert_eq!(
take_server_modacks(),
vec![
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack1".into()],
ack_deadline_seconds: 2,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack1".into()],
ack_deadline_seconds: 2,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack1".into()],
ack_deadline_seconds: 2,
},
]
);
// and the modack should be successful in the end
assert!(matches!(
modack_fut.as_mut().poll(&mut cx),
Poll::Ready(Ok(()))
));
// exceeding the error count should still yield the error in the end
server.injected_errors.lock().unwrap().extend([
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("please try again"),
tonic::Status::unavailable("3 is too many, give up"),
]);
let mut modack_fut = TokenFeedback::send(
&modack_send,
ModAck {
id: "modack2".into(),
deadline: 2,
},
)
.boxed();
assert!(matches!(
modack_handler.as_mut().poll(&mut cx),
Poll::Pending
));
assert_eq!(
take_server_modacks(),
vec![
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack2".into()],
ack_deadline_seconds: 2,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack2".into()],
ack_deadline_seconds: 2,
},
api::ModifyAckDeadlineRequest {
subscription: "test-subscription".into(),
ack_ids: vec!["modack2".into()],
ack_deadline_seconds: 2,
},
]
);
// the modack should be informed that the operation failed
let modack_response = modack_fut.as_mut().poll(&mut cx);
match modack_response {
Poll::Ready(Err(AcknowledgeError(AckErr::Request(status))))
if (status.code(), status.message())
== (Code::Unavailable, "3 is too many, give up") => {}
_ => panic!("unexpected future output {modack_response:?}"),
};
}
}
#[derive(Clone)]
struct TestRetryPolicy {
max_retries: usize,
}
struct TestRetryOp {
attempt: usize,
limit: usize,
}
impl RetryPolicy<(), tonic::Status> for TestRetryPolicy {
type RetryOp = TestRetryOp;
fn new_operation(&mut self) -> Self::RetryOp {
TestRetryOp {
attempt: 0,
limit: self.max_retries,
}
}
}
impl RetryOperation<(), tonic::Status> for TestRetryOp {
type Sleep = future::Ready<()>;
fn check_retry(&mut self, _: &(), _: &tonic::Status) -> Option<Self::Sleep> {
if self.attempt >= self.limit {
return None;
}
self.attempt += 1;
Some(future::ready(()))
}
}
}