refactor: rewrite getHandler
This commit is contained in:
parent
74cc88540d
commit
d06f436fa1
|
@ -62,26 +62,35 @@ func (hs *APIRouter) getHandlerByDomain(domain string) http.Handler {
|
||||||
|
|
||||||
func (hs *APIRouter) getHandler(protocol string) http.Handler {
|
func (hs *APIRouter) getHandler(protocol string) http.Handler {
|
||||||
hs.mutex.RLock()
|
hs.mutex.RLock()
|
||||||
defer hs.mutex.RUnlock()
|
handler, ok := hs.apiHandlers[protocol]
|
||||||
if handler := hs.apiHandlers[protocol]; handler == nil {
|
hs.mutex.RUnlock()
|
||||||
if proto := hs.apis[protocol]; proto == nil {
|
|
||||||
|
if ok {
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
hs.logger.Fatal("Protocol not found", zap.String("protocol", protocol))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
routes, err := hs.apis[protocol].Routes()
|
routes, err := proto.Routes()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hs.logger.Fatal("Error getting routes", zap.Error(err))
|
hs.logger.Fatal("Error getting routes", zap.Error(err))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
hs.mutex.Lock()
|
|
||||||
defer hs.mutex.Unlock()
|
|
||||||
hs.apiHandlers[protocol] = routes
|
hs.apiHandlers[protocol] = routes
|
||||||
}
|
return routes
|
||||||
|
|
||||||
return hs.apiHandlers[protocol]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPIRouter() *APIRouter {
|
func NewAPIRouter() *APIRouter {
|
||||||
|
|
Loading…
Reference in New Issue