ruststream-fred 0.7.1

Redis / Valkey broker implementation (Streams) for the RustStream messaging framework, backed by fred.
Documentation
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
//! Redis Pub/Sub: delivery to the subscribers connected when a message is published.
//!
//! - [PubSub][pubsub]: [`RedisPubSub`], one channel.
//! - [RedisPubSubPattern][pattern]: [`RedisPubSubPattern`], every channel a glob matches.
//! - [PubSubMode][mode]: [`PubSubMode`], classic or sharded delivery.
//! - [buffer][buffer]: [`RedisPubSub::buffer`], what a busy subscription holds.
//! - [Framing][framing]: [`RedisPubSub::codec`], the frame the headers travel in.
//! - [RedisPubSubPublish][publish]: [`Publish`], `PUBLISH` or `SPUBLISH`.
//! - [Prelude][prelude]: [`prelude`], everything a Pub/Sub service imports.
//!
//! [pubsub]: https://powersemmi.github.io/ruststream-fred/latest/components/pubsub/
//! [pattern]: https://powersemmi.github.io/ruststream-fred/latest/components/pubsub/pattern/
//! [mode]: https://powersemmi.github.io/ruststream-fred/latest/components/pubsub/pubsub-mode/
//! [buffer]: https://powersemmi.github.io/ruststream-fred/latest/components/pubsub/buffer/
//! [framing]: https://powersemmi.github.io/ruststream-fred/latest/components/subscriptions/framing/
//! [publish]: https://powersemmi.github.io/ruststream-fred/latest/components/publishing/pubsub-publish/
//! [prelude]: https://powersemmi.github.io/ruststream-fred/latest/components/broker/prelude/

use std::fmt::{Debug, Formatter};
use std::future::{Future, ready};
use std::num::NonZeroUsize;
use std::sync::Arc;

use bytes::Bytes;
use fred::clients::{Client, Pool};
use fred::interfaces::{ClientLike, PubsubInterface};
use fred::types::config::Server;
use fred::types::{Message, MessageKind};
use futures::Stream;
use futures::stream::unfold;
#[cfg(feature = "asyncapi")]
use ruststream::asyncapi::Bindings;
use ruststream::codec::Codec;
use ruststream::runtime::IntoSource;
use ruststream::{
    AckError, AddressedCopies, BatchSubscriber, BufferedSubscriber, HeaderMap, IncomingMessage,
    Lend, NamedCopies, OutgoingMessage, PairError, Partitioned, PublishPolicy, Publisher,
    RedeliveryAddress, RedeliveryAddressed, RetryDeclaration, SubscriptionSource,
};
use tokio::runtime::Handle;
use tokio::sync::broadcast::Receiver;
use tokio::sync::broadcast::error::{RecvError, TryRecvError};
use tokio::task::JoinHandle;
use tracing::warn;

use crate::broker::{ConnectedRedisBroker, RedisCore};
use crate::envelope::{SharedEnvelope, frame, unframe};
use crate::loopback::{InFlight, Tap};
use crate::partition::{RedisPublishOptions, resolved_headers};
use crate::pipeline::{PubSubForm, RoundMessage, Window};
use crate::publisher::joins_round;
use crate::route::Route;
use crate::{error::RedisError, message::PARTITION_KEY_HEADER};

/// This form's publish policy, [`RedisPubSubPublish`], under the mount-site name every form gives
/// its own. Its options, the delivery mode and the framing codec, still chain off it.
pub use crate::pubsub::RedisPubSubPublish as Publish;

/// The core prelude plus everything a Redis Pub/Sub service writes.
///
/// The broker, the [`RedisPubSub`] descriptor with its [`PubSubMode`], this form's [`Publish`]
/// policy, and its per-delivery context. Pub/Sub carries no core capability traits.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::pubsub::prelude::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize)]
/// struct Event {
///     kind: String,
/// }
///
/// #[derive(Serialize, Outgoing)]
/// #[outgoing(name = "events.seen")]
/// struct Seen {
///     kind: String,
/// }
///
/// #[subscriber(RedisPubSub::new("events").mode(PubSubMode::Sharded), reply)]
/// async fn on_event(event: &Event) -> Seen {
///     Seen {
///         kind: event.kind.clone(),
///     }
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
///     RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(
///         RedisBroker::cluster(["10.0.0.1:6379"]),
///         |b| {
///             // Sharded in, sharded out: an `SPUBLISH` reaches only sharded subscribers.
///             b.include(on_event)
///                 .out_reply(Publish::new().mode(PubSubMode::Sharded));
///         },
///     )
/// }
/// # }
/// # fn main() {}
/// ```
///
/// Two vocabularies that do not mix. A handler body imports `ruststream::prelude::*` and bounds an
/// injected slot with the broker capability trait it needs (`Out<impl Publisher>`); a routes file
/// globs this prelude and names the policy by its mount-site word, the same word on every form.
///
/// The
/// [Prelude](https://powersemmi.github.io/ruststream-fred/latest/components/broker/prelude/)
/// page shows each form's prelude in a service.
///
/// A file that also globs another form's prelude sees an ambiguous `Publish`; use
/// [`crate::prelude`] and write `pubsub::Publish` there.
pub mod prelude {
    pub use ruststream::prelude::*;

