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

@ -1,6 +1,7 @@
package adapters
import (
"context"
"encoding/json"
"errors"
"net"
@ -99,7 +100,7 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
}
// URLTest get the delay for the specified URL
func (p *Proxy) URLTest(url string) (t uint16, err error) {
func (p *Proxy) URLTest(ctx context.Context, url string) (t uint16, err error) {
defer func() {
p.alive = err == nil
record := C.DelayHistory{Time: time.Now()}
@ -123,6 +124,13 @@ func (p *Proxy) URLTest(url string) (t uint16, err error) {
return
}
defer instance.Close()
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return
}
req = req.WithContext(ctx)
transport := &http.Transport{
Dial: func(string, string) (net.Conn, error) {
return instance, nil
@ -133,8 +141,9 @@ func (p *Proxy) URLTest(url string) (t uint16, err error) {
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := http.Client{Transport: transport}
resp, err := client.Get(url)
resp, err := client.Do(req)
if err != nil {
return
}