lock update requests

This commit is contained in:
Frederik Löffert 2019-05-21 16:06:49 +02:00
parent 1449d6902c
commit f7aea048d3
2 changed files with 8 additions and 0 deletions

View File

@ -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

View File

@ -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),
}