    pub use super::{PubSubMode, Publish, RedisPubSub, RedisPubSubPattern};
    // `keys` arrives as the module, not as a glob: its members are short words a service also uses
    // for its own types, and `Ctx<keys::Channel>` reads as what it is at the use site.
    pub use crate::context::{PipelineContext, PoolContext, PubSubContext, keys};
    pub use crate::pipeline::{Bindable, InRound, RedisPipelineSteps};
    pub use crate::{PARTITION_KEY_HEADER, RedisBroker, RedisPublishOptions, RedisPublishSteps};

    #[cfg(any(
        feature = "tls-rustls",
        feature = "tls-rustls-ring",
        feature = "tls-native-tls"
    ))]
    pub use crate::{TlsConfig, TlsConnector};
}

/// Pub/Sub delivery mode. Defaults to [`Classic`](Self::Classic).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum PubSubMode {
    /// `SUBSCRIBE` / `PUBLISH`: cluster-wide broadcast, pattern-capable, does not scale by slot.
    #[default]
    Classic,
    /// `SSUBSCRIBE` / `SPUBLISH` (Redis 7+): slot-local sharded delivery, no patterns.
    Sharded,
}

impl PubSubMode {
    /// The word the generated `AsyncAPI` document names this mode by.
    #[cfg(feature = "asyncapi")]
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Classic => "classic",
            Self::Sharded => "sharded",
        }
    }
}

/// Describes one Pub/Sub subscription on a single channel, against a [`ConnectedRedisBroker`].
///
/// A glob is [`RedisPubSubPattern`] instead.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::pubsub::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Event {
///     kind: String,
/// }
///
/// // Classic: `SUBSCRIBE`, broadcast to every node of a cluster.
/// #[subscriber(RedisPubSub::new("events"))]
/// async fn on_event(event: &Event) -> HandlerOutcome {
///     tracing::info!(event.kind, "event");
///     HandlerOutcome::ack()
/// }
///
/// // Sharded: `SSUBSCRIBE`, served by the node that owns the channel's slot.
/// #[subscriber(RedisPubSub::new("metrics").mode(PubSubMode::Sharded))]
/// async fn on_metric(event: &Event) -> HandlerOutcome {
///     tracing::info!(event.kind, "metric");
///     HandlerOutcome::ack()
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
///     RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(
///         RedisBroker::cluster(["10.0.0.1:6379"]),
///         |b| {
///             b.include(on_event);
///             b.include(on_metric);
///         },
///     )
/// }
/// # }
/// # fn main() {}
/// ```
#[derive(Clone)]
#[must_use]
pub struct RedisPubSub {
    channel: String,
    mode: PubSubMode,
    codec: Option<SharedEnvelope>,
    /// Where the mount site sends a spent delivery, taken from its retry declaration when the
    /// subscription opens, so the broker publishes to that name as the channel it is.
    dead_letter: Option<String>,
    /// `None` keeps the capacity `fred` gives a client's broadcast channels by default.
    buffer: Option<NonZeroUsize>,
}

impl Debug for RedisPubSub {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSub")
            .field("channel", &self.channel)
            .field("mode", &self.mode)
            .field("codec", &self.codec.is_some())
            .field("dead_letter", &self.dead_letter)
            .field("buffer", &self.buffer)
            .finish()
    }
}

impl RedisPubSub {
    /// A subscription on the exact `channel`.
    pub fn new(channel: impl Into<String>) -> Self {
        Self {
            channel: channel.into(),
            mode: PubSubMode::default(),
            codec: None,
            dead_letter: None,
            buffer: None,
        }
    }

    /// Sets the delivery mode. Defaults to [`PubSubMode::Classic`].
    pub const fn mode(mut self, mode: PubSubMode) -> Self {
        self.mode = mode;
        self
    }

    /// Decodes the header/payload envelope with `codec` (must match the publisher). Without it the
    /// default binary framing is used. Either way the payload arrives as it was published.
    pub fn codec(mut self, codec: impl Codec + 'static) -> Self {
        self.codec = Some(Arc::new(codec));
        self
    }

    /// Sets how many messages the subscription holds that its handler has not taken yet.
    ///
    /// Redis pushes a Pub/Sub message the moment it is published, and the subscription keeps it
    /// until the handler asks for the next one. The buffer bounds how many may wait. Once it is
    /// full, each new message drops the oldest, and the subscription logs a warning with the
    /// channel and the count it lost. The default is `fred`'s own
    /// (`PerformanceConfig::broadcast_channel_capacity`, 32 messages in `fred` 10.1).
    ///
    /// Raise it for a handler that falls behind bursts: set it to the largest burst the handler
    /// must absorb. The room is reserved when the subscription opens, about 600 bytes per message
    /// of buffer whatever the payloads weigh, because `fred` sizes all eight of a client's
    /// notification channels by it.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream::nonzero;
    /// use ruststream_fred::pubsub::prelude::*;
    /// # #[derive(serde::Deserialize)]
    /// # struct Event { kind: String }
    ///
    /// // A handler that falls behind a burst of up to ten thousand events loses none of them.
    /// #[subscriber(RedisPubSub::new("events").buffer(nonzero!(10_000)))]
    /// async fn on_event(event: &Event) -> HandlerOutcome {
    ///     tracing::info!(event.kind, "event");
    ///     HandlerOutcome::ack()
    /// }
    /// #
    /// # fn app() -> RustStream {
    /// #     RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(
    /// #         RedisBroker::standalone("redis://localhost:6379"),
    /// #         |b| {
    /// #             b.include(on_event);
    /// #         },
    /// #     )
    /// # }
    /// # }
    /// ```
    pub const fn buffer(mut self, messages: NonZeroUsize) -> Self {
        self.buffer = Some(messages);
        self
    }

