Fix some more races
This commit is contained in:
parent
f2c05d0144
commit
3e7a3a2f3b
@ -1,6 +1,7 @@
|
|||||||
package download
|
package download
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"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
|
// startQueued starts any downloads that have been queued, we would not exceed
|
||||||
// maxRunning. If maxRunning is 0, there is no limit.
|
// maxRunning. If maxRunning is 0, there is no limit.
|
||||||
func (m *Manager) startQueued(maxRunning int) {
|
func (m *Manager) startQueued(maxRunning int) {
|
||||||
|
11
main.go
11
main.go
@ -327,7 +327,11 @@ func fetchInfoOneRESTHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// just a get, return the object
|
// just a get, return the object
|
||||||
|
thisDownload.Lock.Lock()
|
||||||
|
defer thisDownload.Lock.Unlock()
|
||||||
|
|
||||||
b, _ := json.Marshal(thisDownload)
|
b, _ := json.Marshal(thisDownload)
|
||||||
|
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
@ -337,9 +341,10 @@ func fetchInfoOneRESTHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func fetchInfoRESTHandler(w http.ResponseWriter, r *http.Request) {
|
func fetchInfoRESTHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
dm.Lock.Lock()
|
b, err := dm.DownloadsAsJSON()
|
||||||
defer dm.Lock.Unlock()
|
if err != nil {
|
||||||
b, _ := json.Marshal(dm.Downloads)
|
panic(err)
|
||||||
|
}
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user