1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
net.go 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
Nicolas Ruflin 提交于 2017-10-03 14:33 . Run make misspell (#5300)
package dialchain
import (
"fmt"
"net"
"time"
"github.com/elastic/beats/heartbeat/look"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/transport"
)
// TCPDialer creates a new NetDialer with constant event fields and default
// connection timeout.
// The fields parameter holds additional constants to be added to the final event
// structure.
//
// The dialer will update the active events with:
//
// {
// "tcp": {
// "port": ...,
// "rtt": { "connect": { "us": ... }}
// }
// }
func TCPDialer(to time.Duration) NetDialer {
return netDialer(to)
}
// UDPDialer creates a new NetDialer with constant event fields and default
// connection timeout.
// The fields parameter holds additional constants to be added to the final event
// structure.
//
// The dialer will update the active events with:
//
// {
// "udp": {
// "port": ...,
// "rtt": { "connect": { "us": ... }}
// }
// }
func UDPDialer(to time.Duration) NetDialer {
return netDialer(to)
}
func netDialer(timeout time.Duration) NetDialer {
return func(event common.MapStr) (transport.Dialer, error) {
return makeDialer(func(network, address string) (net.Conn, error) {
namespace := ""
switch network {
case "tcp", "tcp4", "tcp6":
namespace = "tcp"
case "udp", "udp4", "udp6":
namespace = "udp"
default:
return nil, fmt.Errorf("unsupported network type %v", network)
}
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
addresses, err := net.LookupHost(host)
if err != nil {
logp.Warn(`DNS lookup failure "%s": %v`, host, err)
return nil, err
}
// dial via host IP by randomized iteration of known IPs
dialer := &net.Dialer{Timeout: timeout}
start := time.Now()
conn, err := transport.DialWith(dialer, network, host, addresses, port)
if err != nil {
return nil, err
}
end := time.Now()
event.DeepUpdate(common.MapStr{
namespace: common.MapStr{
"rtt": common.MapStr{
"connect": look.RTT(end.Sub(start)),
},
},
})
return conn, nil
}), nil
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.1.2

搜索帮助