redis 0.32.0

Redis driver for Rust.
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
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
//! Defines a Sentinel type that connects to Redis sentinels and creates clients to
//! master or replica nodes.
//!
//! # Example
//! ```rust,no_run
//! use redis::Commands;
//! use redis::sentinel::Sentinel;
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
//! let mut sentinel = Sentinel::build(nodes).unwrap();
//! let mut master = sentinel.master_for("master_name", None).unwrap().get_connection().unwrap();
//! let mut replica = sentinel.replica_for("master_name", None).unwrap().get_connection().unwrap();
//!
//! let _: () = master.set("test", "test_data").unwrap();
//! let rv: String = replica.get("test").unwrap();
//!
//! assert_eq!(rv, "test_data");
//! ```
//!
//! There is also a SentinelClient which acts like a regular Client, providing the
//! `get_connection` and `get_async_connection` methods, internally using the Sentinel
//! type to create clients on demand for the desired node type (Master or Replica).
//!
//! # Example
//! ```rust,no_run
//! use redis::Commands;
//! use redis::sentinel::{ SentinelServerType, SentinelClient };
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
//! let mut master_client = SentinelClient::build(nodes.clone(), String::from("master_name"), None, SentinelServerType::Master).unwrap();
//! let mut replica_client = SentinelClient::build(nodes, String::from("master_name"), None, SentinelServerType::Replica).unwrap();
//! let mut master_conn = master_client.get_connection().unwrap();
//! let mut replica_conn = replica_client.get_connection().unwrap();
//!
//! let _: () = master_conn.set("test", "test_data").unwrap();
//! let rv: String = replica_conn.get("test").unwrap();
//!
//! assert_eq!(rv, "test_data");
//! ```
//!
//! If the sentinel's nodes are using TLS or require authentication, a full
//! SentinelNodeConnectionInfo struct may be used instead of just the master's name:
//!
//! # Example
//! ```rust,no_run
//! use redis::{ Commands, RedisConnectionInfo };
//! use redis::sentinel::{ Sentinel, SentinelNodeConnectionInfo };
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
//! let mut sentinel = Sentinel::build(nodes).unwrap();
//!
//! let mut master_with_auth = sentinel
//!     .master_for(
//!         "master_name",
//!         Some(&SentinelNodeConnectionInfo {
//!             tls_mode: None,
//!             redis_connection_info: Some(RedisConnectionInfo {
//!                 db: 1,
//!                 username: Some(String::from("foo")),
//!                 password: Some(String::from("bar")),
//!                 ..Default::default()
//!             }),
//!         }),
//!     )
//!     .unwrap()
//!     .get_connection()
//!     .unwrap();
//!
//! let mut replica_with_tls = sentinel
//!     .master_for(
//!         "master_name",
//!         Some(&SentinelNodeConnectionInfo {
//!             tls_mode: Some(redis::TlsMode::Secure),
//!             redis_connection_info: None,
//!         }),
//!     )
//!     .unwrap()
//!     .get_connection()
//!     .unwrap();
//! ```
//!
//! # Example
//! ```rust,no_run
//! use redis::{ Commands, RedisConnectionInfo };
//! use redis::sentinel::{ SentinelServerType, SentinelClient, SentinelNodeConnectionInfo };
//!
//! let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
//! let mut master_client = SentinelClient::build(
//!     nodes,
//!     String::from("master1"),
//!     Some(SentinelNodeConnectionInfo {
//!         tls_mode: Some(redis::TlsMode::Insecure),
//!         redis_connection_info: Some(RedisConnectionInfo {
//!             db: 0,
//!             username: Some(String::from("user")),
//!             password: Some(String::from("pass")),
//!             ..Default::default()
//!         }),
//!     }),
//!     redis::sentinel::SentinelServerType::Master,
//! )
//! .unwrap();
//! ```
//!
//! In addition, there is a `SentinelClientBuilder` to provide the most fine-grained configuration possibilities
//!
//! # Example
//! ```rust,no_run
//! use redis::sentinel::SentinelClientBuilder;
//! use redis::{ConnectionAddr, TlsCertificates};
//! let nodes = vec![ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6379), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6378), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6377)];
//!
//! let mut builder = SentinelClientBuilder::new(nodes, String::from("master"), redis::sentinel::SentinelServerType::Master).unwrap();
//!
//! builder = builder.set_client_to_sentinel_username(String::from("username1"));
//! builder = builder.set_client_to_sentinel_password(String::from("password1"));
//! builder = builder.set_client_to_sentinel_tls_mode(redis::TlsMode::Insecure);
//!
//! builder = builder.set_client_to_redis_username(String::from("username2"));
//! builder = builder.set_client_to_redis_password(String::from("password2"));
//! builder = builder.set_client_to_redis_tls_mode(redis::TlsMode::Secure);
//!
//!
//! let client = builder.build().unwrap();
//! ```
//!

#[cfg(feature = "aio")]
use crate::aio::MultiplexedConnection as AsyncConnection;
#[cfg(feature = "aio")]
use futures_util::StreamExt;
use rand::Rng;
#[cfg(feature = "r2d2")]
use std::sync::Mutex;
use std::{collections::HashMap, num::NonZeroUsize};

#[cfg(feature = "aio")]
use crate::aio::MultiplexedConnection;
#[cfg(feature = "aio")]
use crate::client::AsyncConnectionConfig;
#[cfg(feature = "tls-rustls")]
use crate::tls::retrieve_tls_certificates;
#[cfg(feature = "tls-rustls")]
use crate::TlsCertificates;
use crate::{
    connection::ConnectionInfo, types::RedisResult, Client, Cmd, Connection, ConnectionAddr,
    ErrorKind, FromRedisValue, IntoConnectionInfo, ProtocolVersion, RedisConnectionInfo,
    RedisError, Role, TlsMode,
};

