Skip to content

Commit 1d74b2c

Browse files
henderkesdunglasIndraGunawanRom1Bastide
authored
feat: define domain specific workers in php_server and php blocks (#1509)
* add module (php_server directive) based workers * refactor moduleID to uintptr for faster comparisons * let workers inherit environment variables and root from php_server * caddy can shift FrankenPHPModules in memory for some godforsaken reason, can't rely on them staying the same * remove debugging statement * fix tests * refactor moduleID to uint64 for faster comparisons * actually allow multiple workers per script filename * remove logging * utility function * reuse existing worker with same filename and environment when calling newWorker with a filepath that already has a suitable worker, simply add number of threads * no cleanup happens between tests, so restore old global worker overwriting logic * add test, use getWorker(ForContext) function in frankenphp.go as well * bring error on second global worker with the same filename again * refactor to using name instead of moduleID * nicer name * nicer name * add more tests * remove test case already covered by previous test * revert back to single variable, moduleIDs no longer relevant * update comment * figure out the worker to use in FrankenPHPModule::ServeHTTP * add caddy/config_tests, add --retry 5 to download * add caddy/config_tests * sum up logic a bit, put worker thread addition into moduleWorkers parsing, before workers are actually created * implement suggestions as far as possible * fixup * remove tags * feat: download the mostly static binary when possible (#1467) * feat: download the mostly static binary when possible * cs * docs: remove wildcard matcher from root directive (#1513) * docs: update README with additional documentation links Add link to classic mode, efficiently serving large static files and monitoring FrankenPHP Signed-off-by: Romain Bastide <romain.bastide@orange.com> * ci: combine dependabot updates for one group to 1 pull-request * feat: compatibility with libphp.dylib on macOS * feat: upgrade to Caddy 2.10 * feat: upgrade to Caddy 2.10 * chore: run prettier * fix: build-static.sh consecutive builds (#1496) * fix consecutive builds * use minor version in PHP_VERSION * install jq in centos container * fix "arm64" download arch for spc binary * jq is not available as a rpm download * linter * specify php 8.4 default specify 8.4 so we manually switch to 8.5 when we make sure it works allows to run without jq installed * Apply suggestions from code review Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> * chore: update Go and toolchain version (#1526) * apply suggestions one be one - scriptpath only * generate unique worker names by filename and number * support worker config from embedded apps * rename back to make sure we don't accidentally add FrankenPHPApp workers to the slice * fix test after changing error message * use 🧩 for module workers * use 🌍 for global workers :) * revert 1c414ce * revert 4cc8893 * apply suggestions * add dynamic config loading test of module worker * fix test * minor changes --------- Signed-off-by: Romain Bastide <romain.bastide@orange.com> Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: Indra Gunawan <hello@indra.my.id> Co-authored-by: Romain Bastide <romain.bastide@orange.com>
1 parent 92e9233 commit 1d74b2c

File tree

14 files changed

+737
-114
lines changed

14 files changed

+737
-114
lines changed

build-static.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ if [ -z "${SPC_OPT_BUILD_ARGS}" ]; then
4646
fi
4747
# init spc download additional args
4848
if [ -z "${SPC_OPT_DOWNLOAD_ARGS}" ]; then
49+
SPC_OPT_DOWNLOAD_ARGS="--ignore-cache-sources=php-src --retry 5"
4950
if [ "${SPC_LIBC}" = "musl" ]; then
50-
SPC_OPT_DOWNLOAD_ARGS="--prefer-pre-built --ignore-cache-sources=php-src"
51-
else
52-
SPC_OPT_DOWNLOAD_ARGS="--ignore-cache-sources=php-src"
51+
SPC_OPT_DOWNLOAD_ARGS="${SPC_OPT_DOWNLOAD_ARGS} --prefer-pre-built"
5352
fi
5453
fi
5554
# if we need debug symbols, disable strip

caddy/admin_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package caddy_test
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"fmt"
7+
"github.com/dunglas/frankenphp/internal/fastabs"
58
"io"
69
"net/http"
710
"sync"
@@ -213,3 +216,82 @@ func getDebugState(t *testing.T, tester *caddytest.Tester) frankenphp.FrankenPHP
213216

214217
return debugStates
215218
}
219+
220+
func TestAddModuleWorkerViaAdminApi(t *testing.T) {
221+
// Initialize a server with admin API enabled
222+
tester := caddytest.NewTester(t)
223+
tester.InitServer(`
224+
{
225+
skip_install_trust
226+
admin localhost:2999
227+
http_port `+testPort+`
228+
229+
frankenphp
230+
}
231+
232+
localhost:`+testPort+` {
233+
route {
234+
root ../testdata
235+
php
236+
}
237+
}
238+
`, "caddyfile")
239+
240+
// Get initial debug state to check number of workers
241+
initialDebugState := getDebugState(t, tester)
242+
initialWorkerCount := 0
243+
for _, thread := range initialDebugState.ThreadDebugStates {
244+
if thread.Name != "" && thread.Name != "ready" {
245+
initialWorkerCount++
246+
}
247+
}
248+
249+
// Create a Caddyfile configuration with a module worker
250+
workerConfig := `
251+
{
252+
skip_install_trust
253+
admin localhost:2999
254+
http_port ` + testPort + `
255+
256+
frankenphp
257+
}
258+
259+
localhost:` + testPort + ` {
260+
route {
261+
root ../testdata
262+
php {
263+
worker ../testdata/worker-with-counter.php 1
264+
}
265+
}
266+
}
267+
`
268+
269+
// Send the configuration to the admin API
270+
adminUrl := "http://localhost:2999/load"
271+
r, err := http.NewRequest("POST", adminUrl, bytes.NewBufferString(workerConfig))
272+
assert.NoError(t, err)
273+
r.Header.Set("Content-Type", "text/caddyfile")
274+
resp := tester.AssertResponseCode(r, http.StatusOK)
275+
defer resp.Body.Close()
276+
277+
// Get the updated debug state to check if the worker was added
278+
updatedDebugState := getDebugState(t, tester)
279+
updatedWorkerCount := 0
280+
workerFound := false
281+
filename, _ := fastabs.FastAbs("../testdata/worker-with-counter.php")
282+
for _, thread := range updatedDebugState.ThreadDebugStates {
283+
if thread.Name != "" && thread.Name != "ready" {
284+
updatedWorkerCount++
285+
if thread.Name == "Worker PHP Thread - "+filename {
286+
workerFound = true
287+
}
288+
}
289+
}
290+
291+
// Assert that the worker was added
292+
assert.Greater(t, updatedWorkerCount, initialWorkerCount, "Worker count should have increased")
293+
assert.True(t, workerFound, fmt.Sprintf("Worker with name %q should be found", "Worker PHP Thread - "+filename))
294+
295+
// Make a request to the worker to verify it's working
296+
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-counter.php", http.StatusOK, "requests:1")
297+
}

0 commit comments

Comments
 (0)