Repository navigation
fix: request reloads #2700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlliBalliBaba
wants to merge
25
commits into
main
Choose a base branch
from
refactor/reloads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+589
−361
Open
fix: request reloads #2700
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5a41ea6
puts regular requests on main thread
AlliBalliBaba 4256e99
requeues requests
AlliBalliBaba af4bd87
Moves the fallbackserver to the main thread
AlliBalliBaba b64b778
moves servers to current mainthread
AlliBalliBaba 13015cb
makes worker requests survive reloads
AlliBalliBaba d1aae96
optimize the queue test
AlliBalliBaba 03cc9de
requeues requests only on no config change
AlliBalliBaba e8bcb9a
also rejects if no worker is found
AlliBalliBaba 1fc1286
fixes tests
AlliBalliBaba 46e3f19
removes reload test on the FrankenPHP side and only keeps them on the…
AlliBalliBaba 1d572d7
Merge branch 'main' into refactor/reloads
AlliBalliBaba 0dc8cf4
fixes tests
AlliBalliBaba 88820ad
catches nil mainThreads
AlliBalliBaba ee43087
makes reject test more consistent
AlliBalliBaba 58b13c8
cleanup
AlliBalliBaba 354ac19
unregister servers after new main thread is done
AlliBalliBaba 24e9e0a
prevents races on workers
AlliBalliBaba af6f0c6
moves more vars onto the main thread
AlliBalliBaba 02d295a
moves more config onto the main thread
AlliBalliBaba 36cec47
moves maxidletime onto mainthread
AlliBalliBaba c961c70
moves regular threads onto mainthread
AlliBalliBaba 93a42a4
moves scaled threads onto main thread
AlliBalliBaba dcb7726
moves scalechan onto main thread
AlliBalliBaba e8638b6
moves watcherIsEnabled onto main thread
AlliBalliBaba 9b60fcc
makes RestartWorkers reload-safe
AlliBalliBaba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| package caddy_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/caddyserver/caddy/v2/caddytest" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // https://github.com/php/frankenphp/issues/2469 — queued requests must survive | ||
| // a Caddy reload that restarts FrankenPHP with the same configuration. | ||
| func TestQueuedRequestSurvivesReload_module(t *testing.T) { | ||
| testQueuedRequestSurvivesReload(t, ` | ||
| { | ||
| skip_install_trust | ||
| admin localhost:2999 | ||
| http_port `+testPort+` | ||
| metrics | ||
|
|
||
| frankenphp { | ||
| num_threads 1 | ||
| max_threads 1 | ||
| max_wait_time 5s | ||
| } | ||
| } | ||
|
|
||
| localhost:`+testPort+` { | ||
| route { | ||
| root ../testdata | ||
| rewrite sleep.php | ||
| php | ||
| } | ||
| } | ||
| `) | ||
| } | ||
|
|
||
| func TestQueuedRequestSurvivesReload_worker(t *testing.T) { | ||
| testQueuedRequestSurvivesReload(t, ` | ||
| { | ||
| skip_install_trust | ||
| admin localhost:2999 | ||
| http_port `+testPort+` | ||
| metrics | ||
|
|
||
| frankenphp { | ||
| num_threads 1 | ||
| max_threads 2 | ||
| max_wait_time 5s | ||
| worker ../testdata/sleep.php 1 | ||
| } | ||
| } | ||
|
|
||
| localhost:`+testPort+` { | ||
| route { | ||
| root ../testdata | ||
| rewrite sleep.php | ||
| php | ||
| } | ||
| } | ||
| `) | ||
| } | ||
|
|
||
| func testQueuedRequestSurvivesReload(t *testing.T, config string) { | ||
| tester := caddytest.NewTester(t) | ||
| initServer(t, tester, config, "caddyfile") | ||
|
|
||
| client := &http.Client{Timeout: 30 * time.Second} | ||
| baseURL := "http://localhost:" + testPort + "/sleep.php" | ||
| workerMode := strings.Contains(config, "worker ../testdata/sleep.php") | ||
|
|
||
| var wg sync.WaitGroup | ||
| done := make(chan struct{}) | ||
|
|
||
| doRequest := func(query string) { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| req, err := http.NewRequest(http.MethodGet, baseURL+"?"+query, nil) | ||
| require.NoError(t, err) | ||
| resp, err := client.Do(req) | ||
| require.NoError(t, err, "request must not be rejected") | ||
| require.NoError(t, resp.Body.Close()) | ||
| require.Equal(t, http.StatusOK, resp.StatusCode, "request must not be rejected") | ||
| }() | ||
| } | ||
|
|
||
| // Occupy the only PHP thread so the next request has to wait in the queue. | ||
| doRequest("sleep=10") | ||
| doRequest("sleep=10") | ||
|
|
||
| maxTries := 2000 | ||
| queued := false | ||
| for i := 0; i <= maxTries; i++ { | ||
| depth, err := requestQueueDepth(workerMode) | ||
| require.NoError(t, err) | ||
| if depth > 0 { | ||
| queued = true | ||
| break | ||
| } | ||
|
|
||
| doRequest("sleep=" + strconv.Itoa(i)) | ||
| time.Sleep(time.Millisecond) | ||
| if !assert.NotEqual(t, i, maxTries, "request was never queued") { | ||
| return | ||
| } | ||
| } | ||
| require.True(t, queued) | ||
|
|
||
| // Reload while the request is queued, exactly like the Caddy module does. | ||
| reloadCaddyConfig(t, config) | ||
|
|
||
| go func() { | ||
| wg.Wait() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(15 * time.Second): | ||
| t.Fatal("the queued request never completed after the reload") | ||
| } | ||
| } | ||
|
|
||
| func TestQueuedRequestRejectedWhenFrankenPHPConfigChanges(t *testing.T) { | ||
| memoryLimit := 5000000 // ~5MB | ||
| initial := ` | ||
| { | ||
| skip_install_trust | ||
| admin localhost:2999 | ||
| http_port ` + testPort + ` | ||
| metrics | ||
|
|
||
| frankenphp { | ||
| num_threads 1 | ||
| max_threads 1 | ||
| php_ini memory_limit ` + strconv.Itoa(memoryLimit) + ` | ||
| } | ||
| } | ||
|
|
||
| localhost:` + testPort + ` { | ||
| route { | ||
| root ../testdata | ||
| rewrite sleep.php | ||
| php | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| tester := caddytest.NewTester(t) | ||
| initServer(t, tester, initial, "caddyfile") | ||
|
|
||
| client := &http.Client{Timeout: 30 * time.Second} | ||
| baseURL := "http://localhost:" + testPort + "/sleep.php" | ||
| requestWasRejected := atomic.Bool{} | ||
|
|
||
| doRequest := func(query string) { | ||
| go func() { | ||
| req, err := http.NewRequest(http.MethodGet, baseURL+"?"+query, nil) | ||
| require.NoError(t, err) | ||
| resp, err := client.Do(req) | ||
| require.NoError(t, err, "request must not be rejected") | ||
| require.NoError(t, resp.Body.Close()) | ||
| if resp.StatusCode != http.StatusOK { | ||
| requestWasRejected.Store(true) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| sleepMilliseconds := 0 | ||
| require.Eventually(t, func() bool { | ||
| sleepMilliseconds++ | ||
| changedCaddyConfig := strings.Replace(initial, strconv.Itoa(memoryLimit), strconv.Itoa(memoryLimit+sleepMilliseconds), 1) | ||
| doRequest("sleep=" + strconv.Itoa(sleepMilliseconds)) | ||
| doRequest("sleep=" + strconv.Itoa(sleepMilliseconds)) | ||
| reloadCaddyConfig(t, changedCaddyConfig) | ||
| return requestWasRejected.Load() | ||
| }, 10*time.Second, time.Millisecond, "no queued request was ever rejected") | ||
| } | ||
|
|
||
| func reloadCaddyConfig(t *testing.T, config string) { | ||
| t.Helper() | ||
|
|
||
| r, err := http.NewRequest(http.MethodPost, "http://localhost:2999/load?force=true", bytes.NewBufferString(config)) | ||
| require.NoError(t, err) | ||
| r.Header.Set("Content-Type", "text/caddyfile") | ||
| r.Header.Set("Cache-Control", "must-revalidate") | ||
|
|
||
| resp, err := http.DefaultClient.Do(r) | ||
| require.NoError(t, err) | ||
| body, err := io.ReadAll(resp.Body) | ||
| require.NoError(t, err) | ||
| require.NoError(t, resp.Body.Close()) | ||
| require.Equal(t, http.StatusOK, resp.StatusCode, "reload failed: %s", body) | ||
| } | ||
|
|
||
| func requestQueueDepth(workerMode bool) (float64, error) { | ||
| resp, err := http.Get("http://localhost:2999/metrics") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| _ = resp.Body.Close() | ||
| return 0, err | ||
| } | ||
| if err := resp.Body.Close(); err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| text := string(body) | ||
| if workerMode { | ||
| return sumPrometheusGauge(text, "frankenphp_worker_queue_depth"), nil | ||
| } | ||
|
|
||
| return prometheusGaugeValue(text, "frankenphp_queue_depth"), nil | ||
| } | ||
|
|
||
| func prometheusGaugeValue(body, name string) float64 { | ||
| for line := range strings.SplitSeq(body, "\n") { | ||
| if strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| if strings.HasPrefix(line, name+" ") { | ||
| fields := strings.Fields(line) | ||
| if len(fields) < 2 { | ||
| return 0 | ||
| } | ||
| v, err := strconv.ParseFloat(fields[1], 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| return v | ||
| } | ||
| } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| func sumPrometheusGauge(body, prefix string) float64 { | ||
| var total float64 | ||
| for line := range strings.SplitSeq(body, "\n") { | ||
| if strings.HasPrefix(line, "#") || !strings.HasPrefix(line, prefix) { | ||
| continue | ||
| } | ||
| fields := strings.Fields(line) | ||
| if len(fields) < 2 { | ||
| continue | ||
| } | ||
| v, err := strconv.ParseFloat(fields[len(fields)-1], 64) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| total += v | ||
| } | ||
|
|
||
| return total | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this happens during provision, I don't think caddy modules are all registered at this time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait, why doesn't this set hasChanged to true?