/// The Sentinel type, serves as a special purpose client which builds other clients on
/// demand.
pub struct Sentinel {
    sentinels_connection_info: Vec<ConnectionInfo>,
    connections_cache: Vec<Option<Connection>>,
    #[cfg(feature = "aio")]
    async_connections_cache: Vec<Option<AsyncConnection>>,
    replica_start_index: usize,
    #[cfg(feature = "tls-rustls")]
    certs: Option<TlsCertificates>,
}

/// Holds the connection information that a sentinel should use when connecting to the
/// servers (masters and replicas) belonging to it.
#[derive(Clone, Default)]
pub struct SentinelNodeConnectionInfo {
    /// The TLS mode of the connection, or None if we do not want to connect using TLS
    /// (just a plain TCP connection).
    pub tls_mode: Option<TlsMode>,

    /// The Redis specific/connection independent information to be used.
    pub redis_connection_info: Option<RedisConnectionInfo>,
}

impl SentinelNodeConnectionInfo {
    fn create_connection_info(
        &self,
        ip: String,
        port: u16,
        #[cfg(feature = "tls-rustls")] certs: &Option<TlsCertificates>,
    ) -> RedisResult<ConnectionInfo> {
        let addr = match self.tls_mode {
            None => crate::ConnectionAddr::Tcp(ip, port),
            Some(TlsMode::Secure) => crate::ConnectionAddr::TcpTls {
                host: ip,
                port,
                insecure: false,
                #[cfg(not(feature = "tls-rustls"))]
                tls_params: None,
                #[cfg(feature = "tls-rustls")]
                tls_params: certs.as_ref().map(retrieve_tls_certificates).transpose()?,
            },
            Some(TlsMode::Insecure) => crate::ConnectionAddr::TcpTls {
                host: ip,
                port,
                insecure: true,
                tls_params: None,
            },
        };

        Ok(ConnectionInfo {
            addr,
            redis: self.redis_connection_info.clone().unwrap_or_default(),
        })
    }
}

impl Default for &SentinelNodeConnectionInfo {
    fn default() -> Self {
        static DEFAULT_VALUE: SentinelNodeConnectionInfo = SentinelNodeConnectionInfo {
            tls_mode: None,
            redis_connection_info: None,
        };
        &DEFAULT_VALUE
    }
}

fn sentinel_masters_cmd() -> crate::Cmd {
    let mut cmd = crate::cmd("SENTINEL");
    cmd.arg("MASTERS");
    cmd
}

fn sentinel_replicas_cmd(master_name: &str) -> crate::Cmd {
    let mut cmd = crate::cmd("SENTINEL");
    cmd.arg("SLAVES"); // For compatibility with older redis versions
    cmd.arg(master_name);
    cmd
}

fn is_master_valid(master_info: &HashMap<String, String>, service_name: &str) -> bool {
    master_info.get("name").map(|s| s.as_str()) == Some(service_name)
        && master_info.contains_key("ip")
        && master_info.contains_key("port")
        && master_info.get("flags").is_some_and(|flags| {
            flags.contains("master") && !flags.contains("s_down") && !flags.contains("o_down")
        })
        && master_info["port"].parse::<u16>().is_ok()
}

fn is_replica_valid(replica_info: &HashMap<String, String>) -> bool {
    replica_info.contains_key("ip")
        && replica_info.contains_key("port")
        && replica_info
            .get("flags")
            .is_some_and(|flags| !flags.contains("s_down") && !flags.contains("o_down"))
        && replica_info["port"].parse::<u16>().is_ok()
}

/// Generates a random value in the 0..max range.
fn random_replica_index(max: NonZeroUsize) -> usize {
    rand::rng().random_range(0..max.into())
}

fn try_connect_to_first_replica(
    addresses: &[ConnectionInfo],
    start_index: Option<usize>,
) -> Result<Client, crate::RedisError> {
    if addresses.is_empty() {
        fail!((
            ErrorKind::NoValidReplicasFoundBySentinel,
            "No valid replica found in sentinel for given name",
        ));
    }

    let start_index = start_index.unwrap_or(0);

    let mut last_err = None;
    for i in 0..addresses.len() {
        let index = (i + start_index) % addresses.len();
        match Client::open(addresses[index].clone()) {
            Ok(client) => return Ok(client),
            Err(err) => last_err = Some(err),
        }
    }

    // We can unwrap here because we know there is at least one error, since there is at
    // least one address, so we'll either return a client for it or store an error in
    // last_err.
    Err(last_err.expect("There should be an error because there is should be at least one address"))
}

fn valid_addrs<'a>(
    servers_info: Vec<HashMap<String, String>>,
    validate: impl Fn(&HashMap<String, String>) -> bool + 'a,
) -> impl Iterator<Item = (String, u16)> {
    servers_info
        .into_iter()
        .filter(move |info| validate(info))
        .map(|mut info| {
            // We can unwrap here because we already checked everything
            let ip = info.remove("ip").unwrap();
            let port = info["port"].parse::<u16>().unwrap();
            (ip, port)
        })
}

fn determine_master_from_role_or_info_replication(
    connection_info: &ConnectionInfo,
) -> RedisResult<bool> {
    let client = Client::open(connection_info.clone())?;
    let mut conn = client.get_connection()?;

    //Once the client discovered the address of the master instance, it should attempt a connection with the master, and call the ROLE command in order to verify the role of the instance is actually a master.
    let role = check_role(&mut conn);
    if role.is_ok_and(|x| matches!(x, Role::Primary { .. })) {
        return Ok(true);
    }

    //If the ROLE commands is not available (it was introduced in Redis 2.8.12), a client may resort to the INFO replication command parsing the role: field of the output.
    let role = check_info_replication(&mut conn);
    if role.is_ok_and(|x| x == "master") {
        return Ok(true);
    }

    //TODO: Maybe there should be some kind of error message if both role checks fail due to ACL permissions?
    Ok(false)
}

fn get_node_role(connection_info: &ConnectionInfo) -> RedisResult<Role> {
    let client = Client::open(connection_info.clone())?;
    let mut conn = client.get_connection()?;
    crate::cmd("ROLE").query(&mut conn)
}

