-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
) | ||
|
||
func WithRecovery(exec func(), recoverFn func(r interface{}), logger *zap.Logger) { | ||
defer func() { | ||
r := recover() | ||
if recoverFn != nil { | ||
recoverFn(r) | ||
} | ||
if r != nil { | ||
logger.Error("panic in the recoverable goroutine", | ||
zap.Reflect("r", r), | ||
zap.Stack("stack trace")) | ||
} | ||
}() | ||
exec() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/tiproxy/lib/util/errors" | ||
"github.com/pingcap/tiproxy/lib/util/logger" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithRecovery(t *testing.T) { | ||
lg, text := logger.CreateLoggerForTest(t) | ||
ch := make(chan error, 1) | ||
go WithRecovery(func() { | ||
panic("mock panic") | ||
}, func(r interface{}) { | ||
if r != nil { | ||
ch <- errors.Errorf("%v", r) | ||
} | ||
}, lg) | ||
require.Error(t, <-ch) | ||
require.Eventually(t, func() bool { | ||
return strings.Contains(text.String(), "mock panic") | ||
}, time.Second, 10*time.Millisecond) | ||
} |