客户端增加recover

This commit is contained in:
lwch
2021-09-17 17:58:15 +08:00
parent 6ddf7e8a76
commit df71bcdb4f
3 changed files with 16 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package pool
import ( import (
"context" "context"
"natpass/code/network" "natpass/code/network"
"natpass/code/utils"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -86,6 +87,7 @@ func (conn *Conn) Close() {
} }
func (conn *Conn) loopRead(cancel context.CancelFunc) { func (conn *Conn) loopRead(cancel context.CancelFunc) {
defer utils.Recover("loopRead")
defer conn.Close() defer conn.Close()
defer cancel() defer cancel()
var timeout int var timeout int
@@ -128,6 +130,7 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) {
} }
func (conn *Conn) loopWrite(cancel context.CancelFunc) { func (conn *Conn) loopWrite(cancel context.CancelFunc) {
defer utils.Recover("loopWrite")
defer conn.Close() defer conn.Close()
defer cancel() defer cancel()
for { for {
@@ -158,6 +161,7 @@ func (conn *Conn) ChanUnknown() <-chan *network.Msg {
} }
func (conn *Conn) keepalive(ctx context.Context) { func (conn *Conn) keepalive(ctx context.Context) {
defer utils.Recover("keepalive")
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@@ -5,6 +5,7 @@ import (
"io" "io"
"natpass/code/client/pool" "natpass/code/client/pool"
"natpass/code/network" "natpass/code/network"
"natpass/code/utils"
"net" "net"
"github.com/lwch/logging" "github.com/lwch/logging"
@@ -48,6 +49,7 @@ func (link *Link) Forward() {
} }
func (link *Link) remoteRead() { func (link *Link) remoteRead() {
defer utils.Recover("remoteRead")
defer link.close() defer link.close()
ch := link.remote.ChanRead(link.id) ch := link.remote.ChanRead(link.id)
for { for {
@@ -81,6 +83,7 @@ func (link *Link) remoteRead() {
} }
func (link *Link) localRead() { func (link *Link) localRead() {
defer utils.Recover("localRead")
defer link.close() defer link.close()
<-link.OnWork <-link.OnWork
buf := make([]byte, 16*1024) buf := make([]byte, 16*1024)

9
code/utils/recover.go Normal file
View File

@@ -0,0 +1,9 @@
package utils
import "github.com/lwch/logging"
func Recover(name string) {
if err := recover(); err != nil {
logging.Error("%s: %v", name, err)
}
}