From d06f436fa1ecafb0fdd84299e12c4a0b99496b98 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 21 Mar 2024 16:56:29 -0400 Subject: [PATCH] refactor: rewrite getHandler --- api/router/router.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/api/router/router.go b/api/router/router.go index e73b1ce..7b6d364 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -62,26 +62,35 @@ 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)) - return nil - } + handler, ok := hs.apiHandlers[protocol] + hs.mutex.RUnlock() - routes, err := hs.apis[protocol].Routes() - - if err != nil { - hs.logger.Fatal("Error getting routes", zap.Error(err)) - return nil - } - - hs.mutex.Lock() - defer hs.mutex.Unlock() - hs.apiHandlers[protocol] = routes + if ok { + return handler } - return hs.apiHandlers[protocol] + hs.mutex.Lock() + defer hs.mutex.Unlock() + + // Double-check if the handler was created while acquiring the write lock + if handler, ok := hs.apiHandlers[protocol]; ok { + return handler + } + + proto, ok := hs.apis[protocol] + if !ok { + hs.logger.Fatal("Protocol not found", zap.String("protocol", protocol)) + return nil + } + + routes, err := proto.Routes() + if err != nil { + hs.logger.Fatal("Error getting routes", zap.Error(err)) + return nil + } + + hs.apiHandlers[protocol] = routes + return routes } func NewAPIRouter() *APIRouter {