From e1f9e0fe4efa87f163dcbbc5493a785b78ef965f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 27 Sep 2021 20:44:04 +0800 Subject: [PATCH 1/2] terror: provide a RegisterFinish API --- terror/terror.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/terror/terror.go b/terror/terror.go index 374f9e0f0..02c0ab46c 100644 --- a/terror/terror.go +++ b/terror/terror.go @@ -18,6 +18,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "github.com/pingcap/errors" "github.com/pingcap/log" @@ -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. +func RegisterFinish() { + atomic.StoreUint32(®isterFinish, 1) +} + +func frozen() bool { + return atomic.LoadUint32(®isterFinish) != 0 +} + // RegisterErrorClass registers new error class for terror. func RegisterErrorClass(classCode int, desc string) ErrClass { errClass := ErrClass(classCode) @@ -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{}) From bd914f4cb9b2d765430a49056f64f2a5cd0b5ff2 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 30 Sep 2021 09:18:27 +0800 Subject: [PATCH 2/2] Update terror/terror.go Co-authored-by: kennytm --- terror/terror.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terror/terror.go b/terror/terror.go index 02c0ab46c..121794bea 100644 --- a/terror/terror.go +++ b/terror/terror.go @@ -111,7 +111,7 @@ func (m *code2ErrClassMap) Put(key string, err ErrClass) { 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. +// The use pattern should be register all the errors during initialization, and then call RegisterFinish. func RegisterFinish() { atomic.StoreUint32(®isterFinish, 1) }