mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
新增重试不同的服务地址
This commit is contained in:
29
dial.go
Normal file
29
dial.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/injoyai/ios"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewHostDial(hosts []string, timeout time.Duration) ios.DialFunc {
|
||||
if len(hosts) == 0 {
|
||||
hosts = Hosts
|
||||
}
|
||||
index := 0
|
||||
|
||||
return func(ctx context.Context) (ios.ReadWriteCloser, string, error) {
|
||||
defer func() { index++ }()
|
||||
if index >= len(hosts) {
|
||||
index = 0
|
||||
}
|
||||
addr := hosts[index]
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err := net.DialTimeout("tcp", addr, timeout)
|
||||
return c, hosts[index], err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user