fn check_role(conn: &mut Connection) -> RedisResult<Role> {
    crate::cmd("ROLE").query(conn)
}

fn check_info_replication(conn: &mut Connection) -> RedisResult<String> {
    let info: String = crate::cmd("INFO").arg("REPLICATION").query(conn)?;

    //Taken from test_sentinel parse_replication_info
    let info_map = parse_replication_info(info);
    match info_map.get("role") {
        Some(x) => Ok(x.clone()),
        None => Err(RedisError::from((ErrorKind::ParseError, "parse error"))),
    }
}

fn parse_replication_info(value: String) -> HashMap<String, String> {
    let info_map: std::collections::HashMap<String, String> = value
        .split("\r\n")
        .filter(|line| !line.trim_start().starts_with('#'))
        .filter_map(|line| line.split_once(':'))
        .map(|(key, val)| (key.to_string(), val.to_string()))
        .collect();
    info_map
}

/// Searches for a valid master with the given name in the list of masters returned by
/// a sentinel. A valid master is one which has a role of "master" (checked by running
/// the `ROLE` command and by seeing if its flags contains the "master" flag) and which
/// does not have the flags s_down or o_down set to it (these flags are returned by the
/// `SENTINEL MASTERS` command, and we expect the `masters` parameter to be the result of
/// that command).
fn find_valid_master(
    masters: Vec<HashMap<String, String>>,
    service_name: &str,
    node_connection_info: &SentinelNodeConnectionInfo,
    #[cfg(feature = "tls-rustls")] certs: &Option<TlsCertificates>,
) -> RedisResult<ConnectionInfo> {
    for (ip, port) in valid_addrs(masters, |m| is_master_valid(m, service_name)) {
        #[cfg(not(feature = "tls-rustls"))]
        let connection_info = node_connection_info.create_connection_info(ip, port)?;
        #[cfg(feature = "tls-rustls")]
        let connection_info = node_connection_info.create_connection_info(ip, port, certs)?;
        if determine_master_from_role_or_info_replication(&connection_info).is_ok_and(|x| x) {
            return Ok(connection_info);
        }
    }

    fail!((
        ErrorKind::MasterNameNotFoundBySentinel,
        "Master with given name not found in sentinel",
    ))
}

#[cfg(feature = "aio")]
async fn async_determine_master_from_role_or_info_replication(
    connection_info: &ConnectionInfo,
) -> RedisResult<bool> {
    let client = Client::open(connection_info.clone())?;
    let mut conn = client.get_multiplexed_async_connection().await?;

    //Once the client discovered the address of the master instance, it should attempt a connection with the master, and call the ROLE command in order to verify the role of the instance is actually a master.
    let role = async_check_role(&mut conn).await;
    if role.is_ok_and(|x| matches!(x, Role::Primary { .. })) {
        return Ok(true);
    }

    //If the ROLE commands is not available (it was introduced in Redis 2.8.12), a client may resort to the INFO replication command parsing the role: field of the output.
    let role = async_check_info_replication(&mut conn).await;
    if role.is_ok_and(|x| x == "master") {
        return Ok(true);
    }

    //TODO: Maybe there should be some kind of error message if both role checks fail due to ACL permissions?
    Ok(false)
}

#[cfg(feature = "aio")]
async fn async_determine_slave_from_role_or_info_replication(
    connection_info: &ConnectionInfo,
) -> RedisResult<bool> {
    let client = Client::open(connection_info.clone())?;
    let mut conn = client.get_multiplexed_async_connection().await?;

    //Once the client discovered the address of the master instance, it should attempt a connection with the master, and call the ROLE command in order to verify the role of the instance is actually a master.
    let role = async_check_role(&mut conn).await;
    if role.is_ok_and(|x| matches!(x, Role::Replica { .. })) {
        return Ok(true);
    }

    //If the ROLE commands is not available (it was introduced in Redis 2.8.12), a client may resort to the INFO replication command parsing the role: field of the output.
    let role = async_check_info_replication(&mut conn).await;
    if role.is_ok_and(|x| x == "slave") {
        return Ok(true);
    }

    //TODO: Maybe there should be some kind of error message if both role checks fail due to ACL permissions?
    Ok(false)
}

#[cfg(feature = "aio")]
async fn async_check_role(conn: &mut MultiplexedConnection) -> RedisResult<Role> {
    let role: RedisResult<Role> = crate::cmd("ROLE").query_async(conn).await;
    role
}

#[cfg(feature = "aio")]
async fn async_check_info_replication(conn: &mut MultiplexedConnection) -> RedisResult<String> {
    let info: String = crate::cmd("INFO")
        .arg("REPLICATION")
        .query_async(conn)
        .await?;
    //Taken from test_sentinel parse_replication_info
    let info_map = parse_replication_info(info);
    match info_map.get("role") {
        Some(x) => Ok(x.clone()),
        None => Err(RedisError::from((ErrorKind::ParseError, "parse error"))),
    }
}

/// Async version of [find_valid_master].
#[cfg(feature = "aio")]
async fn async_find_valid_master(
    masters: Vec<HashMap<String, String>>,
    service_name: &str,
    node_connection_info: &SentinelNodeConnectionInfo,
    #[cfg(feature = "tls-rustls")] certs: &Option<TlsCertificates>,
) -> RedisResult<ConnectionInfo> {
    for (ip, port) in valid_addrs(masters, |m| is_master_valid(m, service_name)) {
        #[cfg(not(feature = "tls-rustls"))]
        let connection_info = node_connection_info.create_connection_info(ip, port)?;
        #[cfg(feature = "tls-rustls")]
        let connection_info = node_connection_info.create_connection_info(ip, port, certs)?;
        if async_determine_master_from_role_or_info_replication(&connection_info)
            .await
            .is_ok_and(|x| x)
        {
            return Ok(connection_info);
        }
    }

    fail!((
        ErrorKind::MasterNameNotFoundBySentinel,
        "Master with given name not found in sentinel",
    ))
}

