Remove some potential races

This commit is contained in:
Justin Hawkins 2022-04-18 13:01:07 +09:30
parent b344e757a6
commit 6b1dff54f9
2 changed files with 39 additions and 17 deletions

12
main.go
View File

@ -23,7 +23,9 @@ var downloads download.Downloads
var downloadId = 0 var downloadId = 0
var configService *config.ConfigService var configService *config.ConfigService
var versionInfo = version.Info{CurrentVersion: "v0.6.0"} var versionInfo = version.Manager{
VersionInfo: version.Info{CurrentVersion: "v0.6.0"},
}
//go:embed web //go:embed web
var webFS embed.FS var webFS embed.FS
@ -39,7 +41,7 @@ type errorResponse struct {
} }
func main() { func main() {
log.Printf("Starting gropple %s - https://github.com/tardisx/gropple", versionInfo.CurrentVersion) log.Printf("Starting gropple %s - https://github.com/tardisx/gropple", versionInfo.GetInfo().CurrentVersion)
configService = &config.ConfigService{} configService = &config.ConfigService{}
configService.DetermineConfigDir() configService.DetermineConfigDir()
@ -110,8 +112,8 @@ func main() {
// versionRESTHandler returns the version information, if we have up-to-date info from github // versionRESTHandler returns the version information, if we have up-to-date info from github
func versionRESTHandler(w http.ResponseWriter, r *http.Request) { func versionRESTHandler(w http.ResponseWriter, r *http.Request) {
if versionInfo.GithubVersionFetched { if versionInfo.GetInfo().GithubVersionFetched {
b, _ := json.Marshal(versionInfo) b, _ := json.Marshal(versionInfo.GetInfo())
w.Write(b) w.Write(b)
} else { } else {
w.WriteHeader(400) w.WriteHeader(400)
@ -140,7 +142,7 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
Downloads: downloads, Downloads: downloads,
BookmarkletURL: template.URL(bookmarkletURL), BookmarkletURL: template.URL(bookmarkletURL),
Config: configService.Config, Config: configService.Config,
Version: versionInfo, Version: versionInfo.GetInfo(),
} }
err = t.ExecuteTemplate(w, "layout", info) err = t.ExecuteTemplate(w, "layout", info)

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"sync"
"golang.org/x/mod/semver" "golang.org/x/mod/semver"
) )
@ -19,8 +20,24 @@ type Info struct {
GithubVersionFetched bool `json:"-"` GithubVersionFetched bool `json:"-"`
} }
func (i *Info) UpdateGitHubVersion() error { type Manager struct {
i.GithubVersionFetched = false VersionInfo Info
lock sync.Mutex
}
func (m *Manager) GetInfo() Info {
// log.Print("getting info... b4 lock")
m.lock.Lock()
defer m.lock.Unlock()
return m.VersionInfo
}
func (m *Manager) UpdateGitHubVersion() error {
m.lock.Lock()
m.VersionInfo.GithubVersionFetched = false
m.lock.Unlock()
versionUrl := "https://api.github.com/repos/tardisx/gropple/releases" versionUrl := "https://api.github.com/repos/tardisx/gropple/releases"
resp, err := http.Get(versionUrl) resp, err := http.Get(versionUrl)
if err != nil { if err != nil {
@ -51,27 +68,30 @@ func (i *Info) UpdateGitHubVersion() error {
return errors.New("no releases found") return errors.New("no releases found")
} }
i.GithubVersion = releases[0].Name m.lock.Lock()
defer m.lock.Unlock()
m.VersionInfo.GithubVersion = releases[0].Name
m.VersionInfo.GithubVersionFetched = true
m.VersionInfo.UpgradeAvailable = m.canUpgrade()
i.GithubVersionFetched = true
i.UpgradeAvailable = i.canUpgrade()
return nil return nil
} }
func (i *Info) canUpgrade() bool { func (m *Manager) canUpgrade() bool {
if !i.GithubVersionFetched { if !m.VersionInfo.GithubVersionFetched {
return false return false
} }
if !semver.IsValid(i.CurrentVersion) { if !semver.IsValid(m.VersionInfo.CurrentVersion) {
log.Printf("current version %s is invalid", i.CurrentVersion) log.Printf("current version %s is invalid", m.VersionInfo.CurrentVersion)
} }
if !semver.IsValid(i.GithubVersion) { if !semver.IsValid(m.VersionInfo.GithubVersion) {
log.Printf("github version %s is invalid", i.GithubVersion) log.Printf("github version %s is invalid", m.VersionInfo.GithubVersion)
} }
if semver.Compare(i.CurrentVersion, i.GithubVersion) == -1 { if semver.Compare(m.VersionInfo.CurrentVersion, m.VersionInfo.GithubVersion) == -1 {
return true return true
} }
return false return false