Browse Source

api: improve performance by using RWMutex (#2968)

pull/2978/head
Alessandro Ros 1 year ago committed by GitHub
parent
commit
320f5a7a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      internal/api/api.go

18
internal/api/api.go

@ -167,7 +167,7 @@ type API struct {
Parent apiParent Parent apiParent
httpServer *httpserv.WrappedServer httpServer *httpserv.WrappedServer
mutex sync.Mutex mutex sync.RWMutex
} }
// Initialize initializes API. // 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) { func (a *API) onConfigGlobalGet(ctx *gin.Context) {
a.mutex.Lock() a.mutex.RLock()
c := a.Conf c := a.Conf
a.mutex.Unlock() a.mutex.RUnlock()
ctx.JSON(http.StatusOK, c.Global()) ctx.JSON(http.StatusOK, c.Global())
} }
@ -319,9 +319,9 @@ func (a *API) onConfigGlobalPatch(ctx *gin.Context) {
} }
func (a *API) onConfigPathDefaultsGet(ctx *gin.Context) { func (a *API) onConfigPathDefaultsGet(ctx *gin.Context) {
a.mutex.Lock() a.mutex.RLock()
c := a.Conf c := a.Conf
a.mutex.Unlock() a.mutex.RUnlock()
ctx.JSON(http.StatusOK, c.PathDefaults) ctx.JSON(http.StatusOK, c.PathDefaults)
} }
@ -354,9 +354,9 @@ func (a *API) onConfigPathDefaultsPatch(ctx *gin.Context) {
} }
func (a *API) onConfigPathsList(ctx *gin.Context) { func (a *API) onConfigPathsList(ctx *gin.Context) {
a.mutex.Lock() a.mutex.RLock()
c := a.Conf c := a.Conf
a.mutex.Unlock() a.mutex.RUnlock()
data := &defs.APIPathConfList{ data := &defs.APIPathConfList{
Items: make([]*conf.Path, len(c.Paths)), Items: make([]*conf.Path, len(c.Paths)),
@ -384,9 +384,9 @@ func (a *API) onConfigPathsGet(ctx *gin.Context) {
return return
} }
a.mutex.Lock() a.mutex.RLock()
c := a.Conf c := a.Conf
a.mutex.Unlock() a.mutex.RUnlock()
p, ok := c.Paths[name] p, ok := c.Paths[name]
if !ok { if !ok {

Loading…
Cancel
Save