Skip to content

Commit

Permalink
feat: implement panic recovery. (#666)
Browse files Browse the repository at this point in the history
* feat: implement panic recovery.

* update recover logic.

* update log print.
  • Loading branch information
mo3et committed Aug 16, 2024
1 parent a25de99 commit 2b6ebb4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
21 changes: 21 additions & 0 deletions internal/interaction/long_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -175,6 +176,13 @@ func (c *LongConnMgr) SendReqWaitResp(ctx context.Context, m proto.Message, reqI
// reads from this goroutine.

func (c *LongConnMgr) readPump(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "readPump panic", errs.New(err))
}
}()

log.ZDebug(ctx, "readPump start", "goroutine ID:", getGoroutineID())
defer func() {
_ = c.close()
Expand Down Expand Up @@ -233,6 +241,12 @@ func (c *LongConnMgr) readPump(ctx context.Context) {
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *LongConnMgr) writePump(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "writePump panic", errs.New(err))
}
}()
log.ZDebug(ctx, "writePump start", "goroutine ID:", getGoroutineID())

defer func() {
Expand Down Expand Up @@ -282,6 +296,13 @@ func (c *LongConnMgr) writePump(ctx context.Context) {
}

func (c *LongConnMgr) heartbeat(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "heartbeat panic", errs.New(err))
}
}()

log.ZDebug(ctx, "heartbeat start", "goroutine ID:", getGoroutineID())
ticker := time.NewTicker(pingPeriod)
defer func() {
Expand Down
8 changes: 8 additions & 0 deletions internal/interaction/msg_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package interaction

import (
"context"
"fmt"
"runtime/debug"
"strings"
"sync"

Expand Down Expand Up @@ -149,6 +151,12 @@ func (m *MsgSyncer) loadSeq(ctx context.Context) error {
// DoListener Listen to the message pipe of the message synchronizer
// and process received and pushed messages
func (m *MsgSyncer) DoListener(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "DoListener panic", errs.New(err))
}
}()
for {
select {
case cmd := <-m.PushMsgAndMaxSeqCh:
Expand Down
11 changes: 10 additions & 1 deletion open_im_sdk/userRelated.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/openimsdk/openim-sdk-core/v3/internal/flagconst"
"runtime/debug"
"strings"
"sync"
"time"
"unsafe"

"github.com/openimsdk/openim-sdk-core/v3/internal/flagconst"

"github.com/openimsdk/openim-sdk-core/v3/internal/business"
conv "github.com/openimsdk/openim-sdk-core/v3/internal/conversation_msg"
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
Expand Down Expand Up @@ -248,6 +250,13 @@ func (u *LoginMgr) GetLoginUserID() string {
return u.loginUserID
}
func (u *LoginMgr) logoutListener(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "logoutListener panic", errs.New(err))
}
}()

for {
select {
case <-u.loginMgrCh:
Expand Down
9 changes: 9 additions & 0 deletions pkg/common/trigger_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package common
import (
"context"
"errors"
"fmt"
"runtime/debug"
"time"

"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
Expand Down Expand Up @@ -209,6 +211,13 @@ type goroutine interface {
}

func DoListener(Li goroutine, ctx context.Context) {
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("panic: %+v\n%s", r, debug.Stack())
log.ZError(ctx, "DoListener panic", errs.New(err))
}
}()

for {
select {
case cmd := <-Li.GetCh():
Expand Down

0 comments on commit 2b6ebb4

Please sign in to comment.