Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terror: provide a RegisterFinish API #1352

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions terror/terror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -107,6 +108,18 @@ func (m *code2ErrClassMap) Put(key string, err ErrClass) {
m.data.Store(key, err)
}

var registerFinish uint32

// RegisterFinish makes the register of new error panic.
// The use pattern should be regist all the error during initialization, and then call RegisterFinish.
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
func RegisterFinish() {
atomic.StoreUint32(&registerFinish, 1)
}

func frozen() bool {
return atomic.LoadUint32(&registerFinish) != 0
}

// RegisterErrorClass registers new error class for terror.
func RegisterErrorClass(classCode int, desc string) ErrClass {
errClass := ErrClass(classCode)
Expand Down Expand Up @@ -148,6 +161,9 @@ func (ec ErrClass) NotEqualClass(err error) bool {
}

func (ec ErrClass) initError(code ErrCode) string {
if frozen() {
panic("register error after initialized is prohibited")
}
clsMap, ok := ErrClassToMySQLCodes[ec]
if !ok {
clsMap = make(map[ErrCode]struct{})
Expand Down