Optimization: refactor picker

This commit is contained in:
Dreamacro
2019-07-02 19:18:03 +08:00
parent 0eff8516c0
commit 7c6c147a18
9 changed files with 123 additions and 104 deletions

View File

@ -163,32 +163,22 @@ func (r *Resolver) IsFakeIP() bool {
}
func (r *Resolver) batchExchange(clients []resolver, m *D.Msg) (msg *D.Msg, err error) {
in := make(chan interface{})
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
fast := picker.SelectFast(ctx, in)
fast, ctx := picker.WithContext(ctx)
wg := sync.WaitGroup{}
wg.Add(len(clients))
for _, r := range clients {
go func(r resolver) {
defer wg.Done()
fast.Go(func() (interface{}, error) {
msg, err := r.ExchangeContext(ctx, m)
if err != nil || msg.Rcode != D.RcodeSuccess {
return
return nil, errors.New("resolve error")
}
in <- msg
}(r)
return msg, nil
})
}
// release in channel
go func() {
wg.Wait()
close(in)
}()
elm, exist := <-fast
if !exist {
elm := fast.Wait()
if elm == nil {
return nil, errors.New("All DNS requests failed")
}