aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorMitchell <mitch@riedstra.dev>2021-01-12 20:53:02 -0500
committerMitchell <mitch@riedstra.dev>2021-01-12 20:53:02 -0500
commit9b04f4ca1c2fe470a562be4b075d48d1c18962da (patch)
tree7cb3b465eec5bde3de88d826880d2cc625eff005 /cmd
parentfde64077cdd85f7a7b989fef320bf7fa3826a01d (diff)
downloadsteam-export-9b04f4ca1c2fe470a562be4b075d48d1c18962da.tar.gz
steam-export-9b04f4ca1c2fe470a562be4b075d48d1c18962da.tar.xz
Remove the rwmutex from the steam library as the template may be mutating the map causing issues. Embed the mutexes.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/web/delete.go4
-rw-r--r--cmd/web/download.go8
-rw-r--r--cmd/web/index.go16
-rw-r--r--cmd/web/install.go59
-rw-r--r--cmd/web/main.go18
5 files changed, 53 insertions, 52 deletions
diff --git a/cmd/web/delete.go b/cmd/web/delete.go
index 8edd6c6..49a1326 100644
--- a/cmd/web/delete.go
+++ b/cmd/web/delete.go
@@ -25,9 +25,9 @@ func gameDelete(w http.ResponseWriter, r *http.Request) {
return
}
- libMu.RLock()
+ Lib.Lock()
g, ok := Lib.Games[game]
- libMu.RUnlock()
+ Lib.Unlock()
if !ok {
Logger.Printf("Missing: %s", game)
http.Error(w, "Game is missing", 404)
diff --git a/cmd/web/download.go b/cmd/web/download.go
index a47d88c..f8ba057 100644
--- a/cmd/web/download.go
+++ b/cmd/web/download.go
@@ -1,10 +1,10 @@
package main
import (
+ "fmt"
"io"
"net/http"
"time"
- "fmt"
"github.com/gorilla/mux"
)
@@ -13,9 +13,9 @@ func gameDownloader(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
game := vars["game"]
- libMu.RLock()
+ Lib.Lock()
g, ok := Lib.Games[game]
- libMu.RUnlock()
+ Lib.Unlock()
if !ok {
Logger.Printf("Missing: %s", game)
http.Error(w, "Game is missing", 404)
@@ -23,7 +23,7 @@ func gameDownloader(w http.ResponseWriter, r *http.Request) {
}
w.Header().Add("Content-type", "application/tar")
- w.Header().Add("Estimated-size", fmt.Sprintf("%d", g.Size))
+ w.Header().Add("Estimated-size", fmt.Sprintf("%d", g.Size))
Logger.Printf("Client %s is downloading: %s", r.RemoteAddr, game)
diff --git a/cmd/web/index.go b/cmd/web/index.go
index 970810f..3121384 100644
--- a/cmd/web/index.go
+++ b/cmd/web/index.go
@@ -129,10 +129,14 @@ Change library path
)
func index(w http.ResponseWriter, r *http.Request) {
- libMu.RLock()
- defer libMu.RUnlock()
- status.m.RLock()
- defer status.m.RUnlock()
+ // During rendering of the template I believe it's
+ // mutating during the sort of keys, so Lib no longer
+ // is an RWMutex and we're just locking this as if
+ // we're writing to it
+ Lib.Lock()
+ defer Lib.Unlock()
+ status.Lock()
+ defer status.Unlock()
err := Templ.ExecuteTemplate(w, "index",
struct {
@@ -143,8 +147,8 @@ func index(w http.ResponseWriter, r *http.Request) {
Port string
Version string
}{
- Lib,
- status.s,
+ &Lib.Library,
+ status,
isLocal(r.RemoteAddr),
getHostIP(),
getPort(),
diff --git a/cmd/web/install.go b/cmd/web/install.go
index 8cf8b66..fa1ea7a 100644
--- a/cmd/web/install.go
+++ b/cmd/web/install.go
@@ -14,6 +14,7 @@ import (
)
type statusInfo struct {
+ sync.RWMutex
Running bool
Error error
Url string
@@ -23,26 +24,22 @@ type statusInfo struct {
}
var (
- status = struct {
- m *sync.RWMutex
- s *statusInfo
- }{
- m: &sync.RWMutex{},
- s: &statusInfo{Running: false},
+ status = &statusInfo{
+ Running: false,
}
getPath = make(chan string)
)
func statsHandler(w http.ResponseWriter, r *http.Request) {
- status.m.RLock()
- defer status.m.RUnlock()
+ status.RLock()
+ defer status.RUnlock()
w.Header().Add("Content-type", "application/json")
enc := json.NewEncoder(w)
- err := enc.Encode(status.s)
+ err := enc.Encode(status)
if err != nil {
Logger.Println("While encoding status: ", err)
}
@@ -61,9 +58,9 @@ func installHttp(u string) error {
return fmt.Errorf("Failed to convert estimated size header: %w", err)
}
- status.m.Lock()
- status.s.Size = estSize
- status.m.Unlock()
+ status.Lock()
+ status.Size = estSize
+ status.Unlock()
rdr, wrtr := io.Pipe()
@@ -77,9 +74,9 @@ func installHttp(u string) error {
var total int64
start := time.Now()
- status.m.Lock()
- status.s.Start = &start
- status.m.Unlock()
+ status.Lock()
+ status.Start = &start
+ status.Unlock()
for {
var n int64
n, err = io.CopyN(wrtr, resp.Body, 100*1024*1024)
@@ -97,9 +94,9 @@ func installHttp(u string) error {
Logger.Printf("Downloading from %s, Size: %s, %0.1f%% Done, Rate: %.2f mb/s",
u, formatBytes(estSize), float64(total)/float64(estSize)*100, rate)
- status.m.Lock()
- status.s.Transferred = total
- status.m.Unlock()
+ status.Lock()
+ status.Transferred = total
+ status.Unlock()
}
if err == io.EOF {
@@ -129,11 +126,11 @@ func installPath(p string) error {
func installer(urls <-chan string) {
var err error
for u := range urls {
- status.m.Lock()
+ status.Lock()
Logger.Printf("Installer: running for URI: %s", u)
- status.s.Running = true
- status.s.Url = u
- status.m.Unlock()
+ status.Running = true
+ status.Url = u
+ status.Unlock()
if strings.HasPrefix(u, "http") {
err = installHttp(u)
@@ -141,11 +138,11 @@ func installer(urls <-chan string) {
err = installPath(u)
}
- status.m.Lock()
- status.s.Running = false
- status.s.Error = err
+ status.Lock()
+ status.Running = false
+ status.Error = err
Logger.Printf("Installer: Completed request %s Errors: %s", u, err)
- status.m.Unlock()
+ status.Unlock()
reloadLib()
}
@@ -174,14 +171,8 @@ func gameInstaller(w http.ResponseWriter, r *http.Request) {
}
} else {
fi, err := os.Stat(uri)
- if err != nil {
- Logger.Printf("Installer: While parsing url: %s", err)
- http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400)
- return
- }
-
- if !fi.Mode().IsRegular() {
- Logger.Printf("Installer: While parsing url: %s", err)
+ if err != nil || !fi.Mode().IsRegular() {
+ Logger.Printf("Installer: While parsing url/path: %s", err)
http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400)
return
}
diff --git a/cmd/web/main.go b/cmd/web/main.go
index bbb7fb6..d66e366 100644
--- a/cmd/web/main.go
+++ b/cmd/web/main.go
@@ -16,26 +16,30 @@ import (
"riedstra.dev/mitch/steam-export/steam"
)
+type steamLib struct {
+ steam.Library
+ sync.Mutex
+}
+
var (
Version = "Development"
Logger = log.New(os.Stderr, "", log.LstdFlags)
Listen = ":8899"
- libMu = &sync.RWMutex{}
- Lib *steam.Library
+ Lib steamLib
)
func reloadLib() {
Logger.Println("Starting library reload")
- libMu.Lock()
- defer libMu.Unlock()
+ Lib.Lock()
+ defer Lib.Unlock()
var err error
l2, err := steam.NewLibrary(DefaultLib)
if err != nil {
Logger.Printf("Error reopening library: %s", err)
return
}
- Lib = l2
+ Lib.Library = *l2
Logger.Println("Done reloading library")
}
@@ -141,10 +145,12 @@ func main() {
}
var err error
- Lib, err = steam.NewLibrary(DefaultLib)
+ var l *steam.Library
+ l, err = steam.NewLibrary(DefaultLib)
if err != nil {
Logger.Fatalf("While opening library path: %s", err)
}
+ Lib.Library = *l
go installer(getPath)