@@ -664,99 +664,62 @@ void shard_connection::handle_event(short events)
664664 benchmark_error_log (" Connection error: %s\n " , strerror (errno));
665665 }
666666
667- // Update connection error statistics
668- struct timeval now;
669- gettimeofday (&now, NULL );
670- client* c = static_cast <client*>(m_conns_manager);
671- c->get_stats ()->update_connection_error (&now);
672-
673- // Attempt reconnection if enabled and not already reconnecting
674- if (m_config->reconnect_on_error && !m_reconnecting &&
675- (m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts )) {
676-
677- disconnect ();
678- m_reconnect_attempts++;
679- if (m_config->reconnect_backoff_factor > 0.0 ) {
680- m_current_backoff_delay *= m_config->reconnect_backoff_factor ;
681- }
682-
683- if (m_config->max_reconnect_attempts == 0 ) {
684- benchmark_error_log (" Connection error, attempting reconnection %u (unlimited) in %.2f seconds...\n " ,
685- m_reconnect_attempts, m_current_backoff_delay);
686- } else {
687- benchmark_error_log (" Connection error, attempting reconnection %u/%u in %.2f seconds...\n " ,
688- m_reconnect_attempts, m_config->max_reconnect_attempts , m_current_backoff_delay);
689- }
690-
691- // Schedule reconnection attempt
692- struct timeval delay;
693- delay.tv_sec = (long )m_current_backoff_delay;
694- delay.tv_usec = (long )((m_current_backoff_delay - delay.tv_sec ) * 1000000 );
695-
696- m_reconnect_timer = event_new (m_event_base, -1 , 0 , cluster_client_reconnect_timer_handler, (void *)this );
697- event_add (m_reconnect_timer, &delay);
698- m_reconnecting = true ;
699- } else {
700- benchmark_error_log (" Maximum reconnection attempts (%u) exceeded for connection error, triggering thread restart.\n " ,
701- m_config->max_reconnect_attempts );
702- disconnect ();
703- // Break the event loop to trigger thread restart
704- event_base_loopbreak (m_event_base);
705- }
706-
667+ attempt_reconnect (" Connection error" );
707668 return ;
708669 }
709670
710671 if (events & BEV_EVENT_EOF) {
711672 benchmark_error_log (" connection dropped.\n " );
673+ attempt_reconnect (" Connection dropped" );
674+ return ;
675+ }
676+ }
712677
713- // Update connection error statistics
714- struct timeval now;
715- gettimeofday (&now, NULL );
716- client* c = static_cast <client*>(m_conns_manager);
717- c->get_stats ()->update_connection_error (&now);
718-
719- // Attempt reconnection if enabled and not already reconnecting
720- if (m_config->reconnect_on_error && !m_reconnecting &&
721- (m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts )) {
678+ void shard_connection::handle_timer_event () {
679+ m_request_per_cur_interval = m_config->request_per_interval ;
680+ fill_pipeline ();
681+ }
722682
723- disconnect ();
724- m_reconnect_attempts++;
725- if (m_config->reconnect_backoff_factor > 0.0 ) {
726- m_current_backoff_delay *= m_config->reconnect_backoff_factor ;
727- }
683+ void shard_connection::attempt_reconnect (const char * error_context) {
684+ // Update connection error statistics
685+ struct timeval now;
686+ gettimeofday (&now, NULL );
687+ client* c = static_cast <client*>(m_conns_manager);
688+ c->get_stats ()->update_connection_error (&now);
728689
729- if (m_config->max_reconnect_attempts == 0 ) {
730- benchmark_error_log (" Connection dropped, attempting reconnection %u (unlimited) in %.2f seconds...\n " ,
731- m_reconnect_attempts, m_current_backoff_delay);
732- } else {
733- benchmark_error_log (" Connection dropped, attempting reconnection %u/%u in %.2f seconds...\n " ,
734- m_reconnect_attempts, m_config->max_reconnect_attempts , m_current_backoff_delay);
735- }
690+ // Attempt reconnection if enabled and not already reconnecting
691+ if (m_config->reconnect_on_error && !m_reconnecting &&
692+ (m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts )) {
736693
737- // Schedule reconnection attempt
738- struct timeval delay;
739- delay.tv_sec = (long )m_current_backoff_delay;
740- delay.tv_usec = (long )((m_current_backoff_delay - delay.tv_sec ) * 1000000 );
694+ disconnect ();
695+ m_reconnect_attempts++;
696+ if (m_config->reconnect_backoff_factor > 0.0 ) {
697+ m_current_backoff_delay *= m_config->reconnect_backoff_factor ;
698+ }
741699
742- m_reconnect_timer = event_new (m_event_base, - 1 , 0 , cluster_client_reconnect_timer_handler, ( void *) this );
743- event_add (m_reconnect_timer, &delay);
744- m_reconnecting = true ;
700+ if (m_config-> max_reconnect_attempts == 0 ) {
701+ benchmark_error_log ( " %s, attempting reconnection %u (unlimited) in %.2f seconds... \n " ,
702+ error_context, m_reconnect_attempts, m_current_backoff_delay) ;
745703 } else {
746- benchmark_error_log (" Maximum reconnection attempts (%u) exceeded for connection drop, triggering thread restart.\n " ,
747- m_config->max_reconnect_attempts );
748- disconnect ();
749- // Break the event loop to trigger thread restart
750- event_base_loopbreak (m_event_base);
704+ benchmark_error_log (" %s, attempting reconnection %u/%u in %.2f seconds...\n " ,
705+ error_context, m_reconnect_attempts, m_config->max_reconnect_attempts , m_current_backoff_delay);
751706 }
752707
753- return ;
754- }
755- }
708+ // Schedule reconnection attempt
709+ struct timeval delay;
710+ delay.tv_sec = (long )m_current_backoff_delay;
711+ delay.tv_usec = (long )((m_current_backoff_delay - delay.tv_sec ) * 1000000 );
756712
757- void shard_connection::handle_timer_event () {
758- m_request_per_cur_interval = m_config->request_per_interval ;
759- fill_pipeline ();
713+ m_reconnect_timer = event_new (m_event_base, -1 , 0 , cluster_client_reconnect_timer_handler, (void *)this );
714+ event_add (m_reconnect_timer, &delay);
715+ m_reconnecting = true ;
716+ } else {
717+ benchmark_error_log (" Maximum reconnection attempts (%u) exceeded for %s, triggering thread restart.\n " ,
718+ m_config->max_reconnect_attempts , error_context);
719+ disconnect ();
720+ // Break the event loop to trigger thread restart
721+ event_base_loopbreak (m_event_base);
722+ }
760723}
761724
762725void shard_connection::handle_reconnect_timer_event () {
@@ -815,46 +778,7 @@ void shard_connection::handle_connection_timeout_event() {
815778 }
816779
817780 benchmark_error_log (" Connection timeout after %u seconds.\n " , m_config->connection_timeout );
818-
819- // Update connection error statistics
820- struct timeval now;
821- gettimeofday (&now, NULL );
822- client* c = static_cast <client*>(m_conns_manager);
823- c->get_stats ()->update_connection_error (&now);
824-
825- // Treat timeout as a connection error and attempt reconnection if enabled
826- if (m_config->reconnect_on_error && !m_reconnecting &&
827- (m_config->max_reconnect_attempts == 0 || m_reconnect_attempts < m_config->max_reconnect_attempts )) {
828-
829- disconnect ();
830- m_reconnect_attempts++;
831- if (m_config->reconnect_backoff_factor > 0.0 ) {
832- m_current_backoff_delay *= m_config->reconnect_backoff_factor ;
833- }
834-
835- if (m_config->max_reconnect_attempts == 0 ) {
836- benchmark_error_log (" Connection timeout, attempting reconnection %u (unlimited) in %.2f seconds...\n " ,
837- m_reconnect_attempts, m_current_backoff_delay);
838- } else {
839- benchmark_error_log (" Connection timeout, attempting reconnection %u/%u in %.2f seconds...\n " ,
840- m_reconnect_attempts, m_config->max_reconnect_attempts , m_current_backoff_delay);
841- }
842-
843- // Schedule reconnection attempt
844- struct timeval delay;
845- delay.tv_sec = (long )m_current_backoff_delay;
846- delay.tv_usec = (long )((m_current_backoff_delay - delay.tv_sec ) * 1000000 );
847-
848- m_reconnect_timer = event_new (m_event_base, -1 , 0 , cluster_client_reconnect_timer_handler, (void *)this );
849- event_add (m_reconnect_timer, &delay);
850- m_reconnecting = true ;
851- } else {
852- benchmark_error_log (" Maximum reconnection attempts (%u) exceeded for connection timeout, triggering thread restart.\n " ,
853- m_config->max_reconnect_attempts );
854- disconnect ();
855- // Break the event loop to trigger thread restart
856- event_base_loopbreak (m_event_base);
857- }
781+ attempt_reconnect (" Connection timeout" );
858782}
859783
860784void shard_connection::send_wait_command (struct timeval * sent_time,
0 commit comments