#[cfg(not(feature = "tls-rustls"))]
fn get_valid_replicas_addresses(
    replicas: Vec<HashMap<String, String>>,
    node_connection_info: &SentinelNodeConnectionInfo,
) -> RedisResult<Vec<ConnectionInfo>> {
    let addresses = valid_addrs(replicas, is_replica_valid)
        .map(|(ip, port)| node_connection_info.create_connection_info(ip, port))
        .collect::<RedisResult<Vec<ConnectionInfo>>>()?;

    Ok(addresses
        .into_iter()
        .filter(|connection_info| {
            get_node_role(connection_info).is_ok_and(|x| matches!(x, Role::Replica { .. }))
        })
        .collect())
}

#[cfg(feature = "tls-rustls")]
fn get_valid_replicas_addresses(
    replicas: Vec<HashMap<String, String>>,
    node_connection_info: &SentinelNodeConnectionInfo,
    certs: &Option<TlsCertificates>,
) -> RedisResult<Vec<ConnectionInfo>> {
    let addresses = valid_addrs(replicas, is_replica_valid)
        .map(|(ip, port)| node_connection_info.create_connection_info(ip, port, certs))
        .collect::<RedisResult<Vec<ConnectionInfo>>>()?;

    Ok(addresses
        .into_iter()
        .filter(|connection_info| {
            get_node_role(connection_info).is_ok_and(|x| matches!(x, Role::Replica { .. }))
        })
        .collect())
}

#[cfg(all(feature = "aio", not(feature = "tls-rustls")))]
async fn async_get_valid_replicas_addresses(
    replicas: Vec<HashMap<String, String>>,
    node_connection_info: &SentinelNodeConnectionInfo,
) -> RedisResult<Vec<ConnectionInfo>> {
    async fn is_replica_role_valid(connection_info: ConnectionInfo) -> Option<ConnectionInfo> {
        match async_determine_slave_from_role_or_info_replication(&connection_info).await {
            Ok(x) => {
                if x {
                    Some(connection_info)
                } else {
                    None
                }
            }
            Err(_e) => None,
        }
    }

    let addresses = valid_addrs(replicas, is_replica_valid)
        .map(|(ip, port)| node_connection_info.create_connection_info(ip, port))
        .collect::<RedisResult<Vec<_>>>()?;

    Ok(futures_util::stream::iter(addresses)
        .filter_map(is_replica_role_valid)
        .collect()
        .await)
}

#[cfg(all(feature = "aio", feature = "tls-rustls"))]
async fn async_get_valid_replicas_addresses(
    replicas: Vec<HashMap<String, String>>,
    node_connection_info: &SentinelNodeConnectionInfo,
    certs: &Option<TlsCertificates>,
) -> RedisResult<Vec<ConnectionInfo>> {
    async fn is_replica_role_valid(connection_info: ConnectionInfo) -> Option<ConnectionInfo> {
        match async_determine_slave_from_role_or_info_replication(&connection_info).await {
            Ok(x) => {
                if x {
                    Some(connection_info)
                } else {
                    None
                }
            }
            Err(_e) => None,
        }
    }

    let addresses = valid_addrs(replicas, is_replica_valid)
        .map(|(ip, port)| node_connection_info.create_connection_info(ip, port, certs))
        .collect::<RedisResult<Vec<_>>>()?;

    Ok(futures_util::stream::iter(addresses)
        .filter_map(is_replica_role_valid)
        .collect()
        .await)
}

#[cfg(feature = "aio")]
async fn async_reconnect(
    connection: &mut Option<AsyncConnection>,
    connection_info: &ConnectionInfo,
) -> RedisResult<()> {
    let sentinel_client = Client::open(connection_info.clone())?;
    let new_connection = sentinel_client.get_multiplexed_async_connection().await?;
    connection.replace(new_connection);
    Ok(())
}

#[cfg(feature = "aio")]
async fn async_try_single_sentinel<T: FromRedisValue>(
    cmd: Cmd,
    connection_info: &ConnectionInfo,
    cached_connection: &mut Option<AsyncConnection>,
) -> RedisResult<T> {
    if cached_connection.is_none() {
        async_reconnect(cached_connection, connection_info).await?;
    }

    let result = cmd.query_async(cached_connection.as_mut().unwrap()).await;

    if let Err(err) = result {
        if err.is_unrecoverable_error() || err.is_io_error() {
            async_reconnect(cached_connection, connection_info).await?;
            cmd.query_async(cached_connection.as_mut().unwrap()).await
        } else {
            Err(err)
        }
    } else {
        result
    }
}

fn reconnect(
    connection: &mut Option<Connection>,
    connection_info: &ConnectionInfo,
) -> RedisResult<()> {
    let sentinel_client = Client::open(connection_info.clone())?;
    let new_connection = sentinel_client.get_connection()?;
    connection.replace(new_connection);
    Ok(())
}

fn try_single_sentinel<T: FromRedisValue>(
    cmd: Cmd,
    connection_info: &ConnectionInfo,
    cached_connection: &mut Option<Connection>,
) -> RedisResult<T> {
    if cached_connection.is_none() {
        reconnect(cached_connection, connection_info)?;
    }

    let result = cmd.query(cached_connection.as_mut().unwrap());

    if let Err(err) = result {
        if err.is_unrecoverable_error() || err.is_io_error() {
            reconnect(cached_connection, connection_info)?;
            cmd.query(cached_connection.as_mut().unwrap())
        } else {
            Err(err)
        }
    } else {
        result
    }
}

// non-async methods
impl Sentinel {
    #[cfg(not(feature = "tls-rustls"))]
    /// Creates a Sentinel client performing some basic
    /// checks on the URLs that might make the operation fail.
    pub fn build<T: IntoConnectionInfo>(params: Vec<T>) -> RedisResult<Sentinel> {
        Self::build_inner(params)
    }

    #[cfg(feature = "tls-rustls")]
    /// Creates a Sentinel client performing some basic
    /// checks on the URLs that might make the operation fail.
    pub fn build<T: IntoConnectionInfo>(params: Vec<T>) -> RedisResult<Sentinel> {
        Self::build_inner(params, None)
    }

