mirror of
https://github.com/php/frankenphp.git
synced 2026-03-24 00:52:11 +01:00
perf: tail latency with goSched (#2033)
Alternate implementation to #2016 that doesn't reduce RPS with lower amounts of threads
This commit is contained in:
committed by
GitHub
parent
abaf03c7f7
commit
dadeb5a628
@@ -2,7 +2,9 @@ package frankenphp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// representation of a non-worker PHP thread
|
||||
@@ -16,9 +18,10 @@ type regularThread struct {
|
||||
}
|
||||
|
||||
var (
|
||||
regularThreads []*phpThread
|
||||
regularThreadMu = &sync.RWMutex{}
|
||||
regularRequestChan chan contextHolder
|
||||
regularThreads []*phpThread
|
||||
regularThreadMu = &sync.RWMutex{}
|
||||
regularRequestChan chan contextHolder
|
||||
queuedRegularThreads = atomic.Int32{}
|
||||
)
|
||||
|
||||
func convertToRegularThread(thread *phpThread) {
|
||||
@@ -81,6 +84,7 @@ func (handler *regularThread) waitForRequest() string {
|
||||
// go back to beforeScriptExecution
|
||||
return handler.beforeScriptExecution()
|
||||
case ch = <-regularRequestChan:
|
||||
case ch = <-handler.thread.requestChan:
|
||||
}
|
||||
|
||||
handler.ctx = ch.ctx
|
||||
@@ -100,23 +104,35 @@ func (handler *regularThread) afterRequest() {
|
||||
func handleRequestWithRegularPHPThreads(ch contextHolder) error {
|
||||
metrics.StartRequest()
|
||||
|
||||
select {
|
||||
case regularRequestChan <- ch:
|
||||
// a thread was available to handle the request immediately
|
||||
<-ch.frankenPHPContext.done
|
||||
metrics.StopRequest()
|
||||
runtime.Gosched()
|
||||
|
||||
return nil
|
||||
default:
|
||||
// no thread was available
|
||||
if queuedRegularThreads.Load() == 0 {
|
||||
regularThreadMu.RLock()
|
||||
for _, thread := range regularThreads {
|
||||
select {
|
||||
case thread.requestChan <- ch:
|
||||
regularThreadMu.RUnlock()
|
||||
<-ch.frankenPHPContext.done
|
||||
metrics.StopRequest()
|
||||
|
||||
return nil
|
||||
default:
|
||||
// thread was not available
|
||||
}
|
||||
}
|
||||
regularThreadMu.RUnlock()
|
||||
}
|
||||
|
||||
// if no thread was available, mark the request as queued and fan it out to all threads
|
||||
queuedRegularThreads.Add(1)
|
||||
metrics.QueuedRequest()
|
||||
|
||||
for {
|
||||
select {
|
||||
case regularRequestChan <- ch:
|
||||
queuedRegularThreads.Add(-1)
|
||||
metrics.DequeuedRequest()
|
||||
|
||||
<-ch.frankenPHPContext.done
|
||||
metrics.StopRequest()
|
||||
|
||||
@@ -125,7 +141,9 @@ func handleRequestWithRegularPHPThreads(ch contextHolder) error {
|
||||
// the request has triggered scaling, continue to wait for a thread
|
||||
case <-timeoutChan(maxWaitTime):
|
||||
// the request has timed out stalling
|
||||
queuedRegularThreads.Add(-1)
|
||||
metrics.DequeuedRequest()
|
||||
metrics.StopRequest()
|
||||
|
||||
ch.frankenPHPContext.reject(ErrMaxWaitTimeExceeded)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user