@@ -731,31 +731,7 @@ impl ClusterWorker {
731731 let use_keyspace_results: Vec < Result < ( ) , QueryError > > =
732732 join_all ( use_keyspace_futures) . await ;
733733
734- // If there was at least one Ok and the rest were IoErrors we can return Ok
735- // keyspace name is correct and will be used on broken connection on the next reconnect
736-
737- // If there were only IoErrors then return IoError
738- // If there was an error different than IoError return this error - something is wrong
739-
740- let mut was_ok: bool = false ;
741- let mut io_error: Option < Arc < std:: io:: Error > > = None ;
742-
743- for result in use_keyspace_results {
744- match result {
745- Ok ( ( ) ) => was_ok = true ,
746- Err ( err) => match err {
747- QueryError :: IoError ( io_err) => io_error = Some ( io_err) ,
748- _ => return Err ( err) ,
749- } ,
750- }
751- }
752-
753- if was_ok {
754- return Ok ( ( ) ) ;
755- }
756-
757- // We can unwrap io_error because use_keyspace_futures must be nonempty
758- Err ( QueryError :: IoError ( io_error. unwrap ( ) ) )
734+ use_keyspace_result ( use_keyspace_results. into_iter ( ) )
759735 }
760736
761737 async fn perform_refresh ( & mut self ) -> Result < ( ) , QueryError > {
@@ -788,3 +764,39 @@ impl ClusterWorker {
788764 self . cluster_data . store ( new_cluster_data) ;
789765 }
790766}
767+
768+ /// Returns a result of use_keyspace operation, based on the query results
769+ /// returned from given node/connection.
770+ ///
771+ /// This function assumes that `use_keyspace_results` iterator is NON-EMPTY!
772+ pub ( crate ) fn use_keyspace_result (
773+ use_keyspace_results : impl Iterator < Item = Result < ( ) , QueryError > > ,
774+ ) -> Result < ( ) , QueryError > {
775+ // If there was at least one Ok and the rest were broken connection errors we can return Ok
776+ // keyspace name is correct and will be used on broken connection on the next reconnect
777+
778+ // If there were only broken connection errors then return broken connection error.
779+ // If there was an error different than broken connection error return this error - something is wrong
780+
781+ let mut was_ok: bool = false ;
782+ let mut broken_conn_error: Option < QueryError > = None ;
783+
784+ for result in use_keyspace_results {
785+ match result {
786+ Ok ( ( ) ) => was_ok = true ,
787+ Err ( err) => match err {
788+ QueryError :: BrokenConnection ( _) | QueryError :: ConnectionPoolError ( _) => {
789+ broken_conn_error = Some ( err)
790+ }
791+ _ => return Err ( err) ,
792+ } ,
793+ }
794+ }
795+
796+ if was_ok {
797+ return Ok ( ( ) ) ;
798+ }
799+
800+ // We can unwrap conn_broken_error because use_keyspace_results must be nonempty
801+ Err ( broken_conn_error. unwrap ( ) )
802+ }
0 commit comments