@@ -154,6 +154,9 @@ int is_tcp_main = 0;
154154 * current process - attention, this is a really ugly HACK here */
155155unsigned int last_outgoing_tcp_id = 0 ;
156156
157+ /* Incremental count of which index to check first when picking a worker to send a TCP connection to */
158+ static int send2worker_start_index = 0 ;
159+
157160static struct scaling_profile * s_profile = NULL ;
158161
159162/****************************** helper functions *****************************/
@@ -266,39 +269,48 @@ int tcp_init_sock_opt(int s, struct tcp_conn_profile *prof, enum si_flags socket
266269 return -1 ;
267270}
268271
269- static int send2worker (struct tcp_connection * tcpconn ,int rw )
272+ static int send2worker (struct tcp_connection * tcpconn , int rw )
270273{
271- int i ;
272- int min_load ;
273- int idx ;
274+ int i , worker_idx ;
275+ int min_load = 100 ;
276+ int idx = -1 ;
274277 long response [2 ];
275278 unsigned int load ;
276279
277- min_load = 100 ; /* it is a percentage */
278- idx = 0 ;
279- for (i = 0 ; i < tcp_workers_max_no ; i ++ ){
280- if (tcp_workers [i ].state == STATE_ACTIVE ) {
281- load = pt_get_1m_proc_load ( tcp_workers [i ].pt_idx );
280+ for (i = 0 ; i < tcp_workers_max_no ; i ++ ) {
281+ worker_idx = (send2worker_start_index + i ) % tcp_workers_max_no ;
282+ if (tcp_workers [worker_idx ].state == STATE_ACTIVE ) {
283+ load = pt_get_1m_proc_load ( tcp_workers [worker_idx ].pt_idx );
282284#ifdef EXTRA_DEBUG
283285 LM_DBG ("checking TCP worker %d (proc %d), with load %u,"
284- "min_load so far %u\n" , i , tcp_workers [i ].pt_idx , load ,
286+ "min_load so far %u\n" , worker_idx , tcp_workers [worker_idx ].pt_idx , load ,
285287 min_load );
286288#endif
287- if (min_load > load ) {
289+ if (min_load > load ) {
288290 min_load = load ;
289- idx = i ;
291+ idx = worker_idx ;
290292 }
291293 }
292294 }
293295
296+ /* If no idx is selected due to no active workers but this function is called something is up */
297+ if (idx == -1 ) {
298+ LM_BUG ("No active TCP workers\n" );
299+ return -1 ;
300+ }
301+
302+ if (++ send2worker_start_index == tcp_workers_max_no ) {
303+ send2worker_start_index = 0 ;
304+ }
305+
294306 tcp_workers [idx ].n_reqs ++ ;
295307 LM_DBG ("to tcp worker %d (%d/%d) load %u, %p/%d rw %d\n" , idx ,
296308 tcp_workers [idx ].pid , tcp_workers [idx ].pt_idx , min_load ,
297309 tcpconn , tcpconn -> s , rw );
298- response [ 0 ] = ( long ) tcpconn ;
299- response [1 ] = rw ;
300- if ( send_fd ( tcp_workers [ idx ]. unix_sock , response , sizeof ( response ),
301- tcpconn -> s )<= 0 ){
310+
311+ response [0 ] = ( long ) tcpconn ;
312+ response [ 1 ] = rw ;
313+ if ( send_fd ( tcp_workers [ idx ]. unix_sock , response , sizeof ( response ), tcpconn -> s ) <= 0 ){
302314 LM_ERR ("send_fd failed\n" );
303315 return -1 ;
304316 }
0 commit comments