    /// The channel this subscription listens on.
    #[must_use]
    pub fn channel(&self) -> &str {
        &self.channel
    }

    pub(crate) const fn buffer_size(&self) -> Option<NonZeroUsize> {
        self.buffer
    }

    pub(crate) const fn delivery_mode(&self) -> PubSubMode {
        self.mode
    }

    pub(crate) fn codec_handle(&self) -> Option<SharedEnvelope> {
        self.codec.clone()
    }

    /// How a name this subscription reads or dead-letters to is written: a publish in its mode
    /// and its framing.
    pub(crate) fn route(&self) -> Route {
        Route::Channel {
            mode: self.mode,
            envelope: self.codec.clone(),
        }
    }

    /// The dead-letter destination the mount site declared, once the registration has handed its
    /// declaration over.
    pub(crate) fn dead_letter(&self) -> Option<&str> {
        self.dead_letter.as_deref()
    }

    /// Takes the dead-letter destination out of the mount site's declaration.
    fn with_declaration(mut self, declaration: &RetryDeclaration) -> Self {
        self.dead_letter = declaration.dead_letter().map(str::to_owned);
        self
    }

    /// Where a publisher reaches this subscription again, shared by both broker forms.
    fn redelivery_channel(&self) -> RedeliveryAddress {
        RedeliveryAddress::new(self.channel.clone())
    }

    /// What this subscription adds to its channel in the generated `AsyncAPI` document, shared by
    /// both broker forms: the delivery mode it subscribes in and how headers are framed.
    #[cfg(feature = "asyncapi")]
    fn describe(&self) -> Bindings {
        crate::asyncapi::channel(&crate::asyncapi::PubSubSubscription::new(
            self.mode.as_str(),
            false,
            crate::asyncapi::Envelope::of(self.codec.as_ref()),
        ))
    }
}

/// Describes one Pub/Sub subscription on a glob pattern (`PSUBSCRIBE`), against a
/// [`ConnectedRedisBroker`].
///
/// Patterns are classic-only: sharded Pub/Sub routes by slot and has no `PSUBSCRIBE`, so this
/// descriptor carries no mode to get wrong.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::pubsub::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Event {
///     kind: String,
/// }
///
/// #[subscriber(RedisPubSubPattern::new("events.*"))]
/// async fn on_any_event(event: &Event, Ctx(channel): Ctx<keys::Channel>) -> HandlerOutcome {
///     tracing::info!(event.kind, channel, "event");
///     HandlerOutcome::ack()
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
///     RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(
///         RedisBroker::standalone("redis://localhost:6379"),
///         |b| {
///             // A glob is no channel a `PUBLISH` can name, so the mount site says where a
///             // retry copy goes.
///             b.include(on_any_event)
///                 .out_retry(Publish::default())
///                 .to("events.retry");
///         },
///     )
/// }
/// # }
/// # fn main() {}
/// ```
///
/// Sharded Pub/Sub has no pattern subscription, so a pattern takes no mode:
///
/// ```compile_fail
/// use ruststream_fred::{PubSubMode, RedisPubSubPattern};
///
/// let events = RedisPubSubPattern::new("events.*").mode(PubSubMode::Sharded);
/// ```
#[derive(Clone)]
#[must_use]
pub struct RedisPubSubPattern {
    pattern: String,
    codec: Option<SharedEnvelope>,
    /// `None` keeps the capacity `fred` gives a client's broadcast channels by default.
    buffer: Option<NonZeroUsize>,
}

impl Debug for RedisPubSubPattern {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSubPattern")
            .field("pattern", &self.pattern)
            .field("codec", &self.codec.is_some())
            .field("buffer", &self.buffer)
            .finish()
    }
}

impl RedisPubSubPattern {
    /// A subscription on every channel matching the glob `pattern`.
    pub fn new(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            codec: None,
            buffer: None,
        }
    }

    /// Decodes the header/payload envelope with `codec` (must match the publisher). Without it the
    /// default binary framing is used. Either way the payload arrives as it was published.
    pub fn codec(mut self, codec: impl Codec + 'static) -> Self {
        self.codec = Some(Arc::new(codec));
        self
    }

    /// Sets how many messages the subscription holds that its handler has not taken yet.
    ///
    /// Redis pushes a Pub/Sub message the moment it is published, and the subscription keeps it
    /// until the handler asks for the next one. The buffer bounds how many may wait. Once it is
    /// full, each new message drops the oldest, and the subscription logs a warning with the
    /// channel and the count it lost. The default is `fred`'s own
    /// (`PerformanceConfig::broadcast_channel_capacity`, 32 messages in `fred` 10.1).
    ///
    /// Raise it for a handler that falls behind bursts: set it to the largest burst the handler
    /// must absorb. The room is reserved when the subscription opens, about 600 bytes per message
    /// of buffer whatever the payloads weigh, because `fred` sizes all eight of a client's
    /// notification channels by it.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream::nonzero;
    /// use ruststream_fred::pubsub::prelude::*;
    /// # #[derive(serde::Deserialize)]
    /// # struct Event { kind: String }
    ///
    /// // A handler that falls behind a burst of up to ten thousand events loses none of them.
    /// #[subscriber(RedisPubSubPattern::new("events.*").buffer(nonzero!(10_000)))]
    /// async fn on_event(event: &Event) -> HandlerOutcome {
    ///     tracing::info!(event.kind, "event");
    ///     HandlerOutcome::ack()
    /// }
    /// #
    /// # fn app() -> RustStream {
    /// #     RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(
    /// #         RedisBroker::standalone("redis://localhost:6379"),
    /// #         |b| {
    /// #             b.include(on_event);
    /// #         },
    /// #     )
    /// # }
    /// # }
    /// ```
    pub const fn buffer(mut self, messages: NonZeroUsize) -> Self {
        self.buffer = Some(messages);
        self
    }

    /// The glob this subscription matches channels against.
    #[must_use]
    pub fn pattern(&self) -> &str {
        &self.pattern
    }

    pub(crate) const fn buffer_size(&self) -> Option<NonZeroUsize> {
        self.buffer
    }

    pub(crate) fn codec_handle(&self) -> Option<SharedEnvelope> {
        self.codec.clone()
    }

    /// Patterns are classic-only, so the mode in the document is the only one they have.
    #[cfg(feature = "asyncapi")]
    fn describe(&self) -> Bindings {
        crate::asyncapi::channel(&crate::asyncapi::PubSubSubscription::new(
            PubSubMode::Classic.as_str(),
            true,
            crate::asyncapi::Envelope::of(self.codec.as_ref()),
        ))
    }
}

// The manual path binds a `Handle` body to its source with `subscriber(source, body)`, which
// takes the descriptor as it is, the way the `#[subscriber(..)]` attribute does.
impl IntoSource for RedisPubSub {
    type Source = Self;

    fn into_source(self) -> Self {
        self
    }
}

impl SubscriptionSource<ConnectedRedisBroker> for RedisPubSub {
    type Subscriber = RedisPubSubSubscriber;
    /// Pub/Sub cannot acknowledge, so every retry is a copy this process publishes, and the
    /// channel is where it goes.
    type Copies = AddressedCopies;

    fn name(&self) -> &str {
        self.channel()
    }

    fn declare_retry(self, declaration: &RetryDeclaration) -> Self {
        self.with_declaration(declaration)
    }

    async fn subscribe(
        self,
        connected: &ConnectedRedisBroker,
    ) -> Result<Self::Subscriber, RedisError> {
        connected.subscribe_pubsub(self).await
    }

    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self) -> Bindings {
        self.describe()
    }
}

/// The channel itself.
///
/// The answer is about the address, not about the command: reaching a
/// [`PubSubMode::Sharded`] subscription still takes a publisher in the same mode, which is the
/// policy the mount site names with `out_retry`.
impl RedeliveryAddressed<ConnectedRedisBroker> for RedisPubSub {
    fn redelivery_address(
        &self,
        _connected: &ConnectedRedisBroker,
    ) -> impl Future<Output = Result<RedeliveryAddress, RedisError>> {
        ready(Ok(self.redelivery_channel()))
    }
}

// The manual path binds a `Handle` body to its source with `subscriber(source, body)`, which
// takes the descriptor as it is, the way the `#[subscriber(..)]` attribute does.
impl IntoSource for RedisPubSubPattern {
    type Source = Self;

    fn into_source(self) -> Self {
        self
    }
}

/// A pattern subscription reads every channel the glob matches, and a glob is not a channel a
/// `PUBLISH` can name, so the mount site names where a retry copy goes: `.out_retry(policy).to(..)`
/// for a fixed channel, or a transform that reads the delivery's own channel.
impl SubscriptionSource<ConnectedRedisBroker> for RedisPubSubPattern {
    type Subscriber = RedisPubSubSubscriber;
    type Copies = NamedCopies;

    fn name(&self) -> &str {
        self.pattern()
    }

    async fn subscribe(
        self,
        connected: &ConnectedRedisBroker,
    ) -> Result<Self::Subscriber, RedisError> {
        connected.subscribe_pubsub_pattern(self).await
    }

    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self) -> Bindings {
        self.describe()
    }
}

/// A Pub/Sub subscription backed by a dedicated `fred` client, so its message stream and channel
/// state are isolated from other subscribers and from the publishing pool.
///
/// Pub/Sub delivers one message at a time, so the batches a [`BatchSubscriber`] hands out are
/// assembled on the client by the core's [`BufferedSubscriber`]: it fills a batch up to the size
/// the mount site asked for and closes a partial one on its own deadline.
pub struct RedisPubSubSubscriber(BufferedSubscriber<PubSubWire>);

impl Debug for RedisPubSubSubscriber {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSubSubscriber")
            .finish_non_exhaustive()
    }
}

impl RedisPubSubSubscriber {
    pub(crate) fn new(wire: PubSubWire) -> Self {
        Self(BufferedSubscriber::new(wire))
    }
}

impl ruststream::Subscriber for RedisPubSubSubscriber {
    type Message = RedisPubSubMessage;
    type Error = RedisError;

