@@ -374,6 +374,13 @@ fn parse_url(options: &ClickHouseOptions) -> Result<url::Url> {
374374 Ok ( url)
375375}
376376
377+ pub fn is_cloud_host ( host : & str ) -> bool {
378+ let host = host. to_lowercase ( ) ;
379+ host. ends_with ( ".clickhouse.cloud" )
380+ || host. ends_with ( ".clickhouse-staging.com" )
381+ || host. ends_with ( ".clickhouse-dev.com" )
382+ }
383+
377384fn is_local_address ( host : & str ) -> bool {
378385 let localhost = SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , 0 ) ) ;
379386 let addresses = format ! ( "{}:0" , host) . to_socket_addrs ( ) ;
@@ -557,6 +564,12 @@ fn clickhouse_url_defaults(
557564 panic ! ( "No client config had been read, while --connection was set" ) ;
558565 }
559566
567+ // Cloud hosts always use secure connections unless explicitly disabled
568+ if secure. is_none ( ) && is_cloud_host ( & url. host ( ) . ok_or_else ( || anyhow ! ( "No host" ) ) ?. to_string ( ) )
569+ {
570+ secure = Some ( true ) ;
571+ }
572+
560573 // - 9000 for non secure
561574 // - 9440 for secure
562575 if url. port ( ) . is_none ( ) {
@@ -585,16 +598,23 @@ fn clickhouse_url_defaults(
585598
586599 // some default settings in URL
587600 {
601+ let host_str = url. host ( ) . ok_or_else ( || anyhow ! ( "No host" ) ) ?. to_string ( ) ;
588602 let pairs: HashMap < _ , _ > = url_safe. query_pairs ( ) . into_owned ( ) . collect ( ) ;
589- let is_local = is_local_address ( & url. host ( ) . ok_or_else ( || anyhow ! ( "No host" ) ) ?. to_string ( ) ) ;
603+ let is_local = is_local_address ( & host_str) ;
604+ let is_cloud = is_cloud_host ( & host_str) ;
590605 let mut mut_pairs = url. query_pairs_mut ( ) ;
591606 // Enable compression in non-local network (in the same way as clickhouse does by default)
592607 if !pairs. contains_key ( "compression" ) && !is_local {
593608 mut_pairs. append_pair ( "compression" , "lz4" ) ;
594609 }
595- // default is: 500ms (too small)
596610 if !pairs. contains_key ( "connection_timeout" ) {
597- mut_pairs. append_pair ( "connection_timeout" , "5s" ) ;
611+ if is_cloud {
612+ // Cloud services may need time to wake up from idle state
613+ mut_pairs. append_pair ( "connection_timeout" , "600s" ) ;
614+ } else {
615+ // default is: 500ms (too small)
616+ mut_pairs. append_pair ( "connection_timeout" , "5s" ) ;
617+ }
598618 }
599619 // Note, right now even on a big clusters, everything works within default timeout (180s),
600620 // but just to make it "user friendly" even for some obscure setups, let's increase the
@@ -904,4 +924,35 @@ mod tests {
904924 let args: HashMap < _ , _ > = url. query_pairs ( ) . into_owned ( ) . collect ( ) ;
905925 assert_eq ! ( args. get( "skip_verify" ) , Some ( & "true" . into( ) ) ) ;
906926 }
927+
928+ #[ test]
929+ fn test_cloud_defaults ( ) {
930+ {
931+ let mut options = ClickHouseOptions {
932+ host : Some ( "uzg8q0g12h.eu-central-1.aws.clickhouse.cloud" . into ( ) ) ,
933+ ..Default :: default ( )
934+ } ;
935+ clickhouse_url_defaults ( & mut options, None ) . unwrap ( ) ;
936+ let url = parse_url ( & options) . unwrap ( ) ;
937+ let args: HashMap < _ , _ > = url. query_pairs ( ) . into_owned ( ) . collect ( ) ;
938+
939+ assert_eq ! ( args. get( "secure" ) , Some ( & "true" . into( ) ) ) ;
940+ assert_eq ! ( args. get( "connection_timeout" ) , Some ( & "600s" . into( ) ) ) ;
941+ }
942+
943+ // Note, checking for ClickHouseOptions{secure: false} does not make sense, since it is the default
944+
945+ {
946+ let mut options = ClickHouseOptions {
947+ url : Some ( "uzg8q0g12h.eu-central-1.aws.clickhouse.cloud/?secure=false&connection_timeout=1ms" . into ( ) ) ,
948+ ..Default :: default ( )
949+ } ;
950+ clickhouse_url_defaults ( & mut options, None ) . unwrap ( ) ;
951+ let url = parse_url ( & options) . unwrap ( ) ;
952+ let args: HashMap < _ , _ > = url. query_pairs ( ) . into_owned ( ) . collect ( ) ;
953+
954+ assert_eq ! ( args. get( "secure" ) , Some ( & "false" . into( ) ) ) ;
955+ assert_eq ! ( args. get( "connection_timeout" ) , Some ( & "1ms" . into( ) ) ) ;
956+ }
957+ }
907958}
0 commit comments