diff --git a/services/proxy/manager.go b/services/proxy/manager.go index 5ee283b4..420fb59c 100644 --- a/services/proxy/manager.go +++ b/services/proxy/manager.go @@ -20,6 +20,7 @@ import ( "strconv" "strings" "sync/atomic" + "syscall" "time" "github.com/compose-spec/compose-go/template" @@ -1669,12 +1670,21 @@ func replaceObjectByID(objects []interface{}, id string, replacement interface{} } func (m *DefaultManager) request(method, url string, body []byte) (*http.Response, error) { - request, err := http.NewRequest(method, url, bytes.NewReader(body)) - if err != nil { - return nil, err + for attempt := 1; ; attempt++ { + request, err := http.NewRequest(method, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + request.Header.Set("Content-Type", "application/json") + response, err := m.http.Do(request) + // Each configuration change replaces Caddy's admin server, and the old + // server can close a connection before it reads the request on it. + dropped := errors.Is(err, io.EOF) || errors.Is(err, syscall.ECONNRESET) + if !dropped || attempt == 2 { + return response, err + } + time.Sleep(100 * time.Millisecond) } - request.Header.Set("Content-Type", "application/json") - return m.http.Do(request) } func (m *DefaultManager) alias(service string) string { diff --git a/services/proxy/manager_test.go b/services/proxy/manager_test.go index 6bcf25aa..fcd2773e 100644 --- a/services/proxy/manager_test.go +++ b/services/proxy/manager_test.go @@ -935,6 +935,44 @@ func TestRegisterRouteCreatesServerAfterNotFound(t *testing.T) { } } +func TestRegisterRouteSucceedsWhenAdminClosesConnectionAfterChange(t *testing.T) { + serverCreated := false + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + switch { + case request.Method == http.MethodPost && strings.HasSuffix(request.URL.Path, "/kool-80"): + serverCreated = true + // Caddy can answer a configuration change without "Connection: close" + // and then close the connection without answering the next request. + connection, buffered, err := response.(http.Hijacker).Hijack() + if err != nil { + t.Error(err) + return + } + defer func() { _ = connection.Close() }() + _, _ = buffered.WriteString("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") + _ = buffered.Flush() + if next, err := http.ReadRequest(buffered.Reader); err == nil { + _, _ = io.Copy(io.Discard, next.Body) + } + case request.Method == http.MethodGet && strings.HasSuffix(request.URL.Path, "/routes") && serverCreated: + _, _ = response.Write([]byte(`[]`)) + case request.Method == http.MethodGet: + response.WriteHeader(http.StatusNotFound) + } + })) + defer server.Close() + + env := environment.NewFakeEnvStorage() + env.Set("KOOL_NAME", "example") + env.Set("KOOL_PROXY_HOST", "app.localhost") + manager := NewManager(&shell.FakeShell{}, env).(*DefaultManager) + manager.adminURL = server.URL + + if err := manager.register(route{Service: "app", Listen: 80, Target: 8080, Hosts: []string{"@"}}); err != nil { + t.Fatal(err) + } +} + func TestRegisterTLS(t *testing.T) { var policy map[string]interface{} server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {