Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
cache: true

- name: Run Go Tests
run: go test -coverprofile=coverage.out ./...
run: go test -race -coverprofile=coverage.out ./...
lint:
runs-on: ubuntu-latest
steps:
Expand Down
17 changes: 6 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"hash/maphash"
"io"
"log"
"log/slog"
Expand All @@ -21,8 +20,7 @@ import (

var capabilities *CapabilitiesSpec
var dynamicEndpointMutex = sync.RWMutex{}
var dynamicEndpoints = map[uint64]reflectionSpec{}
var dynamicEndpointsHash = maphash.Hash{}
var dynamicEndpoints = map[string]reflectionSpec{}

//go:embed capabilities.yaml
var capabilitiesDescription []byte
Expand Down Expand Up @@ -172,9 +170,9 @@ func handleConfigureReflection(w http.ResponseWriter, r *http.Request) {

func handleReset(w http.ResponseWriter, r *http.Request) {
slog.Info("Received reset request. Discarding all endpoint configurations now")
for k := range dynamicEndpoints {
delete(dynamicEndpoints, k)
}
dynamicEndpointMutex.Lock()
defer dynamicEndpointMutex.Unlock()
clear(dynamicEndpoints)
}

func handleInspect(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -234,11 +232,8 @@ func decodeBody(spec *reflectionSpec) (string, error) {
return string(bodyBytes), nil
}

func computeEndpointKey(method string, url string) uint64 {
dynamicEndpointsHash.Reset()
dynamicEndpointsHash.WriteString(method)
dynamicEndpointsHash.WriteString(url)
return dynamicEndpointsHash.Sum64()
func computeEndpointKey(method string, url string) string {
return method + " " + url
}

func doReflect(w http.ResponseWriter, r *http.Request, spec *reflectionSpec) {
Expand Down
40 changes: 40 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -555,3 +556,42 @@ func (s *serverTestSuite) TestInspect_LogContent() {
s.Contains(logContent, "request.body.length.value=0")
s.Contains(logContent, "request.body.length.unit=B")
}

// Endpoint configuration state is package-global; concurrent requests must not race on it.
func (s *serverTestSuite) TestConcurrentConfigurationAccess() {
server := httptest.NewServer(Handler())
s.T().Cleanup(server.Close)
s.T().Cleanup(func() { clear(dynamicEndpoints) })

client := http.Client{}
spec := `{"endpoints":[{"method":"GET","url":"/concurrent"}],"status":201}`

wg := sync.WaitGroup{}
for i := 0; i < 20; i++ {
wg.Add(3)
go func() {
defer wg.Done()
response, err := client.Post(server.URL+"/configure_reflection", "application/json", strings.NewReader(spec))
if s.NoError(err) {
s.NoError(response.Body.Close())
}
}()
go func() {
defer wg.Done()
response, err := client.Get(server.URL + "/concurrent")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the error be returned here? 404, since reset was called before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No — that path never 404s. Everything unmatched falls through to handleDefault on /, which is the black hole: it writes an empty 200. I verified it against a live Handler() with /concurrent unconfigured and got 200.

So the only two outcomes here are 201 while the configuration is live and 200 once /reset has discarded it, depending on how the goroutines interleave. The test was ignoring the status entirely, which is what made the question reasonable — it now asserts the response is one of those two, so a genuine 404 or 500 regression would fail it. Pushed as a follow-up commit.

if s.NoError(err) {
s.NoError(response.Body.Close())
}
}()
go func() {
defer wg.Done()
request, err := http.NewRequest(http.MethodPut, server.URL+"/reset", nil)
s.Require().NoError(err)
response, err := client.Do(request)
if s.NoError(err) {
s.NoError(response.Body.Close())
}
}()
}
wg.Wait()
}