mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加NewRandomDial,用于随机连接,防止连接池对一个IP建立大量连接
This commit is contained in:
17
dial.go
17
dial.go
@@ -3,8 +3,10 @@ package tdx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/injoyai/ios"
|
"github.com/injoyai/ios"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHostDial(hosts []string) ios.DialFunc {
|
func NewHostDial(hosts []string) ios.DialFunc {
|
||||||
@@ -26,3 +28,18 @@ func NewHostDial(hosts []string) ios.DialFunc {
|
|||||||
return c, addr, err
|
return c, addr, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRandomDial(hosts []string) ios.DialFunc {
|
||||||
|
if len(hosts) == 0 {
|
||||||
|
hosts = Hosts
|
||||||
|
}
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
return func(ctx context.Context) (ios.ReadWriteCloser, string, error) {
|
||||||
|
addr := hosts[r.Intn(len(hosts))]
|
||||||
|
if !strings.Contains(addr, ":") {
|
||||||
|
addr += ":7709"
|
||||||
|
}
|
||||||
|
c, err := net.Dial("tcp", addr)
|
||||||
|
return c, addr, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user