    fn build_inner<T: IntoConnectionInfo>(
        params: Vec<T>,
        #[cfg(feature = "tls-rustls")] certs: Option<TlsCertificates>,
    ) -> RedisResult<Sentinel> {
        if params.is_empty() {
            fail!((
                ErrorKind::EmptySentinelList,
                "At least one sentinel is required",
            ))
        }

        let sentinels_connection_info = params
            .into_iter()
            .map(|p| p.into_connection_info())
            .collect::<RedisResult<Vec<ConnectionInfo>>>()?;

        let mut connections_cache = vec![];
        connections_cache.resize_with(sentinels_connection_info.len(), Default::default);

        #[cfg(all(feature = "aio", feature = "tls-rustls"))]
        {
            let mut async_connections_cache = vec![];
            async_connections_cache.resize_with(sentinels_connection_info.len(), Default::default);

            Ok(Sentinel {
                sentinels_connection_info,
                connections_cache,
                async_connections_cache,
                replica_start_index: random_replica_index(NonZeroUsize::new(1000000).unwrap()),
                certs,
            })
        }

        #[cfg(all(feature = "aio", not(feature = "tls-rustls")))]
        {
            let mut async_connections_cache = vec![];
            async_connections_cache.resize_with(sentinels_connection_info.len(), Default::default);

            Ok(Sentinel {
                sentinels_connection_info,
                connections_cache,
                async_connections_cache,
                replica_start_index: random_replica_index(NonZeroUsize::new(1000000).unwrap()),
            })
        }

        #[cfg(all(not(feature = "aio"), feature = "tls-rustls"))]
        {
            Ok(Sentinel {
                sentinels_connection_info,
                connections_cache,
                replica_start_index: random_replica_index(NonZeroUsize::new(1000000).unwrap()),
                certs,
            })
        }

        #[cfg(all(not(feature = "aio"), not(feature = "tls-rustls")))]
        {
            Ok(Sentinel {
                sentinels_connection_info,
                connections_cache,
                replica_start_index: random_replica_index(NonZeroUsize::new(1000000).unwrap()),
            })
        }
    }

    /// Try to execute the given command in each sentinel, returning the result of the
    /// first one that executes without errors. If all return errors, we return the
    /// error of the last attempt.
    ///
    /// For each sentinel, we first check if there is a cached connection, and if not
    /// we attempt to connect to it (skipping that sentinel if there is an error during
    /// the connection). Then, we attempt to execute the given command with the cached
    /// connection. If there is an error indicating that the connection is invalid, we
    /// reconnect and try one more time in the new connection.
    ///
    fn try_all_sentinels<T: FromRedisValue>(&mut self, cmd: Cmd) -> RedisResult<T> {
        let mut last_err = None;
        for (connection_info, cached_connection) in self
            .sentinels_connection_info
            .iter()
            .zip(self.connections_cache.iter_mut())
        {
            match try_single_sentinel(cmd.clone(), connection_info, cached_connection) {
                Ok(result) => {
                    return Ok(result);
                }
                Err(err) => {
                    last_err = Some(err);
                }
            }
        }

        // We can unwrap here because we know there is at least one connection info.
        Err(last_err.expect("There should be at least one connection info"))
    }

    /// Get a list of all masters (using the command SENTINEL MASTERS) from the
    /// sentinels.
    fn get_sentinel_masters(&mut self) -> RedisResult<Vec<HashMap<String, String>>> {
        self.try_all_sentinels(sentinel_masters_cmd())
    }

    fn get_sentinel_replicas(
        &mut self,
        service_name: &str,
    ) -> RedisResult<Vec<HashMap<String, String>>> {
        self.try_all_sentinels(sentinel_replicas_cmd(service_name))
    }

    #[cfg(not(feature = "tls-rustls"))]
    fn find_master_address(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<ConnectionInfo> {
        let masters = self.get_sentinel_masters()?;
        find_valid_master(masters, service_name, node_connection_info)
    }

    #[cfg(feature = "tls-rustls")]
    fn find_master_address(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<ConnectionInfo> {
        let masters = self.get_sentinel_masters()?;
        find_valid_master(masters, service_name, node_connection_info, &self.certs)
    }

    #[cfg(not(feature = "tls-rustls"))]
    fn find_valid_replica_addresses(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<Vec<ConnectionInfo>> {
        let replicas = self.get_sentinel_replicas(service_name)?;
        get_valid_replicas_addresses(replicas, node_connection_info)
    }

    #[cfg(feature = "tls-rustls")]
    fn find_valid_replica_addresses(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<Vec<ConnectionInfo>> {
        let replicas = self.get_sentinel_replicas(service_name)?;
        get_valid_replicas_addresses(replicas, node_connection_info, &self.certs)
    }

    /// Determines the masters address for the given name, and returns a client for that
    /// master.
    pub fn master_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let connection_info =
            self.find_master_address(service_name, node_connection_info.unwrap_or_default())?;
        Client::open(connection_info)
    }

    /// Connects to a randomly chosen replica of the given master name.
    pub fn replica_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let addresses = self
            .find_valid_replica_addresses(service_name, node_connection_info.unwrap_or_default())?;
        let start_index = NonZeroUsize::new(addresses.len()).map(random_replica_index);
        try_connect_to_first_replica(&addresses, start_index)
    }

    /// Attempts to connect to a different replica of the given master name each time.
    /// There is no guarantee that we'll actually be connecting to a different replica
    /// in the next call, but in a static set of replicas (no replicas added or
    /// removed), on average we'll choose each replica the same number of times.
    pub fn replica_rotate_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let addresses = self
            .find_valid_replica_addresses(service_name, node_connection_info.unwrap_or_default())?;
        if !addresses.is_empty() {
            self.replica_start_index = (self.replica_start_index + 1) % addresses.len();
        }
        try_connect_to_first_replica(&addresses, Some(self.replica_start_index))
    }
}

// Async versions of the public methods above, along with async versions of private
// methods required for the public methods.
#[cfg(feature = "aio")]
impl Sentinel {
    async fn async_try_all_sentinels<T: FromRedisValue>(&mut self, cmd: Cmd) -> RedisResult<T> {
        let mut last_err = None;
        for (connection_info, cached_connection) in self
            .sentinels_connection_info
            .iter()
            .zip(self.async_connections_cache.iter_mut())
        {
            match async_try_single_sentinel(cmd.clone(), connection_info, cached_connection).await {
                Ok(result) => {
                    return Ok(result);
                }
                Err(err) => {
                    last_err = Some(err);
                }
            }
        }

        // We can unwrap here because we know there is at least one connection info.
        Err(last_err.expect("There should be at least one connection info"))
    }

