소스 검색

rename LogPanic to HandlePanic

Tao Wen 7 년 전
부모
커밋
11b49dc073
2개의 변경된 파일19개의 추가작업 그리고 7개의 파일을 삭제
  1. 3 4
      unbounded_executor.go
  2. 16 3
      unbounded_executor_test.go

+ 3 - 4
unbounded_executor.go

@@ -14,11 +14,10 @@ import (
 var LogInfo = func(event string, properties ...interface{}) {
 }
 
-// LogPanic logs goroutine panic
-var LogPanic = func(recovered interface{}, properties ...interface{}) interface{} {
+// HandlePanic logs goroutine panic by default
+var HandlePanic = func(recovered interface{}, file string, line int) {
 	fmt.Println(fmt.Sprintf("paniced: %v", recovered))
 	debug.PrintStack()
-	return recovered
 }
 
 // StopSignal will not be recovered, will propagate to upper level goroutine
@@ -63,7 +62,7 @@ func (executor *UnboundedExecutor) Go(handler func(ctx context.Context)) {
 		defer func() {
 			recovered := recover()
 			if recovered != nil && recovered != StopSignal {
-				LogPanic(recovered)
+				HandlePanic(recovered, file, line)
 			}
 			executor.activeGoroutinesMutex.Lock()
 			defer executor.activeGoroutinesMutex.Unlock()

+ 16 - 3
unbounded_executor_test.go

@@ -1,13 +1,14 @@
-package concurrent
+package concurrent_test
 
 import (
 	"context"
 	"fmt"
 	"time"
+	"github.com/modern-go/concurrent"
 )
 
 func ExampleUnboundedExecutor_Go() {
-	executor := NewUnboundedExecutor()
+	executor := concurrent.NewUnboundedExecutor()
 	executor.Go(func(ctx context.Context) {
 		fmt.Println("abc")
 	})
@@ -16,7 +17,7 @@ func ExampleUnboundedExecutor_Go() {
 }
 
 func ExampleUnboundedExecutor_StopAndWaitForever() {
-	executor := NewUnboundedExecutor()
+	executor := concurrent.NewUnboundedExecutor()
 	executor.Go(func(ctx context.Context) {
 		everyMillisecond := time.NewTicker(time.Millisecond)
 		for {
@@ -36,3 +37,15 @@ func ExampleUnboundedExecutor_StopAndWaitForever() {
 	// goroutine exited
 	// exectuor stopped
 }
+
+func ExampleUnboundedExecutor_Go_panic() {
+	concurrent.HandlePanic = func(recovered interface{}, file string, line int) {
+		fmt.Println("panic logged")
+	}
+	executor := concurrent.NewUnboundedExecutor()
+	executor.Go(func(ctx context.Context) {
+		panic("!!!")
+	})
+	time.Sleep(time.Second)
+	// output: panic logged
+}