Fix some more races

This commit is contained in:
Justin Hawkins 2023-03-10 00:07:29 +10:30
parent f2c05d0144
commit 3e7a3a2f3b
2 changed files with 21 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package download
import (
"encoding/json"
"fmt"
"io"
"log"
@ -61,6 +62,18 @@ func (m *Manager) ManageQueue() {
}
}
func (m *Manager) DownloadsAsJSON() ([]byte, error) {
m.Lock.Lock()
defer m.Lock.Unlock()
for _, dl := range m.Downloads {
dl.Lock.Lock()
defer dl.Lock.Unlock()
}
b, err := json.Marshal(m.Downloads)
return b, err
}
// startQueued starts any downloads that have been queued, we would not exceed
// maxRunning. If maxRunning is 0, there is no limit.
func (m *Manager) startQueued(maxRunning int) {

11
main.go
View File

@ -327,7 +327,11 @@ func fetchInfoOneRESTHandler(w http.ResponseWriter, r *http.Request) {
}
// just a get, return the object
thisDownload.Lock.Lock()
defer thisDownload.Lock.Unlock()
b, _ := json.Marshal(thisDownload)
w.Write(b)
return
} else {
@ -337,9 +341,10 @@ func fetchInfoOneRESTHandler(w http.ResponseWriter, r *http.Request) {
func fetchInfoRESTHandler(w http.ResponseWriter, r *http.Request) {
dm.Lock.Lock()
defer dm.Lock.Unlock()
b, _ := json.Marshal(dm.Downloads)
b, err := dm.DownloadsAsJSON()
if err != nil {
panic(err)
}
w.Write(b)
}