Skip to content

Commit a8eaa04

Browse files
committed
fix: flush
1 parent 2307bed commit a8eaa04

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

caddy/caddy_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -973,14 +973,15 @@ func TestMaxWaitTimeWorker(t *testing.T) {
973973
{
974974
skip_install_trust
975975
admin localhost:2999
976-
metrics
977976
http_port `+testPort+`
977+
metrics
978978
979979
frankenphp {
980+
num_threads 2
980981
max_wait_time 1ns
981982
worker {
982-
name service
983983
num 1
984+
name service
984985
file ../testdata/sleep.php
985986
}
986987
}
@@ -1001,24 +1002,31 @@ func TestMaxWaitTimeWorker(t *testing.T) {
10011002
wg.Add(10)
10021003
for i := 0; i < 10; i++ {
10031004
go func() {
1004-
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=100", t)
1005-
fmt.Printf("Status code: %d\n", statusCode)
1005+
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=10000&iteration=1", t)
10061006
if statusCode == http.StatusGatewayTimeout {
10071007
success.Store(true)
10081008
}
10091009
wg.Done()
10101010
}()
10111011
}
10121012
wg.Wait()
1013-
10141013
require.True(t, success.Load(), "At least one request should have failed with a 504 Gateway Timeout status")
1014+
1015+
// Fetch metrics
1016+
resp, err := http.Get("http://localhost:2999/metrics")
1017+
require.NoError(t, err, "failed to fetch metrics")
1018+
defer resp.Body.Close()
1019+
1020+
// Read and parse metrics
1021+
metrics := new(bytes.Buffer)
1022+
_, err = metrics.ReadFrom(resp.Body)
1023+
10151024
expectedMetrics := `
10161025
# TYPE frankenphp_worker_queue_depth gauge
1017-
frankenphp_worker_queue_depth{worker="service"} 0
1026+
frankenphp_worker_queue_depth{worker="service"} 9
10181027
`
10191028

10201029
ctx := caddy.ActiveContext()
1021-
fmt.Printf("Metrics: %d\n", testutil.CollectAndCount(ctx.GetMetricsRegistry(), "frankenphp_worker_queue_depth"))
10221030
require.NoError(t,
10231031
testutil.GatherAndCompare(
10241032
ctx.GetMetricsRegistry(),

context.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ func (fc *frankenPHPContext) reject(statusCode int, message string) {
152152
if rw != nil {
153153
rw.WriteHeader(statusCode)
154154
_, _ = rw.Write([]byte(message))
155-
rw.(http.Flusher).Flush()
155+
156+
if f, ok := rw.(http.Flusher); ok {
157+
f.Flush()
158+
}
156159
}
157160

158161
fc.closeContext()

worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (worker *worker) handleRequest(fc *frankenPHPContext) {
225225
case scaleChan <- fc:
226226
// the request has triggered scaling, continue to wait for a thread
227227
case <-timeoutChan(maxWaitTime):
228+
// metrics.DequeuedWorkerRequest(worker.name)
228229
// the request has timed out stalling
229230
fc.reject(504, "Gateway Timeout")
230231
return

0 commit comments

Comments
 (0)