Makes the app without global lock
This commit is contained in:
45
main.go
45
main.go
@@ -37,22 +37,9 @@ type appData struct {
|
||||
opts Options
|
||||
|
||||
errors chan error
|
||||
dbLock chan bool
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// type albumOperationType int
|
||||
|
||||
// const (
|
||||
// OpAdd albumOperationType = iota
|
||||
// OpDelete
|
||||
// )
|
||||
|
||||
// type albumOperation struct {
|
||||
// Type albumOperationType
|
||||
// A *Album
|
||||
// }
|
||||
|
||||
func newAppData(opts Options) (*appData, error) {
|
||||
if opts.BatchSize <= 0 {
|
||||
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{
|
||||
opts: opts,
|
||||
errors: make(chan error, 10),
|
||||
dbLock: make(chan bool, 1),
|
||||
}
|
||||
|
||||
blevePath := filepath.Join(basepath, "index")
|
||||
@@ -227,9 +213,7 @@ func (a *appData) cacheAlbumDescription(getAlbum <-chan *Album, toIndex chan<- *
|
||||
nbAlbums := 0
|
||||
for album := range getAlbum {
|
||||
nbAlbums++
|
||||
a.dbLock <- true
|
||||
albumCache, err := a.db.Get(album.ID)
|
||||
<-a.dbLock
|
||||
|
||||
if err == nil {
|
||||
toIndex <- albumCache
|
||||
@@ -239,12 +223,9 @@ func (a *appData) cacheAlbumDescription(getAlbum <-chan *Album, toIndex chan<- *
|
||||
err = a.getter.Get(album)
|
||||
if err != nil {
|
||||
a.errors <- fmt.Errorf("[CACHE ALBUMS]: getting %d :%s", album.ID, err)
|
||||
<-a.dbLock
|
||||
continue
|
||||
}
|
||||
a.dbLock <- true
|
||||
err = a.db.AddOrUpdate(album)
|
||||
<-a.dbLock
|
||||
if err != nil {
|
||||
a.errors <- fmt.Errorf("[CACHE ALBUMS]: storing %d: %s", album.ID, err)
|
||||
continue
|
||||
@@ -264,26 +245,13 @@ func (a *appData) maintainAlbumDatabase(stopChan <-chan struct{},
|
||||
select {
|
||||
case <-stopChan:
|
||||
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:
|
||||
if ok == false {
|
||||
checkAlbum = nil
|
||||
break
|
||||
}
|
||||
|
||||
a.dbLock <- true
|
||||
cachedAlbum, err := a.db.Get(aID)
|
||||
<-a.dbLock
|
||||
|
||||
if err != nil {
|
||||
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
|
||||
a.dbLock <- true
|
||||
err = a.db.AddOrUpdate(cachedAlbum)
|
||||
<-a.dbLock
|
||||
if err != nil {
|
||||
a.errors <- fmt.Errorf("[DB]: could not add new %d: %s", aID, err)
|
||||
continue
|
||||
}
|
||||
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
|
||||
case <-ticker.C:
|
||||
//we just chek
|
||||
a.dbLock <- true
|
||||
albums, err := a.db.ByPurchaseDate()
|
||||
<-a.dbLock
|
||||
if err != nil {
|
||||
a.errors <- fmt.Errorf("[UPDATER]: could not get albums: %s", err)
|
||||
continue
|
||||
|
||||
12
router.go
12
router.go
@@ -7,6 +7,7 @@ import (
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
bleve_http "github.com/blevesearch/bleve/http"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -23,9 +24,7 @@ func (a *appData) buildRouter() http.Handler {
|
||||
router.Handle("/api/search", searchHandler).Methods("POST")
|
||||
|
||||
router.Handle("/api/recents", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
a.dbLock <- true
|
||||
albums, err := a.db.ByPurchaseDate()
|
||||
<-a.dbLock
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -57,15 +56,14 @@ func (a *appData) buildRouter() http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
a.dbLock <- true
|
||||
album, err := a.db.Get(AlbumID(id))
|
||||
<-a.dbLock
|
||||
albumUnsafe, err := a.db.Get(AlbumID(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
//sanitize extension of the path from bedetheque.com
|
||||
|
||||
album := *albumUnsafe
|
||||
ext := path.Ext(album.CoverURL)
|
||||
rxExt.ReplaceAllString(ext, "")
|
||||
ext = strings.ToLower(rxExt.ReplaceAllString(ext, ""))
|
||||
album.CoverURL = fmt.Sprintf("/covers/%d%s", album.ID, ext)
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
|
||||
Reference in New Issue
Block a user