代码拉取完成,页面将自动刷新
package modeutil
import (
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/mode"
"github.com/elastic/beats/libbeat/outputs/mode/lb"
"github.com/elastic/beats/libbeat/outputs/mode/single"
)
type ClientFactory func(host string) (mode.ProtocolClient, error)
type AsyncClientFactory func(string) (mode.AsyncProtocolClient, error)
type Settings struct {
Failover bool
MaxAttempts int
WaitRetry time.Duration
Timeout time.Duration
MaxWaitRetry time.Duration
}
func NewConnectionMode(
clients []mode.ProtocolClient,
s Settings,
) (mode.ConnectionMode, error) {
if s.Failover {
clients = NewFailoverClient(clients)
}
maxSend := s.MaxAttempts
wait := s.WaitRetry
maxWait := s.MaxWaitRetry
to := s.Timeout
if len(clients) == 1 {
return single.New(clients[0], maxSend, wait, to, maxWait)
}
return lb.NewSync(clients, maxSend, wait, to, maxWait)
}
func NewAsyncConnectionMode(
clients []mode.AsyncProtocolClient,
s Settings,
) (mode.ConnectionMode, error) {
if s.Failover {
clients = NewAsyncFailoverClient(clients)
}
return lb.NewAsync(clients, s.MaxAttempts, s.WaitRetry, s.Timeout, s.MaxWaitRetry)
}
// MakeClients will create a list from of ProtocolClient instances from
// outputer configuration host list and client factory function.
func MakeClients(
config *common.Config,
newClient ClientFactory,
) ([]mode.ProtocolClient, error) {
hosts, err := ReadHostList(config)
if err != nil {
return nil, err
}
if len(hosts) == 0 {
return nil, mode.ErrNoHostsConfigured
}
clients := make([]mode.ProtocolClient, 0, len(hosts))
for _, host := range hosts {
client, err := newClient(host)
if err != nil {
// on error destroy all client instance created
for _, client := range clients {
_ = client.Close() // ignore error
}
return nil, err
}
clients = append(clients, client)
}
return clients, nil
}
func MakeAsyncClients(
config *common.Config,
newClient AsyncClientFactory,
) ([]mode.AsyncProtocolClient, error) {
hosts, err := ReadHostList(config)
if err != nil {
return nil, err
}
if len(hosts) == 0 {
return nil, mode.ErrNoHostsConfigured
}
clients := make([]mode.AsyncProtocolClient, 0, len(hosts))
for _, host := range hosts {
client, err := newClient(host)
if err != nil {
// on error destroy all client instance created
for _, client := range clients {
_ = client.Close() // ignore error
}
return nil, err
}
clients = append(clients, client)
}
return clients, nil
}
func ReadHostList(cfg *common.Config) ([]string, error) {
config := struct {
Hosts []string `config:"hosts" validate:"required"`
Worker int `config:"worker" validate:"min=1"`
}{
Worker: 1,
}
err := cfg.Unpack(&config)
if err != nil {
return nil, err
}
lst := config.Hosts
if len(lst) == 0 || config.Worker <= 1 {
return lst, nil
}
// duplicate entries config.Workers times
hosts := make([]string, 0, len(lst)*config.Worker)
for _, entry := range lst {
for i := 0; i < config.Worker; i++ {
hosts = append(hosts, entry)
}
}
return hosts, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。