    /// Yields one message per Pub/Sub delivery.
    ///
    /// # Cancel safety
    ///
    /// Dropping the returned stream between items is safe. What arrives while no stream is polling
    /// waits in the subscription's [buffer](crate::RedisPubSub::buffer); past it the oldest is
    /// lost, and while the subscription is disconnected everything is, as Redis Pub/Sub keeps
    /// nothing for a subscriber that is not there.
    fn stream(&mut self) -> impl Stream<Item = Result<Self::Message, Self::Error>> + Send + '_ {
        self.0.stream()
    }
}

impl BatchSubscriber for RedisPubSubSubscriber {
    type Batch = Vec<RedisPubSubMessage>;

    /// Yields batches of at most `size` deliveries, assembled as they arrive.
    ///
    /// # Cancel safety
    ///
    /// Same as [`Subscriber::stream`](ruststream::Subscriber::stream). A batch abandoned mid-fill
    /// loses the deliveries it holds, as any Pub/Sub delivery nobody is polling for is lost.
    fn batches(
        &mut self,
        size: NonZeroUsize,
    ) -> impl Stream<Item = Result<Self::Batch, Self::Error>> + Send + '_ {
        self.0.batches(size)
    }
}

/// What a Pub/Sub subscription's client subscribes to, kept so it can subscribe again.
pub(crate) enum Resubscribe {
    Channel(String),
    ShardedChannel(String),
    Pattern(String),
}

/// The dedicated client of a Pub/Sub subscription, and the task that subscribes it again each
/// time it reconnects.
///
/// The server forgets a connection's subscriptions when the connection drops (a restart, a
/// failover, a killed client), and `fred` reconnects the socket but does not repeat the
/// `SUBSCRIBE`. Messages published between the drop and the new subscribe reach nobody, which is
/// what Pub/Sub promises; every later one arrives. The task costs one spawn per subscription and
/// nothing per message.
pub(crate) struct Resubscribing {
    client: Client,
    resubscribing: JoinHandle<()>,
}

impl Resubscribing {
    /// Starts the task on `runtime`. `reconnects` is taken from `client` before its first
    /// subscribe, so a reconnect that lands in between is not missed.
    pub(crate) fn new(
        client: Client,
        mut reconnects: Receiver<Server>,
        target: Resubscribe,
        runtime: &Handle,
    ) -> Self {
        let resubscriber = client.clone();
        let resubscribing = runtime.spawn(async move {
            loop {
                match reconnects.recv().await {
                    Ok(_) | Err(RecvError::Lagged(_)) => {}
                    Err(RecvError::Closed) => return,
                }
                let (name, outcome) = match &target {
                    Resubscribe::Channel(name) => (name, resubscriber.subscribe(name).await),
                    Resubscribe::ShardedChannel(name) => {
                        (name, resubscriber.ssubscribe(name).await)
                    }
                    Resubscribe::Pattern(name) => (name, resubscriber.psubscribe(name).await),
                };
                if let Err(error) = outcome {
                    warn!(
                        subscription = name.as_str(),
                        %error,
                        "a Redis Pub/Sub subscription reconnected but could not subscribe again; \
                         it receives nothing until its connection drops and reconnects"
                    );
                }
            }
        });
        Self {
            client,
            resubscribing,
        }
    }
}

/// The wire side of a Pub/Sub subscription: the dedicated client and the channel it feeds.
pub(crate) struct PubSubWire {
    /// The channel or the glob it reads, for the warning a lost message raises.
    name: String,
    client: Resubscribing,
    rx: Receiver<Message>,
    codec: Option<SharedEnvelope>,
    /// The broker's pool, handed to every delivery for `Ctx<keys::FredPool>`.
    pool: Pool,
    /// The subscription's registration with the in-process server; empty on a real connection.
    tap: Tap,
    /// The broker's runtime, where the dedicated client is closed once the subscription stops.
    runtime: Handle,
}

impl Debug for PubSubWire {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PubSubWire").finish_non_exhaustive()
    }
}

impl PubSubWire {
    pub(crate) fn new(
        name: String,
        client: Resubscribing,
        rx: Receiver<Message>,
        codec: Option<SharedEnvelope>,
        pool: Pool,
        tap: Tap,
        runtime: Handle,
    ) -> Self {
        Self {
            name,
            client,
            rx,
            codec,
            pool,
            tap,
            runtime,
        }
    }

    /// The window's client: a connection of the pool the flushes go out on.
    pub(crate) fn round_client(&self) -> Client {
        self.pool.next().clone()
    }

    /// Yields one delivery per message, each with a slot in `window`, telling the window how
    /// many more have arrived behind it, and reports a failed flush of the window once.
    pub(crate) fn round_stream<'a>(
        &'a mut self,
        window: &'a Arc<Window<PubSubForm>>,
    ) -> impl Stream<Item = Result<RoundMessage<PubSubForm>, RedisError>> + Send + 'a {
        let codec = self.codec.clone();
        let (pool, tap, name) = (&self.pool, &self.tap, self.name.as_str());
        unfold((&mut self.rx, codec), move |(rx, codec)| async move {
            loop {
                if let Some(failure) = window.take_failure() {
                    return Some((Err(failure), (rx, codec)));
                }
                match rx.recv().await {
                    Ok(msg) => {
                        let delivery = to_message(&msg, codec.as_ref(), pool, tap);
                        window.yielded(rx.len());
                        let round = window.open();
                        return Some((
                            Ok(RoundMessage::new(delivery, Arc::clone(window), round)),
                            (rx, codec),
                        ));
                    }
                    // The receiver fell behind the broadcast buffer; skip the gap and keep reading.
                    Err(RecvError::Lagged(skipped)) => lagged(name, tap, skipped),
                    Err(RecvError::Closed) => return None,
                }
            }
        })
    }
}

