-
Notifications
You must be signed in to change notification settings - Fork 4
/
get.go
91 lines (82 loc) · 1.66 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"errors"
"github.com/eugene-eeo/psync/blockfs"
"log"
"net/http"
"os"
)
type Job struct {
Host string
Checksum blockfs.Checksum
}
type Response struct {
Job *Job
Error error
}
func getTask(fs *blockfs.FS, job *Job) error {
url := job.Host + "/" + string(job.Checksum)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New("GET " + url + ": " + string(resp.StatusCode))
}
b := make([]byte, blockfs.BlockSize)
length, err := resp.Body.Read(b)
block := blockfs.NewBlock(b[:length])
if block.Checksum != job.Checksum {
return errors.New("invalid checksum")
}
err = fs.WriteBlock(block)
return err
}
func produceResponses(fs *blockfs.FS, jobs <-chan *Job, dst chan<- *Response) {
for job := range jobs {
err := getTask(fs, job)
dst <- &Response{
Job: job,
Error: err,
}
}
}
func Get(fs *blockfs.FS, addr string, force bool) {
logger := log.New(os.Stderr, "", log.Ltime)
hashlist, err := blockfs.NewHashList(os.Stdin)
checkErr(err)
if !force {
hashlist = fs.MissingBlocks(hashlist)
}
jobs := make(chan *Job, 4)
rets := make(chan *Response, 4)
done := make(chan bool)
for i := 0; i < 4; i++ {
go produceResponses(fs, jobs, rets)
}
for _, checksum := range hashlist {
jobs <- &Job{
Checksum: checksum,
Host: addr,
}
}
close(jobs)
go func() {
failed := false
for i := 0; i < len(hashlist); i++ {
res := <-rets
if res.Error != nil {
logger.Println("error:", res.Error)
failed = true
continue
}
logger.Println("ok:", res.Job.Checksum)
}
done <- failed
}()
ok := <-done
if ok {
os.Exit(1)
}
}