Skip to content

Commit

Permalink
improve gatway and p2papp reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
TenderIronh committed Nov 25, 2022
1 parent c8b8bf0 commit c3a43be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
1 change: 0 additions & 1 deletion core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const MinNodeNameLen = 8

func getmac(ip string) string {
//get mac relative to the ip address which connected to the mq.
ifaces, err := net.Interfaces()
if err != nil {
return ""
Expand Down
5 changes: 4 additions & 1 deletion core/p2papp.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,15 @@ func (app *p2pApp) listen() error {
if app.rtid != 0 {
go app.relayHeartbeatLoop()
}
for app.tunnel.isRuning() && app.running {
for app.tunnel.isRuning() {
if app.config.Protocol == "udp" {
app.listenUDP()
} else {
app.listenTCP()
}
if !app.running {
break
}
time.Sleep(time.Second * 10)
}
return nil
Expand Down
30 changes: 17 additions & 13 deletions core/p2pnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ var (
)

type P2PNetwork struct {
conn *websocket.Conn
online bool
running bool
restartCh chan bool
wg sync.WaitGroup
writeMtx sync.Mutex
serverTs int64
localTs int64
hbTime time.Time
conn *websocket.Conn
online bool
running bool
restartCh chan bool
wgReconnect sync.WaitGroup
writeMtx sync.Mutex
serverTs int64
localTs int64
hbTime time.Time
// msgMap sync.Map
msgMap map[uint64]chan []byte //key: nodeID
msgMapMtx sync.Mutex
Expand Down Expand Up @@ -72,7 +72,7 @@ func (pn *P2PNetwork) run() {

case <-pn.restartCh:
pn.online = false
pn.wg.Wait() // wait read/write goroutine exited
pn.wgReconnect.Wait() // wait read/write goroutine end
err := pn.init()
if err != nil {
gLog.Println(LvERROR, "P2PNetwork init error:", err)
Expand Down Expand Up @@ -151,7 +151,6 @@ func (pn *P2PNetwork) autorunApp() {
continue
}
pn.runAll()
time.Sleep(time.Second * 10)
}
gLog.Println(LvINFO, "autorunApp end")
}
Expand Down Expand Up @@ -436,6 +435,11 @@ func (pn *P2PNetwork) newTunnel(t *P2PTunnel, tid uint64, isClient bool) error {
}
func (pn *P2PNetwork) init() error {
gLog.Println(LvINFO, "init start")
go func() { //reconnect at least 5s
pn.wgReconnect.Add(1)
defer pn.wgReconnect.Done()
time.Sleep(NatTestTimeout)
}()
var err error
for {
// detect nat type
Expand Down Expand Up @@ -568,8 +572,8 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) {

func (pn *P2PNetwork) readLoop() {
gLog.Printf(LvDEBUG, "P2PNetwork readLoop start")
pn.wg.Add(1)
defer pn.wg.Done()
pn.wgReconnect.Add(1)
defer pn.wgReconnect.Done()
for pn.running {
pn.conn.SetReadDeadline(time.Now().Add(NetworkHeartbeatTime + 10*time.Second))
t, msg, err := pn.conn.ReadMessage()
Expand Down
2 changes: 1 addition & 1 deletion core/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"
)

const OpenP2PVersion = "3.5.5"
const OpenP2PVersion = "3.5.6"
const ProductName string = "openp2p"
const LeastSupportVersion = "3.0.0"

Expand Down

0 comments on commit c3a43be

Please sign in to comment.