All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m25s
37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
// 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
|
|
}
|