mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加NewRangeDial函数,更方便使用
This commit is contained in:
23
dial.go
23
dial.go
@@ -3,6 +3,7 @@ package tdx
|
||||
import (
|
||||
"context"
|
||||
"github.com/injoyai/ios"
|
||||
"github.com/injoyai/logs"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -43,3 +44,25 @@ func NewRandomDial(hosts []string) ios.DialFunc {
|
||||
return c, addr, err
|
||||
}
|
||||
}
|
||||
|
||||
func NewRangeDial(hosts []string) ios.DialFunc {
|
||||
if len(hosts) == 0 {
|
||||
hosts = Hosts
|
||||
}
|
||||
return func(ctx context.Context) (c ios.ReadWriteCloser, _ string, err error) {
|
||||
for i, addr := range hosts {
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err = net.Dial("tcp", addr)
|
||||
if err == nil {
|
||||
return c, addr, nil
|
||||
}
|
||||
if i < len(hosts)-1 {
|
||||
//最后一个错误返回出去
|
||||
logs.Err(err)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user