@@ -165,6 +165,9 @@ int is_tcp_main = 0;
165165 * current process - attention, this is a really ugly HACK here */
166166unsigned int last_outgoing_tcp_id = 0 ;
167167
168+ /* Incremental count of which index to check first when picking a worker to send a TCP connection to */
169+ static int send2worker_start_index = 0 ;
170+
168171static struct scaling_profile * s_profile = NULL ;
169172
170173/****************************** helper functions *****************************/
@@ -279,39 +282,48 @@ int tcp_init_sock_opt(int s, const struct tcp_conn_profile *prof, enum si_flags
279282 return -1 ;
280283}
281284
282- static int send2worker (struct tcp_connection * tcpconn ,int rw )
285+ static int send2worker (struct tcp_connection * tcpconn , int rw )
283286{
284- int i ;
285- int min_load ;
286- int idx ;
287+ int i , worker_idx ;
288+ int min_load = 100 ;
289+ int idx = -1 ;
287290 long response [2 ];
288291 unsigned int load ;
289292
290- min_load = 100 ; /* it is a percentage */
291- idx = 0 ;
292- for (i = 0 ; i < tcp_workers_max_no ; i ++ ){
293- if (tcp_workers [i ].state == STATE_ACTIVE ) {
294- load = pt_get_1m_proc_load ( tcp_workers [i ].pt_idx );
293+ for (i = 0 ; i < tcp_workers_max_no ; i ++ ) {
294+ worker_idx = (send2worker_start_index + i ) % tcp_workers_max_no ;
295+ if (tcp_workers [worker_idx ].state == STATE_ACTIVE ) {
296+ load = pt_get_1m_proc_load ( tcp_workers [worker_idx ].pt_idx );
295297#ifdef EXTRA_DEBUG
296298 LM_DBG ("checking TCP worker %d (proc %d), with load %u,"
297- "min_load so far %u\n" , i , tcp_workers [i ].pt_idx , load ,
299+ "min_load so far %u\n" , worker_idx , tcp_workers [worker_idx ].pt_idx , load ,
298300 min_load );
299301#endif
300- if (min_load > load ) {
302+ if (min_load > load ) {
301303 min_load = load ;
302- idx = i ;
304+ idx = worker_idx ;
303305 }
304306 }
305307 }
306308
309+ /* If no idx is selected due to no active workers but this function is called something is up */
310+ if (idx == -1 ) {
311+ LM_BUG ("No active TCP workers\n" );
312+ return -1 ;
313+ }
314+
315+ if (++ send2worker_start_index == tcp_workers_max_no ) {
316+ send2worker_start_index = 0 ;
317+ }
318+
307319 tcp_workers [idx ].n_reqs ++ ;
308320 LM_DBG ("to tcp worker %d (%d/%d) load %u, %p/%d rw %d\n" , idx ,
309321 tcp_workers [idx ].pid , tcp_workers [idx ].pt_idx , min_load ,
310322 tcpconn , tcpconn -> s , rw );
311- response [ 0 ] = ( long ) tcpconn ;
312- response [1 ] = rw ;
313- if ( send_fd ( tcp_workers [ idx ]. unix_sock , response , sizeof ( response ),
314- tcpconn -> s )<= 0 ){
323+
324+ response [0 ] = ( long ) tcpconn ;
325+ response [ 1 ] = rw ;
326+ if ( send_fd ( tcp_workers [ idx ]. unix_sock , response , sizeof ( response ), tcpconn -> s ) <= 0 ){
315327 LM_ERR ("send_fd failed\n" );
316328 return -1 ;
317329 }
0 commit comments