turnloop-redis 0.1.0-beta.2

Pull-driven RESP Redis client, cluster and Sentinel protocols
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
#![deny(unsafe_op_in_unsafe_fn)]
//!
//! # Getting started on turnloop
//! Enable the `turnloop` feature for the `asynchronous` module. The embedding
//! host owns `LocalExecutor` and calls `turn`; adapters await its streams and
//! deadline futures. See the crate README and turnloop-io for ownership, streaming
//! and cancellation examples. Default features retain the sans-I/O API.

//! Sans-I/O Redis connection. The host drains `output`, acknowledges writes with
//! `consume_output`, supplies bytes/time, and polls events. Never reads a clock.
//! Tokens are unique among outstanding commands. A timeout leaves a wire-order
//! tombstone until its reply arrives, preventing later replies being misassigned.
pub mod resp;
pub mod routing;
use resp::{Limits, Value, encode_command};
use std::{
    collections::VecDeque,
    time::{Duration, Instant},
};

#[derive(Debug, Clone)]
pub struct Config {
    pub username: Option<String>,
    pub password: Option<String>,
    pub database: u32,
    pub client_name: Option<String>,
    pub tls: bool,
    pub prefer_resp3: bool,
    pub offline_queue: bool,
    pub auto_resubscribe: bool,
    pub auto_resend_unfulfilled: bool,
    pub max_retries_per_request: Option<u32>,
    pub connect_timeout: Duration,
    pub limits: Limits,
}
impl Default for Config {
    fn default() -> Self {
        Self {
            username: None,
            password: None,
            database: 0,
            client_name: None,
            tls: false,
            prefer_resp3: true,
            offline_queue: true,
            auto_resubscribe: true,
            auto_resend_unfulfilled: true,
            max_retries_per_request: Some(20),
            connect_timeout: Duration::from_secs(10),
            limits: Limits::default(),
        }
    }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    pub name: &'static str,
    pub message: String,
}
impl Error {
    fn new(message: &str) -> Self {
        Self {
            name: "Error",
            message: message.into(),
        }
    }
    fn reply(bytes: Vec<u8>) -> Self {
        Self {
            name: "ReplyError",
            message: String::from_utf8_lossy(&bytes).into_owned(),
        }
    }
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.name, self.message)
    }
}
impl std::error::Error for Error {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
    Disconnected,
    Connecting,
    Tls,
    Handshake,
    Ready,
    RetryDecision,
    WaitingRetry,
    Closed,
}
#[derive(Debug, PartialEq)]
pub enum Event {
    Connect,
    UpgradeTls,
    Ready {
        resp3: bool,
    },
    Reply {
        token: u64,
        result: Result<Value, Error>,
    },
    Message {
        pattern: Option<Vec<u8>>,
        channel: Vec<u8>,
        payload: Vec<u8>,
    },
    Push(Value),
    /// Evaluate retryStrategy(attempt) in the host, then call `retry`.
    Retry {
        attempt: u32,
    },
    Error(Error),
    CloseTransport,
    Closed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Normal,
    Subscribe,
    Psubscribe,
    Unsubscribe,
    Punsubscribe,
    Quit,
}
impl Kind {
    fn of(name: &[u8]) -> Self {
        if name.eq_ignore_ascii_case(b"SUBSCRIBE") {
            Self::Subscribe
        } else if name.eq_ignore_ascii_case(b"PSUBSCRIBE") {
            Self::Psubscribe
        } else if name.eq_ignore_ascii_case(b"UNSUBSCRIBE") {
            Self::Unsubscribe
        } else if name.eq_ignore_ascii_case(b"PUNSUBSCRIBE") {
            Self::Punsubscribe
        } else if name.eq_ignore_ascii_case(b"QUIT") {
            Self::Quit
        } else {
            Self::Normal
        }
    }
    fn subscription(self) -> bool {
        matches!(
            self,
            Self::Subscribe | Self::Psubscribe | Self::Unsubscribe | Self::Punsubscribe
        )
    }
}
struct Pending {
    token: Option<u64>,
    wire: Vec<u8>,
    deadline: Option<Instant>,
    kind: Kind,
    acks: usize,
}
#[derive(Clone, Copy)]
enum Handshake {
    Hello,
    Auth,
    Select,
    Name,
    Restore,
}
pub struct Connection {
    config: Config,
    state: State,
    resp3: bool,
    handshake: Handshake,
    tx: Vec<u8>,
    tx_pos: usize,
    rx: Vec<u8>,
    events: VecDeque<Event>,
    pending: VecDeque<Pending>,
    offline: VecDeque<Pending>,
    pool: Vec<Vec<u8>>,
    channels: Vec<Vec<u8>>,
    patterns: Vec<Vec<u8>>,
    subscriber: bool,
    deadline: Option<Instant>,
    attempt: u32,
}
impl Connection {
    pub fn new(config: Config) -> Self {
        Self {
            config,
            state: State::Disconnected,
            resp3: false,
            handshake: Handshake::Hello,
            tx: Vec::with_capacity(4096),
            tx_pos: 0,
            rx: Vec::with_capacity(4096),
            events: VecDeque::with_capacity(32),
            pending: VecDeque::with_capacity(32),
            offline: VecDeque::with_capacity(32),
            pool: Vec::with_capacity(32),
            channels: Vec::new(),
            patterns: Vec::new(),
            subscriber: false,
            deadline: None,
            attempt: 0,
        }
    }
    pub fn state(&self) -> State {
        self.state
    }
    pub fn poll_event(&mut self) -> Option<Event> {
        self.events.pop_front()
    }
    pub fn output(&self) -> &[u8] {
        &self.tx[self.tx_pos..]
    }
    pub fn consume_output(&mut self, count: usize) {
        assert!(count <= self.output().len());
        self.tx_pos += count;
        if self.tx_pos == self.tx.len() {
            self.tx.clear();
            self.tx_pos = 0;
        }
    }
    pub fn connect(&mut self, now: Instant) -> Result<(), Error> {
        if !matches!(
            self.state,
            State::Disconnected | State::Closed | State::WaitingRetry
        ) {
            return Err(Error::new("Redis is already connecting/connected"));
        }
        self.rx.clear();
        self.state = State::Connecting;
        self.deadline = now.checked_add(self.config.connect_timeout);
        self.events.push_back(Event::Connect);
        Ok(())
    }
    pub fn transport_connected(&mut self) -> Result<(), Error> {
        if self.state != State::Connecting {
            return Err(Error::new("Unexpected connection"));
        }
        if self.config.tls {
            self.state = State::Tls;
            self.events.push_back(Event::UpgradeTls);
        } else {
            self.begin_handshake();
        }
        Ok(())
    }
    pub fn tls_established(&mut self) -> Result<(), Error> {
        if self.state != State::Tls {
            return Err(Error::new("Unexpected TLS completion"));
        }
        self.begin_handshake();
        Ok(())
    }
    fn begin_handshake(&mut self) {
        self.state = State::Handshake;
        self.resp3 = false;
        self.subscriber = false;
        if self.config.prefer_resp3 {
            self.handshake = Handshake::Hello;
            if let Some(password) = &self.config.password {
                encode_command(
                    &[
                        b"HELLO",
                        b"3",
                        b"AUTH",
                        self.config
                            .username
                            .as_deref()
                            .unwrap_or("default")
                            .as_bytes(),
                        password.as_bytes(),
                    ],
                    &mut self.tx,
                );
            } else {
                encode_command(&[b"HELLO", b"3"], &mut self.tx);
            }
        } else {
            self.auth();
        }
    }
    fn auth(&mut self) {
        self.handshake = Handshake::Auth;
        if let Some(password) = &self.config.password {
            if let Some(user) = &self.config.username {
                encode_command(
                    &[b"AUTH", user.as_bytes(), password.as_bytes()],
                    &mut self.tx,
                );
            } else {
                encode_command(&[b"AUTH", password.as_bytes()], &mut self.tx);
            }
        } else {
            self.select();
        }
    }
    fn select(&mut self) {
        self.handshake = Handshake::Select;
        if self.config.database != 0 {
            encode_command(
                &[b"SELECT", self.config.database.to_string().as_bytes()],
                &mut self.tx,
            );
        } else {
            self.name();
        }
    }
    fn name(&mut self) {
        self.handshake = Handshake::Name;
        if let Some(name) = &self.config.client_name {
            encode_command(&[b"CLIENT", b"SETNAME", name.as_bytes()], &mut self.tx);
        } else {
            self.restore();
        }
    }
    fn restore(&mut self) {
        self.handshake = Handshake::Restore;
        if self.config.auto_resubscribe {
            for (kind, items) in [
                (Kind::Subscribe, &self.channels),
                (Kind::Psubscribe, &self.patterns),
            ] {
                for item in items {
                    let mut wire = self.pool.pop().unwrap_or_default();
                    wire.clear();
                    encode_command(
                        &[
                            if kind == Kind::Subscribe {
                                b"SUBSCRIBE"
                            } else {
                                b"PSUBSCRIBE"
                            },
                            item,
                        ],
                        &mut wire,
                    );
                    self.tx.extend_from_slice(&wire);
                    self.pending.push_back(Pending {
                        token: None,
                        wire,
                        deadline: None,
                        kind,
                        acks: 1,
                    });
                }
            }
            self.subscriber = !self.pending.is_empty();
        } else {
            self.channels.clear();
            self.patterns.clear();
        }
        if self.pending.is_empty() {
            self.ready();
        }
    }
    fn ready(&mut self) {
        self.state = State::Ready;
        self.deadline = None;
        self.attempt = 0;
        self.refresh_subscriber();
        self.events.push_back(Event::Ready { resp3: self.resp3 });
        while let Some(p) = self.offline.pop_front() {
            self.tx.extend_from_slice(&p.wire);
            self.pending.push_back(p);
        }
    }
    /// `deadline` is host policy, including commandTimeout or blocking-command
    /// protection. Redis's own BLPOP timeout remains in the command arguments.
    pub fn command(
        &mut self,
        token: u64,
        args: &[&[u8]],
        deadline: Option<Instant>,
    ) -> Result<(), Error> {
        let name = args.first().ok_or_else(|| Error::new("Empty command"))?;
        if self
            .pending
            .iter()
            .chain(&self.offline)
            .any(|p| p.token == Some(token))
        {
            return Err(Error::new("Duplicate command token"));
        }
        if matches!(self.state, State::Closed) {
            return Err(Error::new("Connection is closed."));
        }
        if self.state != State::Ready && !self.config.offline_queue {
            return Err(Error::new(
                "Stream isn't writeable and enableOfflineQueue options is false",
            ));
        }
        let kind = Kind::of(name);
        if self.subscriber
            && !(kind.subscription() || kind == Kind::Quit || name.eq_ignore_ascii_case(b"PING"))
        {
            return Err(Error::new(
                "Connection in subscriber mode, only subscriber commands may be used",
            ));
        }
        if matches!(kind, Kind::Subscribe | Kind::Psubscribe) {
            self.subscriber = true;
        }
        let acks = if args.len() > 1 {
            args.len() - 1
        } else {
            match kind {
                Kind::Unsubscribe | Kind::Punsubscribe => usize::MAX,
                _ => 1,
            }
        };
        let mut wire = self.pool.pop().unwrap_or_default();
        wire.clear();
        encode_command(args, &mut wire);
        let p = Pending {
            token: Some(token),
            wire,
            deadline,
            kind,
            acks,
        };
        if self.state == State::Ready {
            self.tx.extend_from_slice(&p.wire);
            self.pending.push_back(p);
        } else {
            self.offline.push_back(p);
        }
        Ok(())
    }
    pub fn receive(&mut self, bytes: &[u8]) -> Result<(), Error> {
        if !matches!(self.state, State::Ready | State::Handshake) {
            return Err(Error::new("Bytes outside Redis protocol state"));
        }
        // Limit buffering even when the host supplies many frames at once.
        for chunk in bytes.chunks(4096) {
            self.rx.extend_from_slice(chunk);
            let mut consumed = 0;
            loop {
                if self.state == State::Ready
                    && (self.subscriber || self.rx.get(consumed) == Some(&b'>'))
                {
                    match resp::message(&self.rx[consumed..], self.config.limits) {
                        Ok(Some((message, len))) => {
                            self.events.push_back(Event::Message {
                                pattern: message.pattern.map(<[u8]>::to_vec),
                                channel: message.channel.to_vec(),
                                payload: message.payload.to_vec(),
                            });
                            consumed += len;
                            continue;
                        }
                        Ok(None) => {}
                        Err(e) => {
                            let error = Error::new(&e.to_string());
                            self.close();
                            return Err(error);
                        }
                    }
                }
                match resp::decode(&self.rx[consumed..], self.config.limits) {
                    Ok(Some((value, n))) => {
                        consumed += n;
                        if let Err(error) = self.frame(value) {
                            self.close();
                            return Err(error);
                        }
                        if self.state == State::Closed {
                            return Ok(());
                        }
                    }
                    Ok(None) => break,
                    Err(e) => {
                        let error = Error::new(&e.to_string());
                        self.close();
                        return Err(error);
                    }
                }
            }
            self.rx.drain(..consumed);
        }
        Ok(())
    }
    fn frame(&mut self, value: Value) -> Result<(), Error> {
        if let Value::Attribute(_, value) = value {
            return self.frame(*value);
        }
        if self.state == State::Handshake && !matches!(self.handshake, Handshake::Restore) {
            if let Value::Error(bytes) = value {
                if matches!(self.handshake, Handshake::Hello)
                    && (bytes.starts_with(b"ERR unknown command") || bytes.starts_with(b"NOPROTO"))
                {
                    self.auth();
                    return Ok(());
                }
                self.events.push_back(Event::Error(Error::reply(bytes)));
                self.close();
                return Ok(());
            }
            match self.handshake {
                Handshake::Hello => {
                    self.resp3 = true;
                    self.select();
                }
                Handshake::Auth => self.select(),
                Handshake::Select => self.name(),
                Handshake::Name => self.restore(),
                Handshake::Restore => unreachable!(),
            }
            return Ok(());
        }
        let pubsub = matches!(value, Value::Push(_))
            || (self.subscriber && matches!(value, Value::Array(_)));
        if pubsub {
            if let Some(items) = value.items() {
                let tag = items.first().and_then(Value::bytes).unwrap_or_default();
                if (tag == b"message" && items.len() == 3)
                    || (tag == b"pmessage" && items.len() == 4)
                {
                    let mut items = match value {
                        Value::Array(v) | Value::Push(v) => v,
                        _ => unreachable!(),
                    };
                    let payload = take_bytes(items.pop().unwrap())?;
                    let channel = take_bytes(items.pop().unwrap())?;
                    let pattern = if items.len() == 2 {
                        Some(take_bytes(items.pop().unwrap())?)
                    } else {
                        None
                    };
                    self.events.push_back(Event::Message {
                        pattern,
                        channel,
                        payload,
                    });
                    return Ok(());
                }
                let kind = Kind::of(tag);
                if kind.subscription() && items.len() == 3 {
                    let target = if matches!(kind, Kind::Psubscribe | Kind::Punsubscribe) {
                        &mut self.patterns
                    } else {
                        &mut self.channels
                    };
                    if let Some(channel) = items[1].bytes() {
                        if matches!(kind, Kind::Subscribe | Kind::Psubscribe) {
                            if !target.iter().any(|v| v == channel) {
                                target.push(channel.to_vec());
                            }
                        } else {
                            target.retain(|v| v != channel);
                        }
                    }
                    let empty = target.is_empty();
                    let count = items[2]
                        .integer()
                        .ok_or_else(|| Error::new("Invalid subscription count"))?;
                    self.subscriber = count != 0;
                    if let Some(p) = self.pending.front_mut().filter(|p| p.kind == kind) {
                        let done = if p.acks == usize::MAX {
                            empty
                        } else {
                            p.acks = p.acks.saturating_sub(1);
                            p.acks == 0
                        };
                        if done {
                            self.complete(Value::Integer(count))?;
                        }
                        if self.state == State::Handshake && self.pending.is_empty() {
                            self.ready();
                        }
                    } else {
                        return Err(Error::new("Unexpected subscription acknowledgement"));
                    }
                    return Ok(());
                }
                if tag == b"pong" && !self.resp3 {
                    // RESP2 subscriber PING returns [pong, payload].
                    let pong = items
                        .get(1)
                        .and_then(Value::bytes)
                        .unwrap_or_default()
                        .to_vec();
                    self.complete(Value::Bulk(pong))?;
                    return Ok(());
                }
            }
            if matches!(value, Value::Push(_)) {
                self.events.push_back(Event::Push(value));
                return Ok(());
            }
        }
        self.complete(value)
    }
    fn complete(&mut self, value: Value) -> Result<(), Error> {
        let p = self
            .pending
            .pop_front()
            .ok_or_else(|| Error::new("Unexpected Redis reply"))?;
        if let Some(token) = p.token {
            let result = if let Value::Error(bytes) = value {
                Err(Error::reply(bytes))
            } else {
                Ok(value)
            };
            self.events.push_back(Event::Reply { token, result });
        }
        self.pool.push(p.wire);
        self.refresh_subscriber();
        if p.kind == Kind::Quit {
            self.close();
        }
        Ok(())
    }
    fn refresh_subscriber(&mut self) {
        self.subscriber = !self.channels.is_empty()
            || !self.patterns.is_empty()
            || self
                .pending
                .iter()
                .chain(&self.offline)
                .any(|p| matches!(p.kind, Kind::Subscribe | Kind::Psubscribe));
    }
    pub fn next_timeout(&self) -> Option<Instant> {
        self.deadline
            .into_iter()
            .chain(
                self.pending
                    .iter()
                    .chain(&self.offline)
                    .filter_map(|p| p.deadline),
            )
            .min()
    }
    pub fn handle_timeout(&mut self, now: Instant) {
        for p in &mut self.pending {
            if p.deadline.is_some_and(|d| d <= now) {
                p.deadline = None;
                if let Some(token) = p.token.take() {
                    self.events.push_back(Event::Reply {
                        token,
                        result: Err(Error::new("Command timed out")),
                    });
                }
            }
        }
        let mut i = 0;
        while i < self.offline.len() {
            if self.offline[i].deadline.is_some_and(|d| d <= now) {
                let p = self.offline.remove(i).unwrap();
                if let Some(token) = p.token {
                    self.events.push_back(Event::Reply {
                        token,
                        result: Err(Error::new("Command timed out")),
                    });
                }
                self.pool.push(p.wire);
            } else {
                i += 1;
            }
        }
        if self.deadline.is_some_and(|d| d <= now) {
            self.deadline = None;
            if self.state == State::WaitingRetry {
                let _ = self.connect(now);
            } else {
                self.events
                    .push_back(Event::Error(Error::new("connect ETIMEDOUT")));
                self.transport_lost();
            }
        }
    }
    /// Call once per failed transport, including DNS/connect/TLS failures. The
    /// host closes the socket on CloseTransport before accepting new Connect.
    pub fn transport_lost(&mut self) {
        if matches!(
            self.state,
            State::Closed | State::RetryDecision | State::WaitingRetry
        ) {
            return;
        }
        self.tx.clear();
        self.tx_pos = 0;
        self.rx.clear();
        self.deadline = None;
        while let Some(p) = self.pending.pop_back() {
            if self.config.auto_resend_unfulfilled && p.token.is_some() {
                self.offline.push_front(p);
            } else {
                self.fail_pending(p, "Connection is closed.");
            }
        }
        self.attempt = self.attempt.saturating_add(1);
        if self
            .config
            .max_retries_per_request
            .is_some_and(|n| self.attempt > n)
        {
            while let Some(p) = self.offline.pop_front() {
                self.fail_pending(p, "Reached the max retries per request limit");
            }
        }
        self.state = State::RetryDecision;
        self.events.push_back(Event::CloseTransport);
        self.events.push_back(Event::Retry {
            attempt: self.attempt,
        });
    }
    pub fn retry(&mut self, now: Instant, delay: Option<Duration>) -> Result<(), Error> {
        if self.state != State::RetryDecision {
            return Err(Error::new("No retry decision requested"));
        }
        if let Some(delay) = delay {
            self.state = State::WaitingRetry;
            self.deadline = now.checked_add(delay);
        } else {
            self.close();
        }
        Ok(())
    }
    fn fail_pending(&mut self, p: Pending, message: &str) {
        if let Some(token) = p.token {
            self.events.push_back(Event::Reply {
                token,
                result: Err(Error::new(message)),
            });
        }
        self.pool.push(p.wire);
    }
    pub fn close(&mut self) {
        if self.state == State::Closed {
            return;
        }
        while let Some(p) = self.pending.pop_front() {
            self.fail_pending(p, "Connection is closed.");
        }
        while let Some(p) = self.offline.pop_front() {
            self.fail_pending(p, "Connection is closed.");
        }
        self.state = State::Closed;
        self.deadline = None;
        self.tx.clear();
        self.tx_pos = 0;
        self.channels.clear();
        self.patterns.clear();
        self.subscriber = false;
        self.events.push_back(Event::CloseTransport);
        self.events.push_back(Event::Closed);
    }
}
fn take_bytes(value: Value) -> Result<Vec<u8>, Error> {
    match value {
        Value::Bulk(v) | Value::Simple(v) => Ok(v),
        _ => Err(Error::new("Invalid pub/sub message")),
    }
}

#[cfg(feature = "turnloop")]
pub mod asynchronous;