caddyconfig: cancel HTTP loader requests with context (#7918)

pull/7925/head^2
cui fliter 2026-08-10 13:12:47 +08:00 committed by GitHub
parent 64b64c61eb
commit 7d26d34ec6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 1 deletions

View File

@ -95,7 +95,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
}
url := repl.ReplaceAll(hl.URL, "")
req, err := http.NewRequest(method, url, nil)
req, err := http.NewRequestWithContext(ctx, method, url, nil)
if err != nil {
return nil, err
}

View File

@ -0,0 +1,76 @@
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package caddyconfig
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
)
func TestHTTPLoaderLoadConfigRespectsContextCancellation(t *testing.T) {
requestStarted := make(chan struct{})
requestCanceled := make(chan struct{})
stopServer := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(requestStarted)
select {
case <-r.Context().Done():
close(requestCanceled)
case <-stopServer:
}
}))
t.Cleanup(func() {
close(stopServer)
srv.Close()
})
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
loader := HTTPLoader{URL: srv.URL}
errCh := make(chan error, 1)
go func() {
_, err := loader.LoadConfig(ctx)
errCh <- err
}()
select {
case <-requestStarted:
case <-time.After(time.Second):
t.Fatal("request did not reach test server")
}
cancel()
select {
case err := <-errCh:
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context cancellation error, got %v", err)
}
case <-time.After(time.Second):
t.Fatal("LoadConfig did not return after context cancellation")
}
select {
case <-requestCanceled:
case <-time.After(time.Second):
t.Fatal("request context was not canceled")
}
}