From f7aea048d36a5609818424473bfac45a95575d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20L=C3=B6ffert?= Date: Tue, 21 May 2019 16:06:49 +0200 Subject: [PATCH] lock update requests --- repo/loader.go | 5 +++++ repo/repo.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/repo/loader.go b/repo/loader.go index 0aa522a..dbb7d84 100644 --- a/repo/loader.go +++ b/repo/loader.go @@ -130,6 +130,11 @@ func get(URL string) (data []byte, err error) { } func (repo *Repo) update() (repoRuntime int64, jsonBytes []byte, err error) { + + // limit ressources and allow only one update request at once + repo.updateLock.Lock() + defer repo.updateLock.Unlock() + startTimeRepo := time.Now().UnixNano() jsonBytes, err = get(repo.server) repoRuntime = time.Now().UnixNano() - startTimeRepo diff --git a/repo/repo.go b/repo/repo.go index 564b84c..b2cb75b 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "sync" "time" "github.com/foomo/contentserver/content" @@ -25,6 +26,7 @@ type Dimension struct { type Repo struct { server string Directory map[string]*Dimension + updateLock *sync.Mutex updateChannel chan *repoDimension updateDoneChannel chan error history *history @@ -43,6 +45,7 @@ func NewRepo(server string, varDir string) *Repo { server: server, Directory: map[string]*Dimension{}, history: newHistory(varDir), + updateLock: &sync.Mutex{}, updateChannel: make(chan *repoDimension), updateDoneChannel: make(chan error), }