mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-15 21:23:19 +00:00
fa5d7a255b
* v5: Health Check & LeastLoad Strategy (rebased from 2c5a71490368500a982018a74a6d519c7e121816) Some changes will be necessary to integrate it into V2Ray * Update proto * parse duration conf with time.Parse() * moving health ping to observatory as a standalone component * moving health ping to observatory as a standalone component: auto generated file * add initialization for health ping * incorporate changes in router implementation * support principle target output * add v4 json support for BurstObservatory & fix balancer reference * update API command * remove cancelled API * return zero length value when observer is not found * remove duplicated targeted dispatch * adjust test with updated structure * bug fix for observer * fix strategy selector * fix strategy least load * Fix ticker usage ticker.Close does not close ticker.C * feat: Replace default Health Ping URL to HTTPS (#1991) * fix selectLeastLoad() returns wrong number of nodes (#2083) * Test: fix leastload strategy unit test * fix(router): panic caused by concurrent map read and write (#2678) * Clean up code --------- Co-authored-by: Jebbs <qjebbs@gmail.com> Co-authored-by: Shelikhoo <xiaokangwang@outlook.com> Co-authored-by: 世界 <i@sekai.icu> Co-authored-by: Bernd Eichelberger <46166740+4-FLOSS-Free-Libre-Open-Source-Software@users.noreply.github.com> Co-authored-by: 秋のかえで <autmaple@protonmail.com> Co-authored-by: Rinka <kujourinka@gmail.com>
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package burst
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/transport/internet/tagged"
|
|
)
|
|
|
|
type pingClient struct {
|
|
destination string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func newPingClient(ctx context.Context, destination string, timeout time.Duration, handler string) *pingClient {
|
|
return &pingClient{
|
|
destination: destination,
|
|
httpClient: newHTTPClient(ctx, handler, timeout),
|
|
}
|
|
}
|
|
|
|
func newDirectPingClient(destination string, timeout time.Duration) *pingClient {
|
|
return &pingClient{
|
|
destination: destination,
|
|
httpClient: &http.Client{Timeout: timeout},
|
|
}
|
|
}
|
|
|
|
func newHTTPClient(ctxv context.Context, handler string, timeout time.Duration) *http.Client {
|
|
tr := &http.Transport{
|
|
DisableKeepAlives: true,
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
dest, err := net.ParseDestination(network + ":" + addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tagged.Dialer(ctxv, dest, handler)
|
|
},
|
|
}
|
|
return &http.Client{
|
|
Transport: tr,
|
|
Timeout: timeout,
|
|
// don't follow redirect
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
}
|
|
|
|
// MeasureDelay returns the delay time of the request to dest
|
|
func (s *pingClient) MeasureDelay() (time.Duration, error) {
|
|
if s.httpClient == nil {
|
|
panic("pingClient no initialized")
|
|
}
|
|
req, err := http.NewRequest(http.MethodHead, s.destination, nil)
|
|
if err != nil {
|
|
return rttFailed, err
|
|
}
|
|
start := time.Now()
|
|
resp, err := s.httpClient.Do(req)
|
|
if err != nil {
|
|
return rttFailed, err
|
|
}
|
|
// don't wait for body
|
|
resp.Body.Close()
|
|
return time.Since(start), nil
|
|
}
|