Skip to content

Commit

Permalink
enhancement - add dialer timeout option for tcp conn on dialing logstash
Browse files Browse the repository at this point in the history
  • Loading branch information
9iksans committed Jul 12, 2024
1 parent 374adfe commit ef6e6ab
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type options struct {
skipVerify bool
readTimeout time.Duration
writeTimeout time.Duration
dialTimeout time.Duration
tlsConfig *tls.Config
}

Expand Down Expand Up @@ -72,6 +73,13 @@ func SetWriteTimeout(writeTimeout time.Duration) Option {
}
}

// SetDialTimeout Option func
func SetDialTimeout(dialTimeout time.Duration) Option {
return func(o *options) {
o.dialTimeout = dialTimeout
}
}

// SetKeepAlive Option func
func SetKeepAlive(keepAlive time.Duration) Option {
return func(o *options) {
Expand All @@ -94,22 +102,23 @@ func SetProtocolConn(protocol string) Option {
}
}

func (s *Stash) dial(address string, o *options) error {
conn, err := net.Dial(o.protocol, address)
func (s *Stash) dial(address string) error {
conn, err := s.o.dialer.Dial(s.o.protocol, address)
if err != nil {
conn.Close()
return err
}

s.conn = conn

// if useTLS true
// Force stash to use TLS
if o.useTLS {
if s.o.useTLS {
var tlsConfig *tls.Config
if o.tlsConfig == nil {
tlsConfig = &tls.Config{InsecureSkipVerify: o.skipVerify}
if s.o.tlsConfig == nil {
tlsConfig = &tls.Config{InsecureSkipVerify: s.o.skipVerify}
} else {
tlsConfig = o.tlsConfig
tlsConfig = s.o.tlsConfig
}

if tlsConfig.ServerName == "" {
Expand Down Expand Up @@ -143,6 +152,7 @@ func Connect(host string, port uint64, opts ...Option) (*Stash, error) {
o := &options{
dialer: &net.Dialer{
KeepAlive: time.Minute * 5,
Timeout: 30 * time.Second,
},
protocol: "tcp",
writeTimeout: 30 * time.Second,
Expand All @@ -153,7 +163,7 @@ func Connect(host string, port uint64, opts ...Option) (*Stash, error) {
}

s.o = o
if err := s.dial(address, o); err != nil {
if err := s.dial(address); err != nil {
return nil, err
}

Expand Down Expand Up @@ -182,7 +192,7 @@ func (s *Stash) Write(data []byte) (int, error) {
if strings.Contains(err.Error(), BrokenPipeError) {
log.Printf("go-stash: %s | do re dial\n", err.Error())
// re dial ignore error
err = s.dial(s.address, s.o)
err = s.dial(s.address)
if err != nil {
log.Printf("go-stash: %s | do re dial\n", err.Error())
}
Expand Down

0 comments on commit ef6e6ab

Please sign in to comment.