Commit 7a10af48 authored by wei.xuan's avatar wei.xuan

feat: update timeout

parent 6a967689
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"strings" "strings"
"sync"
"time" "time"
"github.com/adamweixuan/getty" "github.com/adamweixuan/getty"
...@@ -21,9 +22,9 @@ var ( ...@@ -21,9 +22,9 @@ var (
func (client *Client) handleHeartbeat(session getty.Session) { func (client *Client) handleHeartbeat(session getty.Session) {
go func() { go func() {
if _, _, err := session.WritePkg(HeartbeatPacket, time.Second*3); err != nil { if _, _, err := session.WritePkg(HeartbeatPacket, time.Second*3); err != nil {
getty.GetLogger().Errorf("handleHeartbeat error %+v %v->%v", err, session.IsClosed(), session.GetActive()) getty.GetLogger().Errorf("handleHeartbeat error %+v %v", err, session.IsClosed())
} else { } else {
getty.GetLogger().Infof("handleHeartbeat success %v->%v", session.IsClosed(), session.GetActive()) getty.GetLogger().Infof("handleHeartbeat success %v", session.IsClosed())
} }
}() }()
} }
...@@ -188,8 +189,9 @@ func (client *Client) CloseAll(session getty.Session) { ...@@ -188,8 +189,9 @@ func (client *Client) CloseAll(session getty.Session) {
client.connStore.Range(func(key, value interface{}) bool { client.connStore.Range(func(key, value interface{}) bool {
serialNumber := key.(int64) serialNumber := key.(int64)
conn, _ := value.(net.Conn) conn, _ := value.(net.Conn)
getty.GetLogger().Infof("close serialNumber -> %d", serialNumber) getty.GetLogger().Infof("[CloseAll] close serialNumber -> %d", serialNumber)
client.OnClose(session, conn, serialNumber) client.OnClose(session, conn, serialNumber)
return true return true
}) })
client.connStore = sync.Map{}
} }
package client package client
import ( import (
"errors"
"runtime" "runtime"
"time" "time"
...@@ -14,7 +15,7 @@ type MajoraEventListener struct { ...@@ -14,7 +15,7 @@ type MajoraEventListener struct {
} }
func (m *MajoraEventListener) OnOpen(session getty.Session) error { func (m *MajoraEventListener) OnOpen(session getty.Session) error {
getty.GetLogger().Info("==================OnOpen==================") getty.GetLogger().Infof("connect to nathost %s success ...", m.client.config.TunnelAddr)
packet := protocol.TypeRegister.CreatePacket() packet := protocol.TypeRegister.CreatePacket()
packet.Extra = m.client.config.ClientID packet.Extra = m.client.config.ClientID
extraMap := make(map[string]string, 1) extraMap := make(map[string]string, 1)
...@@ -35,8 +36,11 @@ func (m *MajoraEventListener) OnClose(session getty.Session) { ...@@ -35,8 +36,11 @@ func (m *MajoraEventListener) OnClose(session getty.Session) {
func (m *MajoraEventListener) OnError(session getty.Session, err error) { func (m *MajoraEventListener) OnError(session getty.Session, err error) {
getty.GetLogger().Errorf("OnError %s", err.Error()) getty.GetLogger().Errorf("OnError %s", err.Error())
if errors.Is(err, common.ErrInvalidMagic) {
session.Close()
}
m.client.CloseAll(session) m.client.CloseAll(session)
session.Close()
} }
func (m *MajoraEventListener) OnCron(session getty.Session) { func (m *MajoraEventListener) OnCron(session getty.Session) {
......
...@@ -29,7 +29,6 @@ func (client *Client) connect() { ...@@ -29,7 +29,6 @@ func (client *Client) connect() {
getty.WithClientTaskPool(taskPool)) getty.WithClientTaskPool(taskPool))
gettyCli.RunEventLoop(NewClientSession(client)) gettyCli.RunEventLoop(NewClientSession(client))
client.natTunnel = gettyCli client.natTunnel = gettyCli
getty.GetLogger().Infof("connect to nathost %s success ...", hostPort)
} }
func NewClientSession(client *Client) func(getty.Session) error { func NewClientSession(client *Client) func(getty.Session) error {
...@@ -81,7 +80,6 @@ func InitialSession(session getty.Session, client *Client) (err error) { ...@@ -81,7 +80,6 @@ func InitialSession(session getty.Session, client *Client) (err error) {
func (client *Client) Redial(session getty.Session) { func (client *Client) Redial(session getty.Session) {
if !client.config.Redial.Valid() { if !client.config.Redial.Valid() {
getty.GetLogger().Warnf("Redial invalid")
return return
} }
client.CloseAll(session) client.CloseAll(session)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment