-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers.go
66 lines (53 loc) · 1.3 KB
/
servers.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
package main
import (
"context"
"fmt"
"time"
"go.formulabun.club/extractor"
"go.formulabun.club/metadatadb"
)
func scrapeServers(hosts []string, c *metadatadb.Client) {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Millisecond*200)
// TODO sort hosts on distance
parallel := 5
filess := make([][]fileFromServer, 0, len(hosts))
serversChan := make(chan string, parallel)
filesChan := make(chan []fileFromServer, parallel)
// This will consume all the servers and put the result in files asap
go serversToFiles(serversChan, filesChan)
go func() {
for _, h := range hosts {
serversChan <- h
}
}()
for len(filess) != len(hosts) {
filess = append(filess, <-filesChan)
}
close(serversChan)
close(filesChan)
oneByOne := make(chan *fileFromServer, 5)
done := make(chan struct{})
go transpose(filess, oneByOne, done)
downloadFiles(oneByOne, done, c)
}
func downloadFiles(oneByOne chan *fileFromServer, done chan struct{}, c *metadatadb.Client) {
dw := newDownloader()
ctx := context.Background()
ec := extractor.NewClient()
for {
select {
case <-done:
dw.WaitGroup.Wait()
dw.report()
return
case f := <-oneByOne:
dbFile := dw.handleFile(f)
err := ec.ExtractFile(dbFile)
if err != nil {
fmt.Println(err)
}
c.AddFile(dbFile, ctx)
}
}
}