You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by di...@apache.org on 2024/12/12 03:22:25 UTC

(rocketmq-client-go) branch master updated: make default panic handler could be customized (#1182)

This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new abae4a0  make default panic handler could be customized (#1182)
abae4a0 is described below

commit abae4a0afa2830f7ff5a9e4b9c16a0e777dea668
Author: WeizhongTu <we...@alibaba-inc.com>
AuthorDate: Thu Dec 12 11:22:19 2024 +0800

    make default panic handler could be customized (#1182)
    
    * make default panic handler could be customized
    
    * feat: add primitive.DefaultPanicHandler
---
 consumer/push_consumer.go |  3 +++
 primitive/base.go         | 13 +++++++++----
 primitive/base_test.go    | 21 +++++++++++++++++++++
 3 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/consumer/push_consumer.go b/consumer/push_consumer.go
index db6a37e..e6ec3e5 100644
--- a/consumer/push_consumer.go
+++ b/consumer/push_consumer.go
@@ -1111,6 +1111,9 @@ func (pc *pushConsumer) consumeMessageConcurrently(pq *processQueue, mq *primiti
 		go primitive.WithRecover(func() {
 			defer func() {
 				if err := recover(); err != nil {
+					if primitive.DefaultPanicHandler != nil {
+						primitive.DefaultPanicHandler(err)
+					}
 					rlog.Error("consumeMessageConcurrently panic", map[string]interface{}{
 						rlog.LogKeyUnderlayError: err,
 						rlog.LogKeyStack:         utils.GetStackAsString(false),
diff --git a/primitive/base.go b/primitive/base.go
index 0042785..6ddb32e 100644
--- a/primitive/base.go
+++ b/primitive/base.go
@@ -87,14 +87,19 @@ func verifyIP(ip string) error {
 
 type PanicHandler func(interface{})
 
-func DefaultPanicHandler(interface{}) {
-	return
-}
+//	primitive.DefaultPanicHandler = func(i interface{}) {
+//		sentry.CaptureMessage(fmt.Sprintf("%+v", i), nil)
+//	}
+var DefaultPanicHandler PanicHandler
 
 func WithRecover(fn func(), handlers ...PanicHandler) {
 	defer func() {
 		if len(handlers) == 0 {
-			handlers = append(handlers, DefaultPanicHandler)
+			if DefaultPanicHandler != nil {
+				handlers = append(handlers, DefaultPanicHandler)
+			} else {
+				handlers = append(handlers, func(interface{}) {})
+			}
 		}
 		for _, handler := range handlers {
 			if handler != nil {
diff --git a/primitive/base_test.go b/primitive/base_test.go
index db947c4..51d7aa1 100644
--- a/primitive/base_test.go
+++ b/primitive/base_test.go
@@ -18,6 +18,7 @@ limitations under the License.
 package primitive
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -74,3 +75,23 @@ func TestBase(t *testing.T) {
 	b = []string{"a", "c"}
 	assert.True(t, Diff(a, b))
 }
+
+func TestWithRecover(t *testing.T) {
+	ass := assert.New(t)
+
+	DefaultPanicHandler = nil
+	ass.NotPanics(func() {
+		WithRecover(func() {
+			panic("test")
+		})
+	})
+
+	DefaultPanicHandler = func(i interface{}) {
+		fmt.Println("panic test")
+	}
+	ass.NotPanics(func() {
+		WithRecover(func() {
+			panic("test")
+		})
+	})
+}