@@ -2904,7 +2904,7 @@ nc_ch_client_thread(void *arg)
29042904 struct nc_ch_client_thread_arg * data = (struct nc_ch_client_thread_arg * )arg ;
29052905 NC_MSG_TYPE msgtype ;
29062906 uint8_t cur_attempts = 0 ;
2907- uint16_t i ;
2907+ uint16_t next_endpt_index ;
29082908 char * cur_endpt_name = NULL ;
29092909 struct nc_ch_endpt * cur_endpt ;
29102910 struct nc_session * session ;
@@ -2957,10 +2957,21 @@ nc_ch_client_thread(void *arg)
29572957
29582958 /* set next endpoint to try */
29592959 if (client -> start_with == NC_CH_FIRST_LISTED ) {
2960- cur_endpt = & client -> ch_endpts [0 ];
2961- free (cur_endpt_name );
2962- cur_endpt_name = strdup (cur_endpt -> name );
2963- } /* else we keep the current one */
2960+ next_endpt_index = 0 ;
2961+ }
2962+ else {
2963+ /* we keep the current one but due to unlock/lock we have to find it again */
2964+ for (next_endpt_index = 0 ; next_endpt_index < client -> ch_endpt_count ; ++ next_endpt_index ) {
2965+ if (!strcmp (client -> ch_endpts [next_endpt_index ].name , cur_endpt_name )) {
2966+ break ;
2967+ }
2968+ }
2969+ if (next_endpt_index >= client -> ch_endpt_count ) {
2970+ /* endpoint was removed, start with the first one */
2971+ next_endpt_index = 0 ;
2972+ }
2973+ }
2974+
29642975 } else {
29652976 /* UNLOCK */
29662977 nc_server_ch_client_unlock (client );
@@ -2977,36 +2988,33 @@ nc_ch_client_thread(void *arg)
29772988 ++ cur_attempts ;
29782989
29792990 /* try to find our endpoint again */
2980- for (i = 0 ; i < client -> ch_endpt_count ; ++ i ) {
2981- if (!strcmp (client -> ch_endpts [i ].name , cur_endpt_name )) {
2991+ for (next_endpt_index = 0 ; next_endpt_index < client -> ch_endpt_count ; ++ next_endpt_index ) {
2992+ if (!strcmp (client -> ch_endpts [next_endpt_index ].name , cur_endpt_name )) {
29822993 break ;
29832994 }
29842995 }
29852996
2986- if (i < client -> ch_endpt_count ) {
2997+ if (next_endpt_index >= client -> ch_endpt_count ) {
29872998 /* endpoint was removed, start with the first one */
2988- cur_endpt = & client -> ch_endpts [0 ];
2989- free (cur_endpt_name );
2990- cur_endpt_name = strdup (cur_endpt -> name );
2991-
2999+ next_endpt_index = 0 ;
29923000 cur_attempts = 0 ;
29933001 } else if (cur_attempts == client -> max_attempts ) {
29943002 /* we have tried to connect to this endpoint enough times */
2995- if (i < client -> ch_endpt_count - 1 ) {
3003+ if (next_endpt_index < client -> ch_endpt_count - 1 ) {
29963004 /* just go to the next endpoint */
2997- cur_endpt = & client -> ch_endpts [i + 1 ];
2998- free (cur_endpt_name );
2999- cur_endpt_name = strdup (cur_endpt -> name );
3005+ ++ next_endpt_index ;
30003006 } else {
30013007 /* cur_endpoint is the last, start with the first one */
3002- cur_endpt = & client -> ch_endpts [0 ];
3003- free (cur_endpt_name );
3004- cur_endpt_name = strdup (cur_endpt -> name );
3008+ next_endpt_index = 0 ;
30053009 }
30063010
30073011 cur_attempts = 0 ;
30083012 } /* else we keep the current one */
30093013 }
3014+
3015+ cur_endpt = & client -> ch_endpts [next_endpt_index ];
3016+ free (cur_endpt_name );
3017+ cur_endpt_name = strdup (cur_endpt -> name );
30103018 }
30113019
30123020cleanup :
0 commit comments