From 320f5a7a213e9446d75adecd4b31275f0132b84d Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Wed, 31 Jan 2024 22:41:37 +0100 Subject: [PATCH] api: improve performance by using RWMutex (#2968) --- internal/api/api.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index ee47c122..a8020b82 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -167,7 +167,7 @@ type API struct { Parent apiParent httpServer *httpserv.WrappedServer - mutex sync.Mutex + mutex sync.RWMutex } // Initialize initializes API. @@ -281,9 +281,9 @@ func (a *API) writeError(ctx *gin.Context, status int, err error) { } func (a *API) onConfigGlobalGet(ctx *gin.Context) { - a.mutex.Lock() + a.mutex.RLock() c := a.Conf - a.mutex.Unlock() + a.mutex.RUnlock() ctx.JSON(http.StatusOK, c.Global()) } @@ -319,9 +319,9 @@ func (a *API) onConfigGlobalPatch(ctx *gin.Context) { } func (a *API) onConfigPathDefaultsGet(ctx *gin.Context) { - a.mutex.Lock() + a.mutex.RLock() c := a.Conf - a.mutex.Unlock() + a.mutex.RUnlock() ctx.JSON(http.StatusOK, c.PathDefaults) } @@ -354,9 +354,9 @@ func (a *API) onConfigPathDefaultsPatch(ctx *gin.Context) { } func (a *API) onConfigPathsList(ctx *gin.Context) { - a.mutex.Lock() + a.mutex.RLock() c := a.Conf - a.mutex.Unlock() + a.mutex.RUnlock() data := &defs.APIPathConfList{ Items: make([]*conf.Path, len(c.Paths)), @@ -384,9 +384,9 @@ func (a *API) onConfigPathsGet(ctx *gin.Context) { return } - a.mutex.Lock() + a.mutex.RLock() c := a.Conf - a.mutex.Unlock() + a.mutex.RUnlock() p, ok := c.Paths[name] if !ok {