Skip to content

Commit

Permalink
Fixing race conditions (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski authored Dec 12, 2024
1 parent 0e80941 commit ace2b4f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions quesma/frontend_connectors/basic_http_frontend_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"io"
"net/http"
quesma_api "quesma_v2/core"
"sync"
)

type BasicHTTPFrontendConnector struct {
listener *http.Server
router quesma_api.Router

listener *http.Server
router quesma_api.Router
mutex sync.Mutex
responseMutator func(w http.ResponseWriter) http.ResponseWriter
endpoint string
}
Expand Down Expand Up @@ -80,6 +81,12 @@ func getMatchingHandler(requestPath string, handlers map[string]quesma_api.Handl
}

func (h *BasicHTTPFrontendConnector) Listen() error {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.listener != nil {
// TODO handle this gracefully and return correct error
return nil
}
h.listener = &http.Server{}
h.listener.Addr = h.endpoint
h.listener.Handler = h
Expand All @@ -93,6 +100,8 @@ func (h *BasicHTTPFrontendConnector) Listen() error {
}

func (h *BasicHTTPFrontendConnector) Stop(ctx context.Context) error {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.listener == nil {
return nil
}
Expand Down

0 comments on commit ace2b4f

Please sign in to comment.