/// Reports the messages a subscription that fell behind its buffer lost, and forgets them.
fn lagged(name: &str, tap: &Tap, skipped: u64) {
    warn!(
        subscription = name,
        lost = skipped,
        "a Redis Pub/Sub subscription fell behind its buffer and lost the oldest messages; raise \
         its buffer or speed up its handler"
    );
    #[cfg(feature = "testing")]
    tap.discard(usize::try_from(skipped).unwrap_or(usize::MAX));
    #[cfg(not(feature = "testing"))]
    let _ = tap;
}

impl PubSubWire {
    /// Yields one batch of up to `size` messages: the first one waited for, and those already
    /// arrived behind it. The batch shares one slot of `window`.
    pub(crate) fn round_batches<'a>(
        &'a mut self,
        window: &'a Arc<Window<PubSubForm>>,
        size: NonZeroUsize,
    ) -> impl Stream<Item = Result<Vec<RoundMessage<PubSubForm>>, RedisError>> + Send + 'a {
        window.fill_at(size.get());
        let codec = self.codec.clone();
        let (pool, tap, name) = (&self.pool, &self.tap, self.name.as_str());
        unfold((&mut self.rx, codec), move |(rx, codec)| async move {
            loop {
                if let Some(failure) = window.take_failure() {
                    return Some((Err(failure), (rx, codec)));
                }
                let first = match rx.recv().await {
                    Ok(msg) => msg,
                    Err(RecvError::Lagged(skipped)) => {
                        lagged(name, tap, skipped);
                        continue;
                    }
                    Err(RecvError::Closed) => return None,
                };
                let mut messages = vec![first];
                while messages.len() < size.get() {
                    match rx.try_recv() {
                        Ok(msg) => messages.push(msg),
                        Err(TryRecvError::Lagged(skipped)) => lagged(name, tap, skipped),
                        Err(_) => break,
                    }
                }
                let members = u32::try_from(messages.len()).unwrap_or(u32::MAX);
                for _ in 0..members {
                    window.yielded(rx.len());
                }
                let round = window.open_batch(members);
                let batch = messages
                    .iter()
                    .map(|msg| {
                        RoundMessage::new(
                            to_message(msg, codec.as_ref(), pool, tap),
                            Arc::clone(window),
                            round.clone(),
                        )
                    })
                    .collect();
                return Some((Ok(batch), (rx, codec)));
            }
        })
    }
}

impl Drop for PubSubWire {
    fn drop(&mut self) {
        // What reached the subscription and was never read leaves with it: the in-process server
        // counted each message in flight when it sent it.
        #[cfg(feature = "testing")]
        {
            let mut unread = 0_usize;
            loop {
                match self.rx.try_recv() {
                    Ok(_) => unread += 1,
                    Err(TryRecvError::Lagged(skipped)) => {
                        unread =
                            unread.saturating_add(usize::try_from(skipped).unwrap_or(usize::MAX));
                    }
                    Err(_) => break,
                }
            }
            self.tap.discard(unread);
        }
        // The dedicated client owns a background connection task; close it on a detached task since
        // `drop` cannot await, on the broker's runtime, which is there wherever the drop happens.
        self.client.resubscribing.abort();
        let client = self.client.client.clone();
        self.runtime.spawn(async move {
            let _ = client.quit().await;
        });
    }
}

fn to_message(
    msg: &Message,
    codec: Option<&SharedEnvelope>,
    pool: &Pool,
    tap: &Tap,
) -> RedisPubSubMessage {
    let raw = msg.value.as_bytes().unwrap_or(&[]);
    let (payload, headers) = unframe(codec, raw);
    RedisPubSubMessage {
        channel: msg.channel.to_string(),
        // `PMessage` is the delivery kind for a `PSUBSCRIBE` match; the message's own channel is the
        // concrete one matched, which differs from the subscription's glob pattern.
        pattern: matches!(msg.kind, MessageKind::PMessage),
        payload,
        headers,
        pool: pool.clone(),
        flight: tap.delivered(),
    }
}

impl ruststream::Subscriber for PubSubWire {
    type Message = RedisPubSubMessage;
    type Error = RedisError;

    fn stream(&mut self) -> impl Stream<Item = Result<Self::Message, Self::Error>> + Send + '_ {
        let codec = self.codec.clone();
        let (pool, tap, name) = (&self.pool, &self.tap, self.name.as_str());
        unfold((&mut self.rx, codec), move |(rx, codec)| async move {
            loop {
                match rx.recv().await {
                    Ok(msg) => {
                        let message = to_message(&msg, codec.as_ref(), pool, tap);
                        return Some((Ok(message), (rx, codec)));
                    }
                    // The receiver fell behind the broadcast buffer; skip the gap and keep reading.
                    Err(RecvError::Lagged(skipped)) => lagged(name, tap, skipped),
                    Err(RecvError::Closed) => return None,
                }
            }
        })
    }
}

/// A Pub/Sub delivery. `ack` / `nack` are unsupported (Pub/Sub has no acknowledgement).
pub struct RedisPubSubMessage {
    channel: String,
    /// Whether this delivery arrived through a `PSUBSCRIBE` pattern match (vs an exact subscribe).
    pattern: bool,
    payload: Bytes,
    headers: HeaderMap,
    /// The broker's pool, which `Ctx<keys::FredPool>` hands the handler.
    pool: Pool,
    /// The test harness's count of this delivery; empty outside the in-process mode.
    #[cfg_attr(
        not(feature = "testing"),
        allow(
            dead_code,
            reason = "held for its release on drop, which only `testing` gives"
        )
    )]
    flight: InFlight,
}

impl Debug for RedisPubSubMessage {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSubMessage")
            .field("channel", &self.channel)
            .field("pattern", &self.pattern)
            .field("payload_len", &self.payload.len())
            .finish_non_exhaustive()
    }
}

impl RedisPubSubMessage {
    /// The channel this message arrived on.
    ///
    /// On a [`RedisPubSubPattern`] subscription this is the concrete channel the message was
    /// published to, which differs from the glob the subscription registered.
    #[must_use]
    pub fn channel(&self) -> &str {
        &self.channel
    }

    /// Whether this delivery arrived through a `PSUBSCRIBE` pattern match rather than an exact
    /// channel subscribe.
    #[must_use]
    pub fn from_pattern(&self) -> bool {
        self.pattern
    }

    /// The broker's connection pool, for the per-delivery context.
    pub(crate) const fn pool(&self) -> &Pool {
        &self.pool
    }

    /// Takes the harness's count of this delivery, for a window to hold until its flush.
    #[cfg(feature = "testing")]
    pub(crate) fn take_flight(&mut self) -> InFlight {
        std::mem::take(&mut self.flight)
    }
}

impl IncomingMessage for RedisPubSubMessage {
    fn payload(&self) -> &[u8] {
        &self.payload
    }

    fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    // See `RedisMessage`: the keyed worker lanes read the key from here, not from `Partitioned`.
    fn partition_key(&self) -> Option<&[u8]> {
        Partitioned::partition_key(self)
    }

    fn ack(self) -> impl Future<Output = Result<(), AckError>> {
        ready(Err(AckError::Unsupported))
    }

    fn nack(self, _requeue: bool) -> impl Future<Output = Result<(), AckError>> {
        ready(Err(AckError::Unsupported))
    }
}

impl Partitioned for RedisPubSubMessage {
    fn partition_key(&self) -> Option<&[u8]> {
        self.headers().get(PARTITION_KEY_HEADER)
    }
}

/// The declaration half of the Pub/Sub publisher: delivery mode and envelope codec, no connection.
///
/// Constructible anywhere (in a router definition, in configuration), it pairs into a
/// [`RedisPubSubPublisher`] against a [`ConnectedRedisBroker`]. The publish mode must match how
/// subscribers subscribed: a sharded publish only reaches sharded subscribers.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::stream::prelude::*;
/// use ruststream_fred::{PubSubMode, RedisPubSubPublish};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize)]
/// struct Order {
///     id: u64,
/// }
///
/// #[derive(Serialize, Outgoing)]
/// #[outgoing(name = "orders.live")]
/// struct Placed {
///     id: u64,
/// }
///
/// #[subscriber(RedisStream::new("orders").group("workers"), reply)]
/// async fn announce(order: &Order) -> Placed {
///     Placed { id: order.id }
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
///         RedisBroker::cluster(["10.0.0.1:6379"]),
///         |b| {
///             // Read from a durable stream, broadcast to whoever is listening with `SPUBLISH`.
///             b.include(announce)
///                 .out_reply(RedisPubSubPublish::new().mode(PubSubMode::Sharded));
///         },
///     )
/// }
/// # }
/// # fn main() {}
/// ```
#[derive(Clone, Default)]
#[must_use]
pub struct RedisPubSubPublish {
    mode: PubSubMode,
    codec: Option<SharedEnvelope>,
}

impl Debug for RedisPubSubPublish {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSubPublish")
            .field("mode", &self.mode)
            .field("codec", &self.codec.is_some())
            .finish()
    }
}

impl RedisPubSubPublish {
    /// A classic-mode policy with the default binary framing. Equivalent to [`Self::default`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the publish mode. Defaults to [`PubSubMode::Classic`].
    pub const fn mode(mut self, mode: PubSubMode) -> Self {
        self.mode = mode;
        self
    }

    /// Serializes the header/payload envelope with `codec` (must match the subscriber), which makes
    /// the wire value readable while the data is text. Without it the default binary framing is
    /// used. Either way a payload that is not text is published byte for byte.
    pub fn codec(mut self, codec: impl Codec + 'static) -> Self {
        self.codec = Some(Arc::new(codec));
        self
    }
}

impl PublishPolicy<ConnectedRedisBroker> for RedisPubSubPublish {
    type Live = RedisPubSubPublisher;

    fn pair(
        self,
        connected: &ConnectedRedisBroker,
    ) -> impl Future<Output = Result<Self::Live, PairError>> {
        ready(Ok(connected.pubsub_publisher(self)))
    }

    /// The channel a `PUBLISH` goes out on, the delivery mode it goes out in, and how headers are
    /// framed beside the payload.
    #[cfg(feature = "asyncapi")]
    fn channel_bindings(&self, channel: &str) -> Bindings {
        crate::asyncapi::channel(&crate::asyncapi::Publish::pubsub(
            channel,
            self.mode.as_str(),
            crate::asyncapi::Envelope::of(self.codec.as_ref()),
        ))
    }
}

/// Publishes Pub/Sub messages with `PUBLISH` (classic) or `SPUBLISH` (sharded): a
/// [`RedisPubSubPublish`] policy paired with a connection.
///
/// Obtain it from
/// [`ConnectedRedisBroker::pubsub_publisher`](crate::ConnectedRedisBroker::pubsub_publisher), or
/// by pairing the policy. Like every publisher here it may outlive the connection, so publishing
/// after shutdown reports [`RedisError::ShutDown`].
#[derive(Clone)]
pub struct RedisPubSubPublisher {
    core: Arc<RedisCore>,
    mode: PubSubMode,
    codec: Option<SharedEnvelope>,
    /// What `pipeline.bind(&out)` names this publisher and its clones by.
    round_name: u64,
}

impl Debug for RedisPubSubPublisher {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisPubSubPublisher")
            .field("mode", &self.mode)
            .field("codec", &self.codec.is_some())
            .finish_non_exhaustive()
    }
}

impl RedisPubSubPublisher {
    /// What `pipeline.bind(&out)` names this publisher and its clones by.
    pub(crate) const fn round_name(&self) -> u64 {
        self.round_name
    }

    pub(crate) fn new(core: Arc<RedisCore>, publish: RedisPubSubPublish) -> Self {
        Self {
            round_name: core.rounds().publisher(),
            core,
            mode: publish.mode,
            codec: publish.codec,
        }
    }
}

impl Publisher for RedisPubSubPublisher {
    /// The payload is read into an envelope of this crate's own before it reaches the client, so
    /// there is nothing to keep: the publish is lent the bytes where they already are.
    type Payload = Lend;

    type Error = RedisError;
    /// `PUBLISH` and `SPUBLISH` take a channel and a payload and nothing else, and which of the
    /// two is issued is the publisher's mode, fixed by the policy, because a sharded publish only
    /// reaches sharded subscribers. What a call site still says is the partition key, framed into
    /// the payload with the message's other headers.
    type Options = RedisPublishOptions;

    async fn publish(
        &self,
        msg: OutgoingMessage<'_>,
        options: Option<&Self::Options>,
    ) -> Result<(), Self::Error> {
        let (channel, payload, headers) = msg.into_parts();
        if let Some(round) = self
            .core
            .rounds()
            .joined(self.round_name, joins_round(options))
        {
            let body = frame(
                self.codec.as_ref(),
                payload,
                &resolved_headers(headers, options),
            );
            return round
                .publish(channel, body, self.mode == PubSubMode::Sharded)
                .await
                .map_err(RedisError::publish);
        }
        send(
            &self.core,
            self.mode,
            self.codec.as_ref(),
            channel,
            payload,
            resolved_headers(headers, options),
        )
        .await
    }
}

/// Publishes one framed message on `channel` in `mode`: the Pub/Sub publish, shared by
/// [`RedisPubSubPublisher`] and the broker's default publisher.
pub(crate) async fn send(
    core: &RedisCore,
    mode: PubSubMode,
    codec: Option<&SharedEnvelope>,
    channel: &str,
    payload: &[u8],
    headers: HeaderMap,
) -> Result<(), RedisError> {
    let pool = core.pool()?;
    let client = pool.next();
    let body = frame(codec, payload, &headers);
    let channel = channel.to_owned();
    let _: i64 = match mode {
        PubSubMode::Classic => client.publish(channel, body).await,
        PubSubMode::Sharded => client.spublish(channel, body).await,
    }
    .map_err(RedisError::publish)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::PubSubContext;
    use fred::types::config::Config;
    use ruststream::BuildContext;

    /// An unconnected pool (just client structs); `Pool::new` opens no sockets.
    fn offline_pool() -> Pool {
        Pool::new(Config::default(), None, None, None, 1).expect("offline pool")
    }

    #[test]
    fn build_context_reads_channel_and_pattern_flag() {
        let exact = RedisPubSubMessage {
            channel: "events".to_owned(),
            pattern: false,
            payload: Bytes::from_static(b"{}"),
            headers: HeaderMap::new(),
            pool: offline_pool(),
            flight: InFlight::default(),
        };
        let cx = PubSubContext::build(&exact);
        assert_eq!(cx.channel(), "events");
        assert!(!cx.from_pattern());

        let matched = RedisPubSubMessage {
            channel: "events.user".to_owned(),
            pattern: true,
            payload: Bytes::from_static(b"{}"),
            headers: HeaderMap::new(),
            pool: offline_pool(),
            flight: InFlight::default(),
        };
        assert!(PubSubContext::build(&matched).from_pattern());
    }

    /// A channel is one destination a `PUBLISH` names, whatever the delivery mode.
    #[test]
    fn a_channel_addresses_its_copies_to_itself() {
        assert_eq!(
            RedisPubSub::new("events").redelivery_channel().as_str(),
            "events"
        );
        assert_eq!(
            RedisPubSub::new("events")
                .mode(PubSubMode::Sharded)
                .redelivery_channel()
                .as_str(),
            "events"
        );
    }
}