Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions services/proxy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"

"github.com/compose-spec/compose-go/template"
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions services/proxy/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading