Skip to content

Commit 3bd2d20

Browse files
committed
fix: flush
1 parent 5442134 commit 3bd2d20

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
@@ -865,14 +865,15 @@ func TestMaxWaitTimeWorker(t *testing.T) {
865865
{
866866
skip_install_trust
867867
admin localhost:2999
868-
metrics
869868
http_port `+testPort+`
869+
metrics
870870
871871
frankenphp {
872+
num_threads 2
872873
max_wait_time 1ns
873874
worker {
874-
name service
875875
num 1
876+
name service
876877
file ../testdata/sleep.php
877878
}
878879
}
@@ -893,24 +894,31 @@ func TestMaxWaitTimeWorker(t *testing.T) {
893894
wg.Add(10)
894895
for i := 0; i < 10; i++ {
895896
go func() {
896-
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=100", t)
897-
fmt.Printf("Status code: %d\n", statusCode)
897+
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=10000&iteration=1", t)
898898
if statusCode == http.StatusGatewayTimeout {
899899
success.Store(true)
900900
}
901901
wg.Done()
902902
}()
903903
}
904904
wg.Wait()
905-
906905
require.True(t, success.Load(), "At least one request should have failed with a 504 Gateway Timeout status")
906+
907+
// Fetch metrics
908+
resp, err := http.Get("http://localhost:2999/metrics")
909+
require.NoError(t, err, "failed to fetch metrics")
910+
defer resp.Body.Close()
911+
912+
// Read and parse metrics
913+
metrics := new(bytes.Buffer)
914+
_, err = metrics.ReadFrom(resp.Body)
915+
907916
expectedMetrics := `
908917
# TYPE frankenphp_worker_queue_depth gauge
909-
frankenphp_worker_queue_depth{worker="service"} 0
918+
frankenphp_worker_queue_depth{worker="service"} 9
910919
`
911920

912921
ctx := caddy.ActiveContext()
913-
fmt.Printf("Metrics: %d\n", testutil.CollectAndCount(ctx.GetMetricsRegistry(), "frankenphp_worker_queue_depth"))
914922
require.NoError(t,
915923
testutil.GatherAndCompare(
916924
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
@@ -217,6 +217,7 @@ func (worker *worker) handleRequest(fc *frankenPHPContext) {
217217
case scaleChan <- fc:
218218
// the request has triggered scaling, continue to wait for a thread
219219
case <-timeoutChan(maxWaitTime):
220+
// metrics.DequeuedWorkerRequest(worker.name)
220221
// the request has timed out stalling
221222
fc.reject(504, "Gateway Timeout")
222223
return

0 commit comments

Comments
 (0)