1use google_cloud_gax::error::Error as GaxError;
18use std::time::Duration;
19
20pub(crate) const MAX_SUPPORTED_CHANNELS: usize = 256;
22
23#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
25#[non_exhaustive]
26pub(crate) enum ChannelSelectionStrategy {
27 #[default]
29 PowerOfTwoLeastBusy,
30}
31
32#[derive(Clone, Debug, PartialEq)]
54#[non_exhaustive]
55pub enum ChannelPoolConfig {
56 Static(StaticChannelPoolConfig),
58 Dynamic(DynamicChannelPoolConfig),
60}
61
62impl Default for ChannelPoolConfig {
63 fn default() -> Self {
64 Self::Dynamic(DynamicChannelPoolConfig::default())
65 }
66}
67
68impl ChannelPoolConfig {
69 pub(crate) fn validate(&self) -> Result<(), GaxError> {
71 match self {
72 Self::Static(config) => config.validate(),
73 Self::Dynamic(config) => config.validate(),
74 }
75 }
76
77 pub fn dynamic_config(&self) -> Option<&DynamicChannelPoolConfig> {
79 match self {
80 Self::Dynamic(config) => Some(config),
81 Self::Static(_) => None,
82 }
83 }
84
85 pub fn static_config(&self) -> Option<&StaticChannelPoolConfig> {
87 match self {
88 Self::Static(config) => Some(config),
89 Self::Dynamic(_) => None,
90 }
91 }
92}
93
94#[derive(Clone, Debug, PartialEq, Eq)]
109#[non_exhaustive]
110pub struct StaticChannelPoolConfig {
111 pub(crate) num_channels: usize,
113}
114
115impl Default for StaticChannelPoolConfig {
116 fn default() -> Self {
117 Self { num_channels: 4 }
118 }
119}
120
121impl StaticChannelPoolConfig {
122 pub fn new(num_channels: usize) -> Self {
136 Self { num_channels }
137 }
138
139 pub fn with_num_channels(mut self, num_channels: usize) -> Self {
149 self.num_channels = num_channels;
150 self
151 }
152
153 pub fn num_channels(&self) -> usize {
155 self.num_channels
156 }
157
158 pub(crate) fn validate(&self) -> Result<(), GaxError> {
160 if self.num_channels == 0 {
161 return Err(GaxError::binding("num_channels must be at least 1"));
162 }
163 if self.num_channels > MAX_SUPPORTED_CHANNELS {
164 return Err(GaxError::binding(format!(
165 "num_channels cannot exceed maximum supported limit of {MAX_SUPPORTED_CHANNELS}"
166 )));
167 }
168 Ok(())
169 }
170}
171
172impl From<StaticChannelPoolConfig> for ChannelPoolConfig {
173 fn from(config: StaticChannelPoolConfig) -> Self {
174 Self::Static(config)
175 }
176}
177
178#[derive(Clone, Debug, PartialEq)]
199#[non_exhaustive]
200pub struct DynamicChannelPoolConfig {
201 pub(crate) initial_channels: usize,
203 pub(crate) min_channels: usize,
205 pub(crate) max_channels: usize,
207 pub(crate) min_rpc_per_channel: f64,
209 pub(crate) max_rpc_per_channel: f64,
211 pub(crate) error_penalty_step: u32,
213 pub(crate) error_penalty_duration: Duration,
215 pub(crate) scale_down_check_interval: Duration,
217 pub(crate) scale_up_cooldown: Duration,
219 pub(crate) consecutive_low_load_checks: usize,
221 pub(crate) max_scale_up_percent: u32,
223 pub(crate) max_remove_channels: usize,
225 pub(crate) drain_idle_grace: Duration,
227 pub(crate) prime_timeout: Duration,
229 pub(crate) prime_max_attempts: usize,
231 pub(crate) selection_strategy: ChannelSelectionStrategy,
233}
234
235impl Default for DynamicChannelPoolConfig {
236 fn default() -> Self {
237 Self {
238 initial_channels: 4,
239 min_channels: 4,
240 max_channels: 256,
241 min_rpc_per_channel: 2.0,
242 max_rpc_per_channel: 8.0,
243 error_penalty_step: 2,
244 error_penalty_duration: Duration::from_secs(5),
245 scale_down_check_interval: Duration::from_secs(60),
246 scale_up_cooldown: Duration::from_secs(1),
247 consecutive_low_load_checks: 3,
248 max_scale_up_percent: 100,
249 max_remove_channels: 4,
250 drain_idle_grace: Duration::from_secs(60),
251 prime_timeout: Duration::from_secs(5),
252 prime_max_attempts: 3,
253 selection_strategy: ChannelSelectionStrategy::PowerOfTwoLeastBusy,
254 }
255 }
256}
257
258impl DynamicChannelPoolConfig {
259 pub fn new() -> Self {
273 Self::default()
274 }
275
276 pub fn with_initial_channels(mut self, channels: usize) -> Self {
280 self.initial_channels = channels;
281 self
282 }
283
284 pub fn with_min_channels(mut self, channels: usize) -> Self {
288 self.min_channels = channels;
289 self
290 }
291
292 pub fn with_max_channels(mut self, channels: usize) -> Self {
296 self.max_channels = channels;
297 self
298 }
299
300 pub fn with_min_rpc_per_channel(mut self, min_rpc: f64) -> Self {
306 self.min_rpc_per_channel = min_rpc;
307 self
308 }
309
310 pub fn with_max_rpc_per_channel(mut self, max_rpc: f64) -> Self {
316 self.max_rpc_per_channel = max_rpc;
317 self
318 }
319
320 pub fn with_error_penalty_step(mut self, step: u32) -> Self {
325 self.error_penalty_step = step;
326 self
327 }
328
329 pub fn with_error_penalty_duration(mut self, duration: Duration) -> Self {
334 self.error_penalty_duration = duration;
335 self
336 }
337
338 pub fn with_scale_down_check_interval(mut self, interval: Duration) -> Self {
343 self.scale_down_check_interval = interval;
344 self
345 }
346
347 pub fn with_scale_up_cooldown(mut self, cooldown: Duration) -> Self {
352 self.scale_up_cooldown = cooldown;
353 self
354 }
355
356 pub fn with_consecutive_low_load_checks(mut self, checks: usize) -> Self {
361 self.consecutive_low_load_checks = checks;
362 self
363 }
364
365 pub fn with_max_scale_up_percent(mut self, percent: u32) -> Self {
370 self.max_scale_up_percent = percent;
371 self
372 }
373
374 pub fn with_max_remove_channels(mut self, max_channels: usize) -> Self {
379 self.max_remove_channels = max_channels;
380 self
381 }
382
383 pub fn with_drain_idle_grace(mut self, grace: Duration) -> Self {
388 self.drain_idle_grace = grace;
389 self
390 }
391
392 pub fn with_prime_timeout(mut self, timeout: Duration) -> Self {
398 self.prime_timeout = timeout;
399 self
400 }
401
402 pub fn with_prime_max_attempts(mut self, attempts: usize) -> Self {
407 self.prime_max_attempts = attempts;
408 self
409 }
410
411 #[cfg(test)]
413 pub(crate) fn with_selection_strategy(mut self, strategy: ChannelSelectionStrategy) -> Self {
414 self.selection_strategy = strategy;
415 self
416 }
417
418 pub fn initial_channels(&self) -> usize {
420 self.initial_channels
421 }
422
423 pub fn min_channels(&self) -> usize {
425 self.min_channels
426 }
427
428 pub fn max_channels(&self) -> usize {
430 self.max_channels
431 }
432
433 pub fn min_rpc_per_channel(&self) -> f64 {
435 self.min_rpc_per_channel
436 }
437
438 pub fn max_rpc_per_channel(&self) -> f64 {
440 self.max_rpc_per_channel
441 }
442
443 pub fn error_penalty_step(&self) -> u32 {
445 self.error_penalty_step
446 }
447
448 pub fn error_penalty_duration(&self) -> Duration {
450 self.error_penalty_duration
451 }
452
453 pub fn scale_down_check_interval(&self) -> Duration {
455 self.scale_down_check_interval
456 }
457
458 pub fn scale_up_cooldown(&self) -> Duration {
460 self.scale_up_cooldown
461 }
462
463 pub fn consecutive_low_load_checks(&self) -> usize {
465 self.consecutive_low_load_checks
466 }
467
468 pub fn max_scale_up_percent(&self) -> u32 {
470 self.max_scale_up_percent
471 }
472
473 pub fn max_remove_channels(&self) -> usize {
475 self.max_remove_channels
476 }
477
478 pub fn drain_idle_grace(&self) -> Duration {
480 self.drain_idle_grace
481 }
482
483 pub fn prime_timeout(&self) -> Duration {
485 self.prime_timeout
486 }
487
488 pub fn prime_max_attempts(&self) -> usize {
490 self.prime_max_attempts
491 }
492
493 #[cfg(test)]
495 pub(crate) fn selection_strategy(&self) -> ChannelSelectionStrategy {
496 self.selection_strategy
497 }
498
499 pub(crate) fn validate(&self) -> Result<(), GaxError> {
501 if self.min_channels == 0 {
502 return Err(GaxError::binding("min_channels must be at least 1"));
503 }
504 if self.max_channels < self.min_channels {
505 return Err(GaxError::binding(
506 "max_channels must be greater than or equal to min_channels",
507 ));
508 }
509 if self.max_channels > MAX_SUPPORTED_CHANNELS {
510 return Err(GaxError::binding(format!(
511 "max_channels cannot exceed maximum supported limit of {MAX_SUPPORTED_CHANNELS}"
512 )));
513 }
514 if self.initial_channels < self.min_channels || self.initial_channels > self.max_channels {
515 return Err(GaxError::binding(
516 "initial_channels must be between min_channels and max_channels",
517 ));
518 }
519 if !self.min_rpc_per_channel.is_finite() || self.min_rpc_per_channel <= 0.0 {
520 return Err(GaxError::binding(
521 "min_rpc_per_channel must be a finite value greater than 0.0",
522 ));
523 }
524 if !self.max_rpc_per_channel.is_finite()
525 || self.max_rpc_per_channel <= self.min_rpc_per_channel
526 {
527 return Err(GaxError::binding(
528 "max_rpc_per_channel must be a finite value strictly greater than min_rpc_per_channel",
529 ));
530 }
531 if self.max_scale_up_percent == 0 || self.max_scale_up_percent > 100 {
532 return Err(GaxError::binding(
533 "max_scale_up_percent must be between 1 and 100",
534 ));
535 }
536 if self.consecutive_low_load_checks == 0 {
537 return Err(GaxError::binding(
538 "consecutive_low_load_checks must be at least 1",
539 ));
540 }
541 if self.max_remove_channels == 0 {
542 return Err(GaxError::binding("max_remove_channels must be at least 1"));
543 }
544 if self.scale_down_check_interval.is_zero() {
545 return Err(GaxError::binding(
546 "scale_down_check_interval must be greater than zero",
547 ));
548 }
549 if self.prime_timeout.is_zero() {
550 return Err(GaxError::binding("prime_timeout must be greater than zero"));
551 }
552 if self.prime_max_attempts == 0 {
553 return Err(GaxError::binding("prime_max_attempts must be at least 1"));
554 }
555 if self.error_penalty_step > self.error_penalty_max() {
556 return Err(GaxError::binding(
557 "error_penalty_step cannot exceed ceil(max_rpc_per_channel)",
558 ));
559 }
560 Ok(())
561 }
562
563 pub(crate) fn error_penalty_max(&self) -> u32 {
566 self.max_rpc_per_channel.ceil() as u32
567 }
568
569 pub(crate) fn target_rpc_per_channel(&self) -> u32 {
571 let midpoint = ((self.min_rpc_per_channel + self.max_rpc_per_channel) / 2.0).floor() as u32;
572 midpoint.max(1)
573 }
574
575 pub(crate) fn desired_channel_count(&self, total_load: u32) -> usize {
577 let target_rpc = self.target_rpc_per_channel() as f64;
578 ((total_load as f64) / target_rpc).ceil() as usize
579 }
580}
581
582impl From<DynamicChannelPoolConfig> for ChannelPoolConfig {
583 fn from(config: DynamicChannelPoolConfig) -> Self {
584 Self::Dynamic(config)
585 }
586}
587
588#[cfg(test)]
589mod tests {
590 use super::*;
591 use std::fmt::Debug;
592
593 #[test]
594 fn traits() {
595 static_assertions::assert_impl_all!(
596 ChannelPoolConfig: Clone,
597 Debug,
598 PartialEq,
599 Send,
600 Sync
601 );
602 static_assertions::assert_impl_all!(
603 StaticChannelPoolConfig: Clone,
604 Debug,
605 PartialEq,
606 Eq,
607 Send,
608 Sync
609 );
610 static_assertions::assert_impl_all!(
611 DynamicChannelPoolConfig: Clone,
612 Debug,
613 PartialEq,
614 Send,
615 Sync
616 );
617 static_assertions::assert_impl_all!(
618 ChannelSelectionStrategy: Clone,
619 Copy,
620 Debug,
621 PartialEq,
622 Eq,
623 Send,
624 Sync
625 );
626 }
627
628 #[test]
629 fn config_defaults_and_precedence() {
630 let static_config = StaticChannelPoolConfig::default();
631 assert_eq!(
632 static_config.num_channels, 4,
633 "StaticChannelPoolConfig default num_channels must be 4"
634 );
635 assert!(
636 static_config.validate().is_ok(),
637 "Default StaticChannelPoolConfig must pass validation"
638 );
639
640 let dynamic_config = DynamicChannelPoolConfig::default();
641 assert_eq!(
642 dynamic_config.initial_channels, 4,
643 "DynamicChannelPoolConfig default initial_channels must be 4"
644 );
645 assert_eq!(
646 dynamic_config.min_channels, 4,
647 "DynamicChannelPoolConfig default min_channels must be 4"
648 );
649 assert_eq!(
650 dynamic_config.max_channels, 256,
651 "DynamicChannelPoolConfig default max_channels must be 256"
652 );
653 assert_eq!(
654 dynamic_config.min_rpc_per_channel, 2.0,
655 "DynamicChannelPoolConfig default min_rpc_per_channel must be 2.0"
656 );
657 assert_eq!(
658 dynamic_config.max_rpc_per_channel, 8.0,
659 "DynamicChannelPoolConfig default max_rpc_per_channel must be 8.0"
660 );
661 assert_eq!(
662 dynamic_config.error_penalty_step, 2,
663 "DynamicChannelPoolConfig default error_penalty_step must be 2"
664 );
665 assert_eq!(
666 dynamic_config.error_penalty_duration,
667 Duration::from_secs(5),
668 "DynamicChannelPoolConfig default error_penalty_duration must be 5s"
669 );
670 assert_eq!(
671 dynamic_config.error_penalty_max(),
672 8,
673 "DynamicChannelPoolConfig default error_penalty_max must be 8"
674 );
675 assert_eq!(
676 dynamic_config.scale_down_check_interval,
677 Duration::from_secs(60),
678 "DynamicChannelPoolConfig default scale_down_check_interval must be 60s"
679 );
680 assert_eq!(
681 dynamic_config.scale_up_cooldown,
682 Duration::from_secs(1),
683 "DynamicChannelPoolConfig default scale_up_cooldown must be 1s"
684 );
685 assert_eq!(
686 dynamic_config.consecutive_low_load_checks, 3,
687 "DynamicChannelPoolConfig default consecutive_low_load_checks must be 3"
688 );
689 assert_eq!(
690 dynamic_config.max_scale_up_percent, 100,
691 "DynamicChannelPoolConfig default max_scale_up_percent must be 100"
692 );
693 assert_eq!(
694 dynamic_config.max_remove_channels, 4,
695 "DynamicChannelPoolConfig default max_remove_channels must be 4"
696 );
697 assert_eq!(
698 dynamic_config.drain_idle_grace,
699 Duration::from_secs(60),
700 "DynamicChannelPoolConfig default drain_idle_grace must be 60s"
701 );
702 assert_eq!(
703 dynamic_config.prime_timeout,
704 Duration::from_secs(5),
705 "DynamicChannelPoolConfig default prime_timeout must be 5s"
706 );
707 assert_eq!(
708 dynamic_config.prime_max_attempts, 3,
709 "DynamicChannelPoolConfig default prime_max_attempts must be 3"
710 );
711 assert_eq!(
712 dynamic_config.selection_strategy,
713 ChannelSelectionStrategy::PowerOfTwoLeastBusy,
714 "DynamicChannelPoolConfig default selection_strategy must be PowerOfTwoLeastBusy"
715 );
716 assert!(
717 dynamic_config.validate().is_ok(),
718 "Default DynamicChannelPoolConfig must pass validation"
719 );
720
721 let default_pool_config = ChannelPoolConfig::default();
722 assert_eq!(
723 default_pool_config,
724 ChannelPoolConfig::Dynamic(DynamicChannelPoolConfig::default()),
725 "Default ChannelPoolConfig must be Dynamic"
726 );
727
728 let from_static: ChannelPoolConfig = static_config.into();
729 assert_eq!(
730 from_static,
731 ChannelPoolConfig::Static(StaticChannelPoolConfig::default()),
732 "Conversion from StaticChannelPoolConfig must produce Static variant"
733 );
734
735 let from_dynamic: ChannelPoolConfig = dynamic_config.into();
736 assert_eq!(
737 from_dynamic,
738 ChannelPoolConfig::Dynamic(DynamicChannelPoolConfig::default()),
739 "Conversion from DynamicChannelPoolConfig must produce Dynamic variant"
740 );
741 }
742
743 #[test]
744 fn config_validation_errors() {
745 let invalid_static = StaticChannelPoolConfig { num_channels: 0 };
746 assert!(
747 invalid_static.validate().is_err(),
748 "StaticChannelPoolConfig with 0 channels must fail validation"
749 );
750
751 let invalid_max = DynamicChannelPoolConfig {
752 min_channels: 10,
753 max_channels: 5,
754 ..Default::default()
755 };
756 assert!(
757 invalid_max.validate().is_err(),
758 "DynamicChannelPoolConfig with max_channels < min_channels must fail validation"
759 );
760
761 let invalid_initial = DynamicChannelPoolConfig {
762 initial_channels: 2,
763 min_channels: 4,
764 max_channels: 10,
765 ..Default::default()
766 };
767 assert!(
768 invalid_initial.validate().is_err(),
769 "DynamicChannelPoolConfig with initial_channels < min_channels must fail validation"
770 );
771
772 let invalid_rpc = DynamicChannelPoolConfig {
773 min_rpc_per_channel: 25.0,
774 max_rpc_per_channel: 20.0,
775 ..Default::default()
776 };
777 assert!(
778 invalid_rpc.validate().is_err(),
779 "DynamicChannelPoolConfig with max_rpc <= min_rpc must fail validation"
780 );
781
782 let nan_min_rpc = DynamicChannelPoolConfig {
783 min_rpc_per_channel: f64::NAN,
784 ..Default::default()
785 };
786 assert!(
787 nan_min_rpc.validate().is_err(),
788 "DynamicChannelPoolConfig with NaN min_rpc must fail validation"
789 );
790
791 let nan_max_rpc = DynamicChannelPoolConfig {
792 max_rpc_per_channel: f64::NAN,
793 ..Default::default()
794 };
795 assert!(
796 nan_max_rpc.validate().is_err(),
797 "DynamicChannelPoolConfig with NaN max_rpc must fail validation"
798 );
799
800 let invalid_interval = DynamicChannelPoolConfig {
801 scale_down_check_interval: Duration::ZERO,
802 ..Default::default()
803 };
804 assert!(
805 invalid_interval.validate().is_err(),
806 "DynamicChannelPoolConfig with zero scale_down_check_interval must fail validation"
807 );
808
809 let invalid_timeout = DynamicChannelPoolConfig {
810 prime_timeout: Duration::ZERO,
811 ..Default::default()
812 };
813 assert!(
814 invalid_timeout.validate().is_err(),
815 "DynamicChannelPoolConfig with zero prime_timeout must fail validation"
816 );
817
818 let invalid_percent = DynamicChannelPoolConfig {
819 max_scale_up_percent: 150,
820 ..Default::default()
821 };
822 assert!(
823 invalid_percent.validate().is_err(),
824 "DynamicChannelPoolConfig with max_scale_up_percent > 100 must fail validation"
825 );
826
827 let zero_min_channels = DynamicChannelPoolConfig {
828 min_channels: 0,
829 ..Default::default()
830 };
831 assert!(
832 zero_min_channels.validate().is_err(),
833 "min_channels = 0 must fail validation"
834 );
835
836 let exceeds_max_channels = DynamicChannelPoolConfig {
837 min_channels: 1,
838 max_channels: MAX_SUPPORTED_CHANNELS + 1,
839 initial_channels: 1,
840 ..Default::default()
841 };
842 assert!(
843 exceeds_max_channels.validate().is_err(),
844 "max_channels > 256 must fail validation"
845 );
846
847 let initial_exceeds_max = DynamicChannelPoolConfig {
848 min_channels: 2,
849 initial_channels: 12,
850 max_channels: 10,
851 ..Default::default()
852 };
853 assert!(
854 initial_exceeds_max.validate().is_err(),
855 "initial_channels > max_channels must fail validation"
856 );
857
858 let zero_min_rpc = DynamicChannelPoolConfig {
859 min_rpc_per_channel: 0.0,
860 ..Default::default()
861 };
862 assert!(
863 zero_min_rpc.validate().is_err(),
864 "min_rpc_per_channel <= 0.0 must fail validation"
865 );
866
867 let negative_min_rpc = DynamicChannelPoolConfig {
868 min_rpc_per_channel: -5.0,
869 ..Default::default()
870 };
871 assert!(
872 negative_min_rpc.validate().is_err(),
873 "min_rpc_per_channel < 0.0 must fail validation"
874 );
875
876 let nan_min_rpc = DynamicChannelPoolConfig {
877 min_rpc_per_channel: f64::NAN,
878 ..Default::default()
879 };
880 assert!(
881 nan_min_rpc.validate().is_err(),
882 "min_rpc_per_channel = NaN must fail validation"
883 );
884
885 let inf_min_rpc = DynamicChannelPoolConfig {
886 min_rpc_per_channel: f64::INFINITY,
887 max_rpc_per_channel: f64::INFINITY,
888 ..Default::default()
889 };
890 assert!(
891 inf_min_rpc.validate().is_err(),
892 "min_rpc_per_channel = INFINITY must fail validation"
893 );
894
895 let nan_max_rpc = DynamicChannelPoolConfig {
896 max_rpc_per_channel: f64::NAN,
897 ..Default::default()
898 };
899 assert!(
900 nan_max_rpc.validate().is_err(),
901 "max_rpc_per_channel = NaN must fail validation"
902 );
903
904 let inf_max_rpc = DynamicChannelPoolConfig {
905 max_rpc_per_channel: f64::INFINITY,
906 ..Default::default()
907 };
908 assert!(
909 inf_max_rpc.validate().is_err(),
910 "max_rpc_per_channel = INFINITY must fail validation"
911 );
912
913 let zero_scale_up_percent = DynamicChannelPoolConfig {
914 max_scale_up_percent: 0,
915 ..Default::default()
916 };
917 assert!(
918 zero_scale_up_percent.validate().is_err(),
919 "max_scale_up_percent == 0 must fail validation"
920 );
921
922 let zero_consecutive_checks = DynamicChannelPoolConfig {
923 consecutive_low_load_checks: 0,
924 ..Default::default()
925 };
926 assert!(
927 zero_consecutive_checks.validate().is_err(),
928 "consecutive_low_load_checks == 0 must fail validation"
929 );
930
931 let zero_max_remove = DynamicChannelPoolConfig {
932 max_remove_channels: 0,
933 ..Default::default()
934 };
935 assert!(
936 zero_max_remove.validate().is_err(),
937 "max_remove_channels == 0 must fail validation"
938 );
939
940 let zero_prime_attempts = DynamicChannelPoolConfig {
941 prime_max_attempts: 0,
942 ..Default::default()
943 };
944 assert!(
945 zero_prime_attempts.validate().is_err(),
946 "prime_max_attempts == 0 must fail validation"
947 );
948
949 let invalid_error_penalty_step = DynamicChannelPoolConfig {
950 error_penalty_step: 30,
951 max_rpc_per_channel: 25.0,
952 ..Default::default()
953 };
954 assert!(
955 invalid_error_penalty_step.validate().is_err(),
956 "error_penalty_step > ceil(max_rpc_per_channel) must fail validation"
957 );
958
959 let static_exceeds_max = StaticChannelPoolConfig {
960 num_channels: MAX_SUPPORTED_CHANNELS + 1,
961 };
962 assert!(
963 static_exceeds_max.validate().is_err(),
964 "StaticChannelPoolConfig with num_channels > MAX_SUPPORTED_CHANNELS must fail validation"
965 );
966 }
967
968 #[test]
969 fn channel_pool_config_enum_methods() {
970 let static_config = StaticChannelPoolConfig { num_channels: 6 };
971 let pool_static = ChannelPoolConfig::Static(static_config.clone());
972 assert!(
973 pool_static.validate().is_ok(),
974 "Valid static pool config must pass validate()"
975 );
976 assert_eq!(
977 pool_static.static_config(),
978 Some(&static_config),
979 "static_config() must return Some for Static variant"
980 );
981 assert_eq!(
982 pool_static.dynamic_config(),
983 None,
984 "dynamic_config() must return None for Static variant"
985 );
986
987 let dynamic_config = DynamicChannelPoolConfig::default();
988 let pool_dynamic = ChannelPoolConfig::Dynamic(dynamic_config.clone());
989 assert!(
990 pool_dynamic.validate().is_ok(),
991 "Valid dynamic pool config must pass validate()"
992 );
993 assert_eq!(
994 pool_dynamic.dynamic_config(),
995 Some(&dynamic_config),
996 "dynamic_config() must return Some for Dynamic variant"
997 );
998 assert_eq!(
999 pool_dynamic.static_config(),
1000 None,
1001 "static_config() must return None for Dynamic variant"
1002 );
1003
1004 let invalid_pool_static =
1005 ChannelPoolConfig::Static(StaticChannelPoolConfig { num_channels: 0 });
1006 assert!(
1007 invalid_pool_static.validate().is_err(),
1008 "Invalid static pool enum config must fail validate()"
1009 );
1010
1011 let invalid_pool_dynamic = ChannelPoolConfig::Dynamic(DynamicChannelPoolConfig {
1012 min_channels: 0,
1013 ..Default::default()
1014 });
1015 assert!(
1016 invalid_pool_dynamic.validate().is_err(),
1017 "Invalid dynamic pool enum config must fail validate()"
1018 );
1019 }
1020
1021 #[test]
1022 fn calculations_target_rpc_and_desired_channel_count() {
1023 let config = DynamicChannelPoolConfig {
1024 min_rpc_per_channel: 15.0,
1025 max_rpc_per_channel: 25.0,
1026 ..Default::default()
1027 };
1028
1029 assert_eq!(
1031 config.target_rpc_per_channel(),
1032 20,
1033 "Target RPC per channel must be midpoint 20"
1034 );
1035
1036 assert_eq!(
1037 config.desired_channel_count(0),
1038 0,
1039 "0 load requires 0 desired channels"
1040 );
1041 assert_eq!(
1042 config.desired_channel_count(20),
1043 1,
1044 "20 load with target 20 requires 1 channel"
1045 );
1046 assert_eq!(
1047 config.desired_channel_count(21),
1048 2,
1049 "21 load with target 20 ceil requires 2 channels"
1050 );
1051 assert_eq!(
1052 config.desired_channel_count(40),
1053 2,
1054 "40 load with target 20 requires 2 channels"
1055 );
1056 assert_eq!(
1057 config.desired_channel_count(41),
1058 3,
1059 "41 load with target 20 ceil requires 3 channels"
1060 );
1061
1062 let low_config = DynamicChannelPoolConfig {
1064 min_rpc_per_channel: 0.1,
1065 max_rpc_per_channel: 0.5,
1066 ..Default::default()
1067 };
1068 assert_eq!(
1069 low_config.target_rpc_per_channel(),
1070 1,
1071 "Target RPC per channel must clamp to minimum 1"
1072 );
1073 }
1074
1075 #[test]
1076 fn error_penalty_max_derived_from_max_rpc_per_channel() {
1077 let config = DynamicChannelPoolConfig {
1078 max_rpc_per_channel: 25.0,
1079 ..Default::default()
1080 };
1081 assert_eq!(
1082 config.error_penalty_max(),
1083 25,
1084 "error_penalty_max must equal ceil(25.0)"
1085 );
1086
1087 let config_fractional = DynamicChannelPoolConfig {
1088 max_rpc_per_channel: 25.2,
1089 ..Default::default()
1090 };
1091 assert_eq!(
1092 config_fractional.error_penalty_max(),
1093 26,
1094 "error_penalty_max must equal ceil(25.2) -> 26"
1095 );
1096 }
1097
1098 #[test]
1099 fn static_channel_pool_config_new() {
1100 let config = StaticChannelPoolConfig::new(8);
1101 assert_eq!(config.num_channels, 8, "num_channels must be 8");
1102 assert_eq!(
1103 config.num_channels(),
1104 8,
1105 "num_channels() getter must return 8"
1106 );
1107 assert!(config.validate().is_ok(), "validation must succeed");
1108
1109 let config = StaticChannelPoolConfig::default().with_num_channels(12);
1110 assert_eq!(config.num_channels, 12, "num_channels must be 12");
1111 assert_eq!(
1112 config.num_channels(),
1113 12,
1114 "num_channels() getter must return 12"
1115 );
1116 assert!(config.validate().is_ok(), "validation must succeed");
1117 }
1118
1119 #[test]
1120 fn dynamic_channel_pool_config_builder() {
1121 let config = DynamicChannelPoolConfig::new()
1122 .with_initial_channels(5)
1123 .with_min_channels(3)
1124 .with_max_channels(12)
1125 .with_min_rpc_per_channel(10.0)
1126 .with_max_rpc_per_channel(20.0)
1127 .with_error_penalty_step(8)
1128 .with_error_penalty_duration(Duration::from_secs(15))
1129 .with_scale_down_check_interval(Duration::from_secs(120))
1130 .with_scale_up_cooldown(Duration::from_secs(30))
1131 .with_consecutive_low_load_checks(5)
1132 .with_max_scale_up_percent(50)
1133 .with_max_remove_channels(3)
1134 .with_drain_idle_grace(Duration::from_secs(90))
1135 .with_prime_timeout(Duration::from_secs(20))
1136 .with_prime_max_attempts(5)
1137 .with_selection_strategy(ChannelSelectionStrategy::PowerOfTwoLeastBusy);
1138
1139 assert_eq!(config.initial_channels, 5, "initial_channels must match");
1140 assert_eq!(
1141 config.initial_channels(),
1142 5,
1143 "initial_channels() getter must return 5"
1144 );
1145 assert_eq!(config.min_channels, 3, "min_channels must match");
1146 assert_eq!(
1147 config.min_channels(),
1148 3,
1149 "min_channels() getter must return 3"
1150 );
1151 assert_eq!(config.max_channels, 12, "max_channels must match");
1152 assert_eq!(
1153 config.max_channels(),
1154 12,
1155 "max_channels() getter must return 12"
1156 );
1157 assert_eq!(
1158 config.min_rpc_per_channel, 10.0,
1159 "min_rpc_per_channel must match"
1160 );
1161 assert_eq!(
1162 config.min_rpc_per_channel(),
1163 10.0,
1164 "min_rpc_per_channel() getter must return 10.0"
1165 );
1166 assert_eq!(
1167 config.max_rpc_per_channel, 20.0,
1168 "max_rpc_per_channel must match"
1169 );
1170 assert_eq!(
1171 config.max_rpc_per_channel(),
1172 20.0,
1173 "max_rpc_per_channel() getter must return 20.0"
1174 );
1175 assert_eq!(
1176 config.error_penalty_step, 8,
1177 "error_penalty_step must match"
1178 );
1179 assert_eq!(
1180 config.error_penalty_step(),
1181 8,
1182 "error_penalty_step() getter must return 8"
1183 );
1184 assert_eq!(
1185 config.error_penalty_duration,
1186 Duration::from_secs(15),
1187 "error_penalty_duration must match"
1188 );
1189 assert_eq!(
1190 config.error_penalty_duration(),
1191 Duration::from_secs(15),
1192 "error_penalty_duration() getter must return 15s"
1193 );
1194 assert_eq!(
1195 config.scale_down_check_interval,
1196 Duration::from_secs(120),
1197 "scale_down_check_interval must match"
1198 );
1199 assert_eq!(
1200 config.scale_down_check_interval(),
1201 Duration::from_secs(120),
1202 "scale_down_check_interval() getter must return 120s"
1203 );
1204 assert_eq!(
1205 config.scale_up_cooldown,
1206 Duration::from_secs(30),
1207 "scale_up_cooldown must match"
1208 );
1209 assert_eq!(
1210 config.scale_up_cooldown(),
1211 Duration::from_secs(30),
1212 "scale_up_cooldown() getter must return 30s"
1213 );
1214 assert_eq!(
1215 config.consecutive_low_load_checks, 5,
1216 "consecutive_low_load_checks must match"
1217 );
1218 assert_eq!(
1219 config.consecutive_low_load_checks(),
1220 5,
1221 "consecutive_low_load_checks() getter must return 5"
1222 );
1223 assert_eq!(
1224 config.max_scale_up_percent, 50,
1225 "max_scale_up_percent must match"
1226 );
1227 assert_eq!(
1228 config.max_scale_up_percent(),
1229 50,
1230 "max_scale_up_percent() getter must return 50"
1231 );
1232 assert_eq!(
1233 config.max_remove_channels, 3,
1234 "max_remove_channels must match"
1235 );
1236 assert_eq!(
1237 config.max_remove_channels(),
1238 3,
1239 "max_remove_channels() getter must return 3"
1240 );
1241 assert_eq!(
1242 config.drain_idle_grace,
1243 Duration::from_secs(90),
1244 "drain_idle_grace must match"
1245 );
1246 assert_eq!(
1247 config.drain_idle_grace(),
1248 Duration::from_secs(90),
1249 "drain_idle_grace() getter must return 90s"
1250 );
1251 assert_eq!(
1252 config.prime_timeout,
1253 Duration::from_secs(20),
1254 "prime_timeout must match"
1255 );
1256 assert_eq!(
1257 config.prime_timeout(),
1258 Duration::from_secs(20),
1259 "prime_timeout() getter must return 20s"
1260 );
1261 assert_eq!(
1262 config.prime_max_attempts, 5,
1263 "prime_max_attempts must match"
1264 );
1265 assert_eq!(
1266 config.prime_max_attempts(),
1267 5,
1268 "prime_max_attempts() getter must return 5"
1269 );
1270 assert_eq!(
1271 config.selection_strategy,
1272 ChannelSelectionStrategy::PowerOfTwoLeastBusy,
1273 "selection_strategy must match"
1274 );
1275 assert_eq!(
1276 config.selection_strategy(),
1277 ChannelSelectionStrategy::PowerOfTwoLeastBusy,
1278 "selection_strategy() getter must return PowerOfTwoLeastBusy"
1279 );
1280 assert!(config.validate().is_ok(), "validation must succeed");
1281 }
1282}