mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 11:34:01 +03:00
Merge pull request #355 from 9seconds/scout-native
Use native dialer for scout
This commit is contained in:
@@ -2,6 +2,7 @@ package doppel
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -28,11 +29,15 @@ type Network interface {
|
|||||||
// Dial establishes context-free TCP connections.
|
// Dial establishes context-free TCP connections.
|
||||||
Dial(network, address string) (essentials.Conn, error)
|
Dial(network, address string) (essentials.Conn, error)
|
||||||
|
|
||||||
// DialContext dials using a context. This is a preferrable way of
|
// DialContext dials using a context. This is a preferable way of
|
||||||
// establishing TCP connections.
|
// establishing TCP connections.
|
||||||
DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
|
DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
|
||||||
|
|
||||||
// MakeHTTPClient build an HTTP client with given dial function. If nothing is
|
// MakeHTTPClient build an HTTP client with given dial function. If nothing is
|
||||||
// provided, then DialContext of this interface is going to be used.
|
// provided, then DialContext of this interface is going to be used.
|
||||||
MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
|
MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
|
||||||
|
|
||||||
|
// NativeDialer returns a configured instance of native dialer that
|
||||||
|
// skips proxy connections or any other irrelevant settings.
|
||||||
|
NativeDialer() *net.Dialer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ func (s SimpleNetwork) DialContext(ctx context.Context, network, address string)
|
|||||||
return conn.(*net.TCPConn), nil
|
return conn.(*net.TCPConn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s SimpleNetwork) NativeDialer() *net.Dialer {
|
||||||
|
return &net.Dialer{}
|
||||||
|
}
|
||||||
|
|
||||||
func (s SimpleNetwork) MakeHTTPClient(dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client {
|
func (s SimpleNetwork) MakeHTTPClient(dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client {
|
||||||
if dialFunc == nil {
|
if dialFunc == nil {
|
||||||
dialFunc = s.DialContext
|
dialFunc = s.DialContext
|
||||||
|
|||||||
@@ -79,18 +79,19 @@ func (s Scout) learn(ctx context.Context, url string) ([]time.Duration, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s Scout) makeClient() (*http.Client, *ScoutConnCollected) {
|
func (s Scout) makeClient() (*http.Client, *ScoutConnCollected) {
|
||||||
|
dialer := s.network.NativeDialer()
|
||||||
collected := NewScoutConnCollected()
|
collected := NewScoutConnCollected()
|
||||||
client := s.network.MakeHTTPClient(func(
|
client := s.network.MakeHTTPClient(func(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
network string,
|
network string,
|
||||||
address string,
|
address string,
|
||||||
) (essentials.Conn, error) {
|
) (essentials.Conn, error) {
|
||||||
conn, err := s.network.DialContext(ctx, network, address)
|
conn, err := dialer.DialContext(ctx, network, address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewScoutConn(conn, collected), nil
|
return NewScoutConn(essentials.WrapNetConn(conn), collected), nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return client, collected
|
return client, collected
|
||||||
|
|||||||
Reference in New Issue
Block a user