From 1dd98c25dfe839ceedaa5392fa8a61910bee6743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 27 Jul 2023 15:54:26 +0800 Subject: [PATCH] =?UTF-8?q?enhance(hail):=E6=9A=B4=E9=9C=B2Hail=20ID?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=9A=84=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E5=85=B6=E4=BB=96=E9=A1=B9=E7=9B=AE=E4=B8=AD=E6=94=BE?= =?UTF-8?q?=E7=BD=AE=E4=BA=8EContext=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serial_code/hail/hail.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_code/hail/hail.go b/serial_code/hail/hail.go index 7a6524d..a7f957b 100644 --- a/serial_code/hail/hail.go +++ b/serial_code/hail/hail.go @@ -10,17 +10,17 @@ import ( ) // 用于记录当前冰雹ID组成内容的数据结构 -type _HailAlgorithm struct { +type HailAlgorithm struct { hostSerial int64 lastTimestamp int64 lastSequence int64 lock sync.Mutex } -var hailAlgorithmInstance *_HailAlgorithm +var hailAlgorithmInstance *HailAlgorithm // 获取当前已经完成初始化的冰雹ID生成算法实例,如果尚未完成初始化则会返回未初始化的错误。 -func Get() (*_HailAlgorithm, error) { +func Get() (*HailAlgorithm, error) { if hailAlgorithmInstance == nil { return nil, &HailAlgorithmNotInitializedError{} } @@ -29,7 +29,7 @@ func Get() (*_HailAlgorithm, error) { // 指定一个主机编号,并完成冰雹ID生成算法的初始化。 func Initialize(hostSerial int64) { - hailAlgorithmInstance = &_HailAlgorithm{ + hailAlgorithmInstance = &HailAlgorithm{ hostSerial: hostSerial, lastTimestamp: types.Timestamp(), lastSequence: 0, @@ -38,7 +38,7 @@ func Initialize(hostSerial int64) { } // 返回一个可用的时间戳,如果主机发生了时间回拨,那么将等待一秒钟。 -func (h *_HailAlgorithm) timestamp() int64 { +func (h *HailAlgorithm) timestamp() int64 { for { timestamp := types.Timestamp() if timestamp == h.lastTimestamp { @@ -54,7 +54,7 @@ func (h *_HailAlgorithm) timestamp() int64 { } // 生成一个冰雹ID。 -func (h *_HailAlgorithm) Generate() int64 { +func (h *HailAlgorithm) Generate() int64 { h.lock.Lock() defer h.lock.Unlock() timestamp := h.timestamp() @@ -63,11 +63,11 @@ func (h *_HailAlgorithm) Generate() int64 { } // 生成一个冰雹ID,并将其转换为字符串。 -func (h *_HailAlgorithm) GenerateString() string { +func (h *HailAlgorithm) GenerateString() string { return fmt.Sprintf("%017d", h.Generate()) } // 生成一个冰雹ID,将其转换为字符串并附加指定的前缀 -func (h *_HailAlgorithm) GeneratePrefixedString(prefix string) string { +func (h *HailAlgorithm) GeneratePrefixedString(prefix string) string { return fmt.Sprintf("%s%s", prefix, h.GenerateString()) }