Skip to content

Commit 00d819f

Browse files
committed
add test, use getWorker(ForContext) function in frankenphp.go as well
1 parent e362fd3 commit 00d819f

5 files changed

Lines changed: 83 additions & 25 deletions

File tree

caddy/caddy_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9+
"strconv"
910
"strings"
1011
"sync"
1112
"sync/atomic"
@@ -119,6 +120,59 @@ func TestWorker(t *testing.T) {
119120
wg.Wait()
120121
}
121122

123+
func TestGlobalAndLocalWorker(t *testing.T) {
124+
var wg sync.WaitGroup
125+
testPortNum, _ := strconv.Atoi(testPort)
126+
testPortTwo := strconv.Itoa(testPortNum + 1)
127+
tester := caddytest.NewTester(t)
128+
tester.InitServer(`
129+
{
130+
skip_install_trust
131+
admin localhost:2999
132+
133+
frankenphp {
134+
worker {
135+
file ../testdata/worker-with-env.php
136+
num 1
137+
env APP_ENV global
138+
}
139+
}
140+
}
141+
142+
http://localhost:`+testPort+` {
143+
route {
144+
php {
145+
root ../testdata
146+
worker {
147+
file worker-with-env.php
148+
num 2
149+
env APP_ENV local
150+
}
151+
}
152+
}
153+
}
154+
155+
http://localhost:`+testPortTwo+` {
156+
route {
157+
php {
158+
root ../testdata
159+
}
160+
}
161+
}
162+
`, "caddyfile")
163+
164+
for i := 0; i < 100; i++ {
165+
wg.Add(1)
166+
167+
go func(i int) {
168+
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=local")
169+
tester.AssertGetResponse("http://localhost:"+testPortTwo+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=global")
170+
wg.Done()
171+
}(i)
172+
}
173+
wg.Wait()
174+
}
175+
122176
func TestEnv(t *testing.T) {
123177
tester := caddytest.NewTester(t)
124178
tester.InitServer(`

frankenphp.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,13 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
405405
}
406406

407407
// Detect if a worker is available to handle this request
408-
// Look for a worker with matching moduleID or a global worker (moduleID == 0)
409-
if workersList, ok := workers[fc.scriptFilename]; ok {
410-
for _, worker := range workersList {
411-
if worker.moduleID == 0 || worker.moduleID == fc.moduleID {
412-
worker.handleRequest(fc)
413-
return nil
414-
}
415-
}
408+
if worker := getWorkerForContext(fc); worker != nil {
409+
worker.handleRequest(fc)
410+
} else {
411+
// If no worker was available, send the request to non-worker threads
412+
handleRequestWithRegularPHPThreads(fc)
416413
}
417414

418-
// If no worker was available, send the request to non-worker threads
419-
handleRequestWithRegularPHPThreads(fc)
420-
421415
return nil
422416
}
423417

scaling.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,6 @@ func scaleRegularThread() {
130130
autoScaledThreads = append(autoScaledThreads, thread)
131131
}
132132

133-
func getWorker(fc *frankenPHPContext) *worker {
134-
// if the request has been stalled long enough, scale
135-
if workersList, ok := workers[fc.scriptFilename]; ok {
136-
// Look for a worker with matching moduleID or a global worker (moduleID == 0)
137-
for _, worker := range workersList {
138-
if worker.moduleID == 0 || worker.moduleID == fc.moduleID {
139-
return worker
140-
}
141-
}
142-
}
143-
return nil
144-
}
145-
146133
func startUpscalingThreads(maxScaledThreads int, scale chan *frankenPHPContext, done chan struct{}) {
147134
for {
148135
scalingMu.Lock()
@@ -173,7 +160,7 @@ func startUpscalingThreads(maxScaledThreads int, scale chan *frankenPHPContext,
173160
}
174161

175162
// if the request has been stalled long enough, scale
176-
if worker := getWorker(fc); worker != nil {
163+
if worker := getWorkerForContext(fc); worker != nil {
177164
scaleWorkerThread(worker)
178165
} else {
179166
scaleRegularThread()

testdata/worker-with-env.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$i = 0;
4+
$env = $_SERVER['APP_ENV'];
5+
do {
6+
$ok = frankenphp_handle_request(function () use ($i, $env): void {
7+
echo "Worker has APP_ENV=$env";
8+
});
9+
10+
$i++;
11+
} while ($ok);

worker.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ func initWorkers(opt []workerOpt) error {
7070
return nil
7171
}
7272

73+
func getWorkerForContext(fc *frankenPHPContext) *worker {
74+
if workersList, ok := workers[fc.scriptFilename]; ok {
75+
// Look for a worker with matching moduleID or a global worker (moduleID == 0)
76+
for _, worker := range workersList {
77+
if worker.moduleID == 0 || worker.moduleID == fc.moduleID {
78+
return worker
79+
}
80+
}
81+
}
82+
return nil
83+
}
84+
7385
func newWorker(o workerOpt) (*worker, error) {
7486
absFileName, err := fastabs.FastAbs(o.fileName)
7587
if err != nil {

0 commit comments

Comments
 (0)