feat: add query config(sniff). (#5)
This commit is contained in:
parent
a574118649
commit
b247b4fea8
@ -7,6 +7,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -16,10 +17,16 @@ import (
|
|||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated. use uri query: http://<username>:<password>@example.com:port?ping=false&...
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DisablePing bool
|
DisablePing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UriConfig struct {
|
||||||
|
Ping bool `json:"ping"`
|
||||||
|
Sniff bool `json:"sniff"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewClient
|
// NewClient
|
||||||
// new esv7 client
|
// new esv7 client
|
||||||
// uri example:
|
// uri example:
|
||||||
@ -55,6 +62,12 @@ func NewClient(ctx context.Context, uri string, configs ...Config) (*elastic.Cli
|
|||||||
password, _ = ins.User.Password()
|
password, _ = ins.User.Password()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query := ins.Query()
|
||||||
|
|
||||||
|
cfg2 := &UriConfig{}
|
||||||
|
cfg2.Ping, _ = strconv.ParseBool(query.Get("ping"))
|
||||||
|
cfg2.Sniff, _ = strconv.ParseBool(query.Get("sniff"))
|
||||||
|
|
||||||
if client, err = elastic.NewClient(
|
if client, err = elastic.NewClient(
|
||||||
elastic.Config{
|
elastic.Config{
|
||||||
Addresses: endpoints,
|
Addresses: endpoints,
|
||||||
@ -68,12 +81,15 @@ func NewClient(ctx context.Context, uri string, configs ...Config) (*elastic.Cli
|
|||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext,
|
DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext,
|
||||||
},
|
},
|
||||||
|
DiscoverNodesOnStart: cfg2.Sniff,
|
||||||
},
|
},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cfg.DisablePing {
|
// Deprecated.
|
||||||
|
cfg.DisablePing = cfg.DisablePing || cfg2.Ping
|
||||||
|
if cfg.DisablePing {
|
||||||
var res *esapi.Response
|
var res *esapi.Response
|
||||||
if res, err = client.Ping(client.Ping.WithContext(tool.TimeoutCtx(ctx, 5))); err != nil {
|
if res, err = client.Ping(client.Ping.WithContext(tool.TimeoutCtx(ctx, 5))); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user