| 338 | } |
| 339 | |
| 340 | static proxy_worker *find_best_worker(proxy_balancer *balancer, |
| 341 | request_rec *r) |
| 342 | { |
| 343 | proxy_worker *candidate = NULL; |
| 344 | apr_status_t rv; |
| 345 | |
| 346 | #if APR_HAS_THREADS |
| 347 | if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) { |
| 348 | ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163) |
| 349 | "%s: Lock failed for find_best_worker()", |
| 350 | balancer->s->name); |
| 351 | return NULL; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | candidate = (*balancer->lbmethod->finder)(balancer, r); |
| 356 | |
| 357 | if (candidate) |
| 358 | candidate->s->elected++; |
| 359 | |
| 360 | #if APR_HAS_THREADS |
| 361 | if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) { |
| 362 | ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164) |
| 363 | "%s: Unlock failed for find_best_worker()", |
| 364 | balancer->s->name); |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | if (candidate == NULL) { |
| 369 | /* All the workers are in error state or disabled. |
| 370 | * If the balancer has a timeout sleep for a while |
| 371 | * and try again to find the worker. The chances are |
| 372 | * that some other thread will release a connection. |
| 373 | * By default the timeout is not set, and the server |
| 374 | * returns SERVER_BUSY. |
| 375 | */ |
| 376 | if (balancer->s->timeout) { |
| 377 | /* XXX: This can perhaps be build using some |
| 378 | * smarter mechanism, like tread_cond. |
| 379 | * But since the statuses can came from |
| 380 | * different children, use the provided algo. |
| 381 | */ |
| 382 | apr_interval_time_t timeout = balancer->s->timeout; |
| 383 | apr_interval_time_t step, tval = 0; |
| 384 | /* Set the timeout to 0 so that we don't |
| 385 | * end in infinite loop |
| 386 | */ |
| 387 | balancer->s->timeout = 0; |
| 388 | step = timeout / 100; |
| 389 | while (tval < timeout) { |
| 390 | apr_sleep(step); |
| 391 | /* Try again */ |
| 392 | if ((candidate = find_best_worker(balancer, r))) |
| 393 | break; |
| 394 | tval += step; |
| 395 | } |
| 396 | /* restore the timeout */ |
| 397 | balancer->s->timeout = timeout; |
no outgoing calls
no test coverage detected