    async fn async_get_sentinel_masters(&mut self) -> RedisResult<Vec<HashMap<String, String>>> {
        self.async_try_all_sentinels(sentinel_masters_cmd()).await
    }

    async fn async_get_sentinel_replicas(
        &mut self,
        service_name: &str,
    ) -> RedisResult<Vec<HashMap<String, String>>> {
        self.async_try_all_sentinels(sentinel_replicas_cmd(service_name))
            .await
    }

    #[cfg(not(feature = "tls-rustls"))]
    async fn async_find_master_address(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<ConnectionInfo> {
        let masters = self.async_get_sentinel_masters().await?;
        async_find_valid_master(masters, service_name, node_connection_info).await
    }

    #[cfg(feature = "tls-rustls")]
    async fn async_find_master_address(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<ConnectionInfo> {
        let masters = self.async_get_sentinel_masters().await?;
        async_find_valid_master(masters, service_name, node_connection_info, &self.certs).await
    }

    #[cfg(not(feature = "tls-rustls"))]
    async fn async_find_valid_replica_addresses(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<Vec<ConnectionInfo>> {
        let replicas = self.async_get_sentinel_replicas(service_name).await?;
        async_get_valid_replicas_addresses(replicas, node_connection_info).await
    }

    #[cfg(feature = "tls-rustls")]
    async fn async_find_valid_replica_addresses(
        &mut self,
        service_name: &str,
        node_connection_info: &SentinelNodeConnectionInfo,
    ) -> RedisResult<Vec<ConnectionInfo>> {
        let replicas = self.async_get_sentinel_replicas(service_name).await?;
        async_get_valid_replicas_addresses(replicas, node_connection_info, &self.certs).await
    }

    /// Determines the masters address for the given name, and returns a client for that
    /// master.
    pub async fn async_master_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let address = self
            .async_find_master_address(service_name, node_connection_info.unwrap_or_default())
            .await?;
        Client::open(address)
    }

    /// Connects to a randomly chosen replica of the given master name.
    pub async fn async_replica_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let addresses = self
            .async_find_valid_replica_addresses(
                service_name,
                node_connection_info.unwrap_or_default(),
            )
            .await?;
        let start_index = NonZeroUsize::new(addresses.len()).map(random_replica_index);
        try_connect_to_first_replica(&addresses, start_index)
    }

    /// Attempts to connect to a different replica of the given master name each time.
    /// There is no guarantee that we'll actually be connecting to a different replica
    /// in the next call, but in a static set of replicas (no replicas added or
    /// removed), on average we'll choose each replica the same number of times.
    pub async fn async_replica_rotate_for(
        &mut self,
        service_name: &str,
        node_connection_info: Option<&SentinelNodeConnectionInfo>,
    ) -> RedisResult<Client> {
        let addresses = self
            .async_find_valid_replica_addresses(
                service_name,
                node_connection_info.unwrap_or_default(),
            )
            .await?;
        if !addresses.is_empty() {
            self.replica_start_index = (self.replica_start_index + 1) % addresses.len();
        }
        try_connect_to_first_replica(&addresses, Some(self.replica_start_index))
    }
}

/// Enum defining the server types from a sentinel's point of view.
#[derive(Debug, Clone)]
pub enum SentinelServerType {
    /// Master connections only
    Master,
    /// Replica connections only
    Replica,
}

/// LockedSentinelClient is a wrapper around SentinelClient usable in r2d2.
// [internal comment]: this was added since SentinelClient requires &mut ref for get_connection()
// and we can't use it like this inside the r2d2 Manager, which requires a shared reference.
#[cfg(feature = "r2d2")]
pub struct LockedSentinelClient(pub(crate) Mutex<SentinelClient>);

#[cfg(feature = "r2d2")]
impl LockedSentinelClient {
    /// new creates a LockedSentinelClient by wrapping a new Mutex around the SentinelClient
    pub fn new(client: SentinelClient) -> Self {
        LockedSentinelClient(Mutex::new(client))
    }

    /// get_connection is the override for LockedSentinelClient to make it possible to
    /// use LockedSentinelClient with r2d2 macro.
    pub fn get_connection(&self) -> RedisResult<Connection> {
        self.0.lock().unwrap().get_connection()
    }
}

/// A utility wrapping `Sentinel` with an interface similar to [Client].
///
/// Uses the Sentinel type internally. This is a utility to help make it easier
/// to use sentinels but with an interface similar to the client (`get_connection` and
/// `get_async_connection`). The type of server (master or replica) and name of the
/// desired master are specified when constructing an instance, so it will always
/// return connections to the same target (for example, always to the master with name
/// "mymaster123", or always to replicas of the master "another-master-abc").
pub struct SentinelClient {
    sentinel: Sentinel,
    service_name: String,
    node_connection_info: SentinelNodeConnectionInfo,
    server_type: SentinelServerType,
}

impl SentinelClient {
    #[cfg(not(feature = "tls-rustls"))]
    /// Creates a SentinelClient performing some basic checks on the URLs that might
    /// result in an error.
    pub fn build<T: IntoConnectionInfo>(
        params: Vec<T>,
        service_name: String,
        node_connection_info: Option<SentinelNodeConnectionInfo>,
        server_type: SentinelServerType,
    ) -> RedisResult<Self> {
        Self::build_inner(params, service_name, node_connection_info, server_type)
    }

