From 74cc88540d3f92892a666c4d002dad5d4bac4f5f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 21 Mar 2024 16:53:48 -0400 Subject: [PATCH] fix: add rw mutex lock to getHandler --- api/router/router.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/router/router.go b/api/router/router.go index 4a5b790..e73b1ce 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -2,6 +2,7 @@ package router import ( "net/http" + "sync" "git.lumeweb.com/LumeWeb/portal/config" @@ -25,6 +26,7 @@ type APIRouter struct { apiHandlers map[string]http.Handler logger *zap.Logger config *config.Manager + mutex *sync.RWMutex } // Implement the ServeHTTP method on our new type @@ -59,6 +61,8 @@ func (hs *APIRouter) getHandlerByDomain(domain string) http.Handler { } func (hs *APIRouter) getHandler(protocol string) http.Handler { + hs.mutex.RLock() + defer hs.mutex.RUnlock() if handler := hs.apiHandlers[protocol]; handler == nil { if proto := hs.apis[protocol]; proto == nil { hs.logger.Fatal("Protocol not found", zap.String("protocol", protocol)) @@ -72,6 +76,8 @@ func (hs *APIRouter) getHandler(protocol string) http.Handler { return nil } + hs.mutex.Lock() + defer hs.mutex.Unlock() hs.apiHandlers[protocol] = routes } @@ -83,6 +89,7 @@ func NewAPIRouter() *APIRouter { apis: make(map[string]RoutableAPI), apiHandlers: make(map[string]http.Handler), apiDomain: make(map[string]string), + mutex: &sync.RWMutex{}, } }