Makes the app without global lock

This commit is contained in:
2016-01-25 13:23:39 +01:00
parent 575655b6a4
commit 624b68616e
2 changed files with 14 additions and 43 deletions

45
main.go
View File

@@ -37,22 +37,9 @@ type appData struct {
opts Options opts Options
errors chan error errors chan error
dbLock chan bool
wg sync.WaitGroup wg sync.WaitGroup
} }
// type albumOperationType int
// const (
// OpAdd albumOperationType = iota
// OpDelete
// )
// type albumOperation struct {
// Type albumOperationType
// A *Album
// }
func newAppData(opts Options) (*appData, error) { func newAppData(opts Options) (*appData, error) {
if opts.BatchSize <= 0 { if opts.BatchSize <= 0 {
return nil, fmt.Errorf("Invalid --batch-size of %d, need to be >0", opts.BatchSize) return nil, fmt.Errorf("Invalid --batch-size of %d, need to be >0", opts.BatchSize)
@@ -66,7 +53,6 @@ func newAppData(opts Options) (*appData, error) {
res := &appData{ res := &appData{
opts: opts, opts: opts,
errors: make(chan error, 10), errors: make(chan error, 10),
dbLock: make(chan bool, 1),
} }
blevePath := filepath.Join(basepath, "index") blevePath := filepath.Join(basepath, "index")
@@ -227,9 +213,7 @@ func (a *appData) cacheAlbumDescription(getAlbum <-chan *Album, toIndex chan<- *
nbAlbums := 0 nbAlbums := 0
for album := range getAlbum { for album := range getAlbum {
nbAlbums++ nbAlbums++
a.dbLock <- true
albumCache, err := a.db.Get(album.ID) albumCache, err := a.db.Get(album.ID)
<-a.dbLock
if err == nil { if err == nil {
toIndex <- albumCache toIndex <- albumCache
@@ -239,12 +223,9 @@ func (a *appData) cacheAlbumDescription(getAlbum <-chan *Album, toIndex chan<- *
err = a.getter.Get(album) err = a.getter.Get(album)
if err != nil { if err != nil {
a.errors <- fmt.Errorf("[CACHE ALBUMS]: getting %d :%s", album.ID, err) a.errors <- fmt.Errorf("[CACHE ALBUMS]: getting %d :%s", album.ID, err)
<-a.dbLock
continue continue
} }
a.dbLock <- true
err = a.db.AddOrUpdate(album) err = a.db.AddOrUpdate(album)
<-a.dbLock
if err != nil { if err != nil {
a.errors <- fmt.Errorf("[CACHE ALBUMS]: storing %d: %s", album.ID, err) a.errors <- fmt.Errorf("[CACHE ALBUMS]: storing %d: %s", album.ID, err)
continue continue
@@ -264,26 +245,13 @@ func (a *appData) maintainAlbumDatabase(stopChan <-chan struct{},
select { select {
case <-stopChan: case <-stopChan:
return return
case aID, ok := <-deleteAlbum:
if ok == false {
deleteAlbum = nil
break
}
a.dbLock <- true
err := a.db.Delete(aID)
<-a.dbLock
if err != nil {
a.errors <- fmt.Errorf("[DB]: delete %d: %s", aID, err)
}
case aID, ok := <-checkAlbum: case aID, ok := <-checkAlbum:
if ok == false { if ok == false {
checkAlbum = nil checkAlbum = nil
break break
} }
a.dbLock <- true
cachedAlbum, err := a.db.Get(aID) cachedAlbum, err := a.db.Get(aID)
<-a.dbLock
if err != nil { if err != nil {
a.errors <- fmt.Errorf("[DB]: check %d: %s", aID, err) a.errors <- fmt.Errorf("[DB]: check %d: %s", aID, err)
@@ -303,14 +271,21 @@ func (a *appData) maintainAlbumDatabase(stopChan <-chan struct{},
} }
// re-lock the db // re-lock the db
a.dbLock <- true
err = a.db.AddOrUpdate(cachedAlbum) err = a.db.AddOrUpdate(cachedAlbum)
<-a.dbLock
if err != nil { if err != nil {
a.errors <- fmt.Errorf("[DB]: could not add new %d: %s", aID, err) a.errors <- fmt.Errorf("[DB]: could not add new %d: %s", aID, err)
continue continue
} }
updateIndex <- cachedAlbum updateIndex <- cachedAlbum
case aID, ok := <-deleteAlbum:
if ok == false {
deleteAlbum = nil
break
}
err := a.db.Delete(aID)
if err != nil {
a.errors <- fmt.Errorf("[DB]: delete %d: %s", aID, err)
}
} }
} }
} }
@@ -327,9 +302,7 @@ func (a *appData) updateCache(stopChan <-chan struct{}, periode time.Duration, c
return return
case <-ticker.C: case <-ticker.C:
//we just chek //we just chek
a.dbLock <- true
albums, err := a.db.ByPurchaseDate() albums, err := a.db.ByPurchaseDate()
<-a.dbLock
if err != nil { if err != nil {
a.errors <- fmt.Errorf("[UPDATER]: could not get albums: %s", err) a.errors <- fmt.Errorf("[UPDATER]: could not get albums: %s", err)
continue continue

View File

@@ -7,6 +7,7 @@ import (
"path" "path"
"regexp" "regexp"
"strconv" "strconv"
"strings"
bleve_http "github.com/blevesearch/bleve/http" bleve_http "github.com/blevesearch/bleve/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@@ -23,9 +24,7 @@ func (a *appData) buildRouter() http.Handler {
router.Handle("/api/search", searchHandler).Methods("POST") router.Handle("/api/search", searchHandler).Methods("POST")
router.Handle("/api/recents", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { router.Handle("/api/recents", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.dbLock <- true
albums, err := a.db.ByPurchaseDate() albums, err := a.db.ByPurchaseDate()
<-a.dbLock
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -57,15 +56,14 @@ func (a *appData) buildRouter() http.Handler {
return return
} }
a.dbLock <- true albumUnsafe, err := a.db.Get(AlbumID(id))
album, err := a.db.Get(AlbumID(id))
<-a.dbLock
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
//sanitize extension of the path from bedetheque.com
album := *albumUnsafe
ext := path.Ext(album.CoverURL) ext := path.Ext(album.CoverURL)
rxExt.ReplaceAllString(ext, "") ext = strings.ToLower(rxExt.ReplaceAllString(ext, ""))
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, ext) album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, ext)
enc := json.NewEncoder(w) enc := json.NewEncoder(w)