-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbest_mirror.go
188 lines (170 loc) · 3.57 KB
/
best_mirror.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package getparty
import (
"bufio"
"container/heap"
"context"
"errors"
"io"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
"unicode"
)
var _ heap.Interface = (*mirrorPQ)(nil)
type mirror struct {
index int
queryDur time.Duration
url string
}
type mirrorPQ []*mirror
func (pq mirrorPQ) Len() int { return len(pq) }
func (pq mirrorPQ) Less(i, j int) bool {
return pq[i].queryDur < pq[j].queryDur
}
func (pq mirrorPQ) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *mirrorPQ) Push(x interface{}) {
n := len(*pq)
link := x.(*mirror)
link.index = n
*pq = append(*pq, link)
}
func (pq *mirrorPQ) Pop() interface{} {
old := *pq
n := len(old)
link := old[n-1]
old[n-1] = nil // avoid memory leak
link.index = -1 // for safety
*pq = old[:n-1]
return link
}
func (cmd Cmd) bestMirror(transport http.RoundTripper) ([]string, error) {
var top []string
var input io.Reader
var fdClose func() error
if cmd.opt.BestMirror.Mirrors == "-" {
input = os.Stdin
fdClose = func() error { return nil }
} else {
fd, err := os.Open(cmd.opt.BestMirror.Mirrors)
if err != nil {
return nil, withStack(err)
}
input = fd
fdClose = fd.Close
}
pq := cmd.batchMirrors(input, transport)
topn := int(cmd.opt.BestMirror.TopN)
if topn == 0 {
topn = pq.Len()
}
for i := 0; i < topn && pq.Len() != 0; i++ {
m := heap.Pop(&pq).(*mirror)
top = append(top, m.url)
cmd.loggers[INFO].Printf("%s: %q", m.queryDur.Truncate(time.Microsecond), m.url)
}
return top, withStack(fdClose())
}
func (cmd Cmd) batchMirrors(input io.Reader, transport http.RoundTripper) mirrorPQ {
max := int(cmd.opt.BestMirror.MaxGo)
if max == 0 {
max = runtime.NumCPU()
}
cmd.loggers[DEBUG].Println("Best-mirror max:", max)
mirrors := readLines(input)
result := make(chan *mirror)
var wg sync.WaitGroup
wg.Add(max)
go func() {
wg.Wait()
close(result)
}()
timeout := cmd.getTimeout()
for i := 0; i < max; i++ {
client := &http.Client{
Transport: transport,
}
go func() {
defer wg.Done()
for m := range mirrors {
err := queryMirror(cmd.Ctx, m, client, timeout, cmd.patcher)
if err != nil {
cmd.loggers[WARN].Println(err.Error())
continue
}
result <- m
}
}()
}
var pq mirrorPQ
for m := range result {
heap.Push(&pq, m)
}
return pq
}
func queryMirror(
ctx context.Context,
mirror *mirror,
client *http.Client,
timeout time.Duration,
patcher func(*http.Request),
) error {
req, err := http.NewRequest(http.MethodHead, mirror.url, nil)
if err != nil {
return err
}
if patcher != nil {
patcher(req)
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
start := time.Now()
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
mirror.queryDur = time.Since(start)
return resp.Body.Close()
}
if resp.Body != nil {
err = resp.Body.Close()
}
return errors.Join(err, UnexpectedHttpStatus(resp.StatusCode))
}
func readLines(r io.Reader) <-chan *mirror {
ch := make(chan *mirror)
go func() {
defer close(ch)
seen := make(map[string]bool)
scanner := bufio.NewScanner(r)
index := 0
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
line = strings.TrimFunc(line, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
})
if !seen[line] {
seen[line] = true
ch <- &mirror{
index: index,
url: line,
}
index++
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
}()
return ch
}