Skip to content

Commit

Permalink
feat: reconnect set timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqinghe committed Oct 18, 2022
1 parent ceecb19 commit 316803b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ func (cli *Client) Dial(network string, addr string) (*Session, error) {
return cli.mgr.StoreConn(conn)
}

func (cli *Client) DialTimeout(network string, addr string, timeout time.Duration) (*Session, error) {
c, err := net.DialTimeout(network, addr, timeout)
if err != nil {
return nil, err
}

conn := newConn(c, cli.codec)
if cli.opts.OnConnected != nil {
if err := cli.opts.OnConnected(conn); err != nil {
return nil, err
}
}

return cli.mgr.StoreConn(conn)
}

type ReconnectPolicy interface {
Retry() bool
Timer() *time.Timer
Expand All @@ -82,7 +98,7 @@ func (cli *Client) reconnect(sess *Session) {
for cli.needReconnect(sess) && policy.Retry() {
logrus.WithField("sessionId", sess.id).WithField("remoteAddr", sess.RemoteAddr().String()).Debugln("session reconnect")

if s, err := cli.Dial(addr.Network(), addr.String()); err == nil {
if s, err := cli.DialTimeout(addr.Network(), addr.String(), time.Second*5); err == nil {
s.data = sess.data
s.user = sess.user
s.lastPackTs = sess.lastPackTs
Expand Down
2 changes: 1 addition & 1 deletion example/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
Version: 0x01,
ID: 1,
Timestamp: time.Now().Unix(),
Length: uint32(len(data)),
Length: uint64(len(data)),
},
Data: data,
})
Expand Down

0 comments on commit 316803b

Please sign in to comment.