This commit is contained in:
36
pkg/outboxid/outboxid.go
Normal file
36
pkg/outboxid/outboxid.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Package outboxid 提供公共 Outbox 稳定事件标识生成能力。
|
||||
package outboxid
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// Stable 保留未超限标识;超限时保留业务前缀并追加稳定摘要。
|
||||
func Stable(prefix, value string) string {
|
||||
candidate := prefix + value
|
||||
if utf8.RuneCountInString(candidate) <= constants.OutboxEventIDMaxLength {
|
||||
return candidate
|
||||
}
|
||||
digest := sha256.Sum256([]byte(candidate))
|
||||
encoded := hex.EncodeToString(digest[:])
|
||||
if len(prefix) >= constants.OutboxEventIDMaxLength {
|
||||
return encoded
|
||||
}
|
||||
return prefix + encoded[:constants.OutboxEventIDMaxLength-len(prefix)]
|
||||
}
|
||||
|
||||
// Validate 校验公共 Outbox 事件及父事件标识的数据库长度契约。
|
||||
func Validate(eventID, parentEventID string) error {
|
||||
if utf8.RuneCountInString(eventID) > constants.OutboxEventIDMaxLength {
|
||||
return errors.New("Outbox 事件ID超过64字符上限")
|
||||
}
|
||||
if utf8.RuneCountInString(parentEventID) > constants.OutboxEventIDMaxLength {
|
||||
return errors.New("Outbox 父事件ID超过64字符上限")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user