(ch contextHolder)
| 288 | } |
| 289 | |
| 290 | func (worker *worker) handleRequest(ch contextHolder) error { |
| 291 | metrics.StartWorkerRequest(worker.name) |
| 292 | |
| 293 | runtime.Gosched() |
| 294 | |
| 295 | if worker.queuedRequests.Load() == 0 { |
| 296 | // dispatch requests to all worker threads in order |
| 297 | worker.threadMutex.RLock() |
| 298 | for _, thread := range worker.threads { |
| 299 | select { |
| 300 | case thread.requestChan <- ch: |
| 301 | worker.threadMutex.RUnlock() |
| 302 | <-ch.frankenPHPContext.done |
| 303 | metrics.StopWorkerRequest(worker.name, time.Since(ch.frankenPHPContext.startedAt)) |
| 304 | |
| 305 | return nil |
| 306 | default: |
| 307 | // thread is busy, continue |
| 308 | } |
| 309 | } |
| 310 | worker.threadMutex.RUnlock() |
| 311 | } |
| 312 | |
| 313 | // if no thread was available, mark the request as queued and apply the scaling strategy |
| 314 | worker.queuedRequests.Add(1) |
| 315 | metrics.QueuedWorkerRequest(worker.name) |
| 316 | |
| 317 | for { |
| 318 | workerScaleChan := scaleChan |
| 319 | if worker.isAtThreadLimit() { |
| 320 | workerScaleChan = nil // max_threads for this worker reached, do not attempt scaling |
| 321 | } |
| 322 | |
| 323 | select { |
| 324 | case worker.requestChan <- ch: |
| 325 | worker.queuedRequests.Add(-1) |
| 326 | metrics.DequeuedWorkerRequest(worker.name) |
| 327 | <-ch.frankenPHPContext.done |
| 328 | metrics.StopWorkerRequest(worker.name, time.Since(ch.frankenPHPContext.startedAt)) |
| 329 | |
| 330 | return nil |
| 331 | case workerScaleChan <- ch.frankenPHPContext: |
| 332 | // the request has triggered scaling, continue to wait for a thread |
| 333 | case <-timeoutChan(maxWaitTime): |
| 334 | // the request has timed out stalling |
| 335 | worker.queuedRequests.Add(-1) |
| 336 | metrics.DequeuedWorkerRequest(worker.name) |
| 337 | metrics.StopWorkerRequest(worker.name, time.Since(ch.frankenPHPContext.startedAt)) |
| 338 | |
| 339 | ch.frankenPHPContext.reject(ErrMaxWaitTimeExceeded) |
| 340 | |
| 341 | return ErrMaxWaitTimeExceeded |
| 342 | } |
| 343 | } |
| 344 | } |
no test coverage detected