    #[cfg(feature = "tls-rustls")]
    /// Creates a SentinelClient performing some basic checks on the URLs that might
    /// result in an error.
    pub fn build<T: IntoConnectionInfo>(
        params: Vec<T>,
        service_name: String,
        node_connection_info: Option<SentinelNodeConnectionInfo>,
        server_type: SentinelServerType,
    ) -> RedisResult<Self> {
        Self::build_inner(
            params,
            service_name,
            node_connection_info,
            server_type,
            None,
        )
    }

    fn build_inner<T: IntoConnectionInfo>(
        params: Vec<T>,
        service_name: String,
        node_connection_info: Option<SentinelNodeConnectionInfo>,
        server_type: SentinelServerType,
        #[cfg(feature = "tls-rustls")] certs: Option<TlsCertificates>,
    ) -> RedisResult<Self> {
        Ok(SentinelClient {
            #[cfg(not(feature = "tls-rustls"))]
            sentinel: Sentinel::build_inner(params)?,
            #[cfg(feature = "tls-rustls")]
            sentinel: Sentinel::build_inner(params, certs)?,
            service_name,
            node_connection_info: node_connection_info.unwrap_or_default(),
            server_type,
        })
    }

    fn get_client(&mut self) -> RedisResult<Client> {
        match self.server_type {
            SentinelServerType::Master => self
                .sentinel
                .master_for(self.service_name.as_str(), Some(&self.node_connection_info)),
            SentinelServerType::Replica => self
                .sentinel
                .replica_for(self.service_name.as_str(), Some(&self.node_connection_info)),
        }
    }

    /// Creates a new connection to the desired type of server (based on the
    /// service/master name, and the server type). We use a Sentinel to create a client
    /// for the target type of server, and then create a connection using that client.
    pub fn get_connection(&mut self) -> RedisResult<Connection> {
        let client = self.get_client()?;
        client.get_connection()
    }
}

/// To enable async support you need to chose one of the supported runtimes and active its
/// corresponding feature: `tokio-comp` or `async-std-comp`
#[cfg(feature = "aio")]
#[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
impl SentinelClient {
    async fn async_get_client(&mut self) -> RedisResult<Client> {
        match self.server_type {
            SentinelServerType::Master => {
                self.sentinel
                    .async_master_for(self.service_name.as_str(), Some(&self.node_connection_info))
                    .await
            }
            SentinelServerType::Replica => {
                self.sentinel
                    .async_replica_for(self.service_name.as_str(), Some(&self.node_connection_info))
                    .await
            }
        }
    }

    /// Returns an async connection from the client, using the same logic from
    /// `SentinelClient::get_connection`.
    #[cfg(feature = "aio")]
    pub async fn get_async_connection(&mut self) -> RedisResult<AsyncConnection> {
        self.get_async_connection_with_config(&AsyncConnectionConfig::new())
            .await
    }

    /// Returns an async connection from the client with options, using the same logic from
    /// `SentinelClient::get_connection`.
    #[cfg(feature = "aio")]
    pub async fn get_async_connection_with_config(
        &mut self,
        config: &AsyncConnectionConfig,
    ) -> RedisResult<AsyncConnection> {
        self.async_get_client()
            .await?
            .get_multiplexed_async_connection_with_config(config)
            .await
    }
}

struct BuilderConnectionParams {
    tls_mode: Option<TlsMode>,
    db: Option<i64>,
    username: Option<String>,
    password: Option<String>,
    protocol: Option<ProtocolVersion>,
    #[cfg(feature = "tls-rustls")]
    certificates: Option<TlsCertificates>,
}

/// Used to configure and build a [`SentinelClient`].
/// There are two connections that can be configured independently
/// 1. The connection towards the redis nodes (configured via `set_client_to_redis_..` functions)
/// 2. The connection towards the sentinel nodes (configure via `set_client_to_sentinel_..` functions)
pub struct SentinelClientBuilder {
    sentinels: Vec<ConnectionAddr>,
    service_name: String,
    server_type: SentinelServerType,
    client_to_redis_params: BuilderConnectionParams,
    client_to_sentinel_params: BuilderConnectionParams,
}

impl SentinelClientBuilder {
    /// Creates a new `SentinelClientBuilder`
    /// - `sentinels` - Addresses of sentinel nodes
    /// - `service_name` - The name of the service to be queried via the sentinels
    /// - `server_type` - The server type to be queried via the sentinels
    pub fn new<T: IntoIterator<Item = ConnectionAddr>>(
        sentinels: T,
        service_name: String,
        server_type: SentinelServerType,
    ) -> RedisResult<SentinelClientBuilder> {
        Ok(SentinelClientBuilder {
            sentinels: sentinels.into_iter().collect::<Vec<_>>(),
            service_name,
            server_type,
            client_to_redis_params: BuilderConnectionParams {
                tls_mode: None,
                db: None,
                username: None,
                password: None,
                protocol: None,
                #[cfg(feature = "tls-rustls")]
                certificates: None,
            },
            client_to_sentinel_params: BuilderConnectionParams {
                tls_mode: None,
                db: None,
                username: None,
                password: None,
                protocol: None,
                #[cfg(feature = "tls-rustls")]
                certificates: None,
            },
        })
    }

    /// Creates a new `SentinelClient` from the parameters
    pub fn build(mut self) -> RedisResult<SentinelClient> {
        let mut client_to_redis_connection_info = RedisConnectionInfo::default();

        if let Some(db) = self.client_to_redis_params.db {
            client_to_redis_connection_info.db = db;
        }

        if let Some(username) = self.client_to_redis_params.username {
            client_to_redis_connection_info.username = Some(username);
        }

        if let Some(password) = self.client_to_redis_params.password {
            client_to_redis_connection_info.password = Some(password);
        }

        if let Some(protocol) = self.client_to_redis_params.protocol {
            client_to_redis_connection_info.protocol = protocol;
        }

        let client_to_redis_connection_info = SentinelNodeConnectionInfo {
            tls_mode: self.client_to_redis_params.tls_mode,
            redis_connection_info: Some(client_to_redis_connection_info),
        };

        for sentinel in &mut self.sentinels {
            match sentinel {
                ConnectionAddr::Tcp(_, _) => {
                    if self.client_to_sentinel_params.tls_mode.is_some() {
                        return Err(RedisError::from((
                            ErrorKind::InvalidClientConfig,
                            "Tls mode cannot be set for Tcp connection.",
                        )));
                    }
                    #[cfg(feature = "tls-rustls")]
                    if self.client_to_sentinel_params.certificates.is_some() {
                        return Err(RedisError::from((
                            ErrorKind::InvalidClientConfig,
                            "Certificates cannot be set for Tcp connection.",
                        )));
                    }
                }
                ConnectionAddr::TcpTls {
                    host: _,
                    port: _,
                    ref mut insecure,
                    #[cfg(feature = "tls-rustls")]
                    ref mut tls_params,
                    #[cfg(not(feature = "tls-rustls"))]
                        tls_params: _,
                } => {
                    if let Some(tls_mode) = self.client_to_sentinel_params.tls_mode {
                        match tls_mode {
                            TlsMode::Secure => {
                                *insecure = false;
                            }
                            TlsMode::Insecure => {
                                *insecure = true;
                            }
                        }
                    }
                    #[cfg(feature = "tls-rustls")]
                    if let Some(certs) = &self.client_to_sentinel_params.certificates {
                        let new_tls_params = retrieve_tls_certificates(certs)?;
                        *tls_params = Some(new_tls_params);
                    }
                }
                ConnectionAddr::Unix(_) => {
                    if self.client_to_sentinel_params.tls_mode.is_some() {
                        return Err(RedisError::from((
                            ErrorKind::InvalidClientConfig,
                            "Tls mode cannot be set for unix sockets.",
                        )));
                    }
                    #[cfg(feature = "tls-rustls")]
                    if self.client_to_sentinel_params.certificates.is_some() {
                        return Err(RedisError::from((
                            ErrorKind::InvalidClientConfig,
                            "Certificates cannot be set for unix sockets.",
                        )));
                    }
                }
            }
        }

        let mut client_to_sentinel_redis_connection_info = RedisConnectionInfo::default();

        if let Some(db) = self.client_to_sentinel_params.db {
            client_to_sentinel_redis_connection_info.db = db;
        }

        if let Some(username) = self.client_to_sentinel_params.username.as_ref() {
            client_to_sentinel_redis_connection_info.username = Some(username.clone());
        }

        if let Some(password) = self.client_to_sentinel_params.password.as_ref() {
            client_to_sentinel_redis_connection_info.password = Some(password.clone());
        }

        if let Some(protocol) = self.client_to_sentinel_params.protocol {
            client_to_sentinel_redis_connection_info.protocol = protocol;
        }

        let sentinels = self
            .sentinels
            .into_iter()
            .map(|connection_addr| ConnectionInfo {
                addr: connection_addr,
                redis: client_to_sentinel_redis_connection_info.clone(),
            })
            .collect();

        SentinelClient::build_inner(
            sentinels,
            self.service_name,
            Some(client_to_redis_connection_info),
            self.server_type,
            #[cfg(feature = "tls-rustls")]
            self.client_to_redis_params.certificates,
        )
    }

    /// Set tls mode for the connection to redis
    pub fn set_client_to_redis_tls_mode(mut self, tls_mode: TlsMode) -> SentinelClientBuilder {
        self.client_to_redis_params.tls_mode = Some(tls_mode);
        self
    }

    /// Set db for the connection to redis
    pub fn set_client_to_redis_db(mut self, db: i64) -> SentinelClientBuilder {
        self.client_to_redis_params.db = Some(db);
        self
    }

    /// Set username for the connection to redis
    pub fn set_client_to_redis_username(mut self, username: String) -> SentinelClientBuilder {
        self.client_to_redis_params.username = Some(username);
        self
    }

    /// Set password for the connection to redis
    pub fn set_client_to_redis_password(mut self, password: String) -> SentinelClientBuilder {
        self.client_to_redis_params.password = Some(password);
        self
    }

    /// Set protocol for the connection to redis
    pub fn set_client_to_redis_protocol(
        mut self,
        protocol: ProtocolVersion,
    ) -> SentinelClientBuilder {
        self.client_to_redis_params.protocol = Some(protocol);
        self
    }

    #[cfg(feature = "tls-rustls")]
    /// Set certificates for the connection to redis
    pub fn set_client_to_redis_certificates(
        mut self,
        certificates: TlsCertificates,
    ) -> SentinelClientBuilder {
        self.client_to_redis_params.certificates = Some(certificates);
        self
    }

    /// Set tls mode for the connection to the sentinels
    pub fn set_client_to_sentinel_tls_mode(mut self, tls_mode: TlsMode) -> SentinelClientBuilder {
        self.client_to_sentinel_params.tls_mode = Some(tls_mode);
        self
    }

    /// Set username for the connection to the sentinels
    pub fn set_client_to_sentinel_username(mut self, username: String) -> SentinelClientBuilder {
        self.client_to_sentinel_params.username = Some(username);
        self
    }

    /// Set password for the connection to the sentinels
    pub fn set_client_to_sentinel_password(mut self, password: String) -> SentinelClientBuilder {
        self.client_to_sentinel_params.password = Some(password);
        self
    }

    /// Set protocol for the connection to the sentinels
    pub fn set_client_to_sentinel_protocol(
        mut self,
        protocol: ProtocolVersion,
    ) -> SentinelClientBuilder {
        self.client_to_sentinel_params.protocol = Some(protocol);
        self
    }

    #[cfg(feature = "tls-rustls")]
    /// Set certificate for the connection to the sentinels
    pub fn set_client_to_sentinel_certificates(
        mut self,
        certificates: TlsCertificates,
    ) -> SentinelClientBuilder {
        self.client_to_sentinel_params.certificates = Some(certificates);
        self
    }
}