1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
Steffen Siering 提交于 2017-04-28 09:08 . Heartbeat event format (#4091)
package transport
import (
"fmt"
"math/rand"
"net"
"strings"
)
func fullAddress(host string, defaultPort int) string {
if _, _, err := net.SplitHostPort(host); err == nil {
return host
}
idx := strings.Index(host, ":")
if idx >= 0 {
// IPv6 address detected
return fmt.Sprintf("[%v]:%v", host, defaultPort)
}
return fmt.Sprintf("%v:%v", host, defaultPort)
}
// DialWith randomly dials one of a number of addresses with a given dialer.
//
// Use this to select and dial one IP being known for one host name.
func DialWith(
dialer Dialer,
network, host string,
addresses []string,
port string,
) (c net.Conn, err error) {
switch len(addresses) {
case 0:
return nil, fmt.Errorf("no route to host %v", host)
case 1:
return dialer.Dial(network, net.JoinHostPort(addresses[0], port))
}
// Use randomization on DNS reported addresses combined with timeout and ACKs
// to spread potential load when starting up large number of beats using
// lumberjack.
//
// RFCs discussing reasons for ignoring order of DNS records:
// http://www.ietf.org/rfc/rfc3484.txt
// > is specific to locality-based address selection for multiple dns
// > records, but exists as prior art in "Choose some different ordering for
// > the dns records" done by a client
//
// https://tools.ietf.org/html/rfc1794
// > "Clients, of course, may reorder this information" - with respect to
// > handling order of dns records in a response.orwarded. Really required?
for _, i := range rand.Perm(len(addresses)) {
c, err = dialer.Dial(network, net.JoinHostPort(addresses[i], port))
if err == nil && c != nil {
return c, err
}
}
if err == nil {
err = fmt.Errorf("unable to connect to '%v'", host)
}
return nil, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.1.2

搜索帮助