Skip to content

Commit

Permalink
Add mutext around render.Respond override
Browse files Browse the repository at this point in the history
- This is an attempt to fix a race condition which only occurs in CI
  • Loading branch information
calvinmclean committed Dec 9, 2023
1 parent f0c4c8b commit d36deac
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"errors"
"fmt"
"net/http"
"sync"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)

var respondMtx sync.Mutex

// HTMLer allows for easily represending reponses as HTML strings when accepted content
// type is text/html
type HTMLer interface {
Expand All @@ -17,6 +20,7 @@ type HTMLer interface {

// Create API routes on the given router
func (a *API[T]) Route(r chi.Router) {
respondMtx.Lock()
render.Respond = func(w http.ResponseWriter, r *http.Request, v interface{}) {
if render.GetAcceptedContentType(r) == render.ContentTypeHTML {
htmler, ok := v.(HTMLer)
Expand All @@ -28,6 +32,7 @@ func (a *API[T]) Route(r chi.Router) {

render.DefaultResponder(w, r, v)
}
respondMtx.Unlock()

r.Route(a.base, func(r chi.Router) {
// Only set these middleware for root-level API
Expand Down

0 comments on commit d36deac

Please sign in to comment.