code refactor
This commit is contained in:
@@ -45,7 +45,7 @@ type RequestIdInfo struct {
|
|||||||
type DefaultRequestIdGenerator struct {
|
type DefaultRequestIdGenerator struct {
|
||||||
serverUniqId uint16
|
serverUniqId uint16
|
||||||
instanceUniqId uint16
|
instanceUniqId uint16
|
||||||
requestSeqId uint32
|
requestSeqId atomic.Uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDefaultRequestIdGenerator returns a new default request id generator
|
// NewDefaultRequestIdGenerator returns a new default request id generator
|
||||||
@@ -154,7 +154,7 @@ func (r *DefaultRequestIdGenerator) getRequestId(serverUniqId uint16, instanceUn
|
|||||||
|
|
||||||
secondsAndRandomNumber := (secondsLow17bits << randomNumberBits) | randomNumberLow15bits
|
secondsAndRandomNumber := (secondsLow17bits << randomNumberBits) | randomNumberLow15bits
|
||||||
|
|
||||||
seqId := atomic.AddUint32(&r.requestSeqId, 1)
|
seqId := r.requestSeqId.Add(1)
|
||||||
seqIdLow31bits := seqId & reqSeqNumberBitsMask
|
seqIdLow31bits := seqId & reqSeqNumberBitsMask
|
||||||
|
|
||||||
seqIdAndClientIpv6Flag := (seqIdLow31bits << clientIpv6Bit) | (clientIpv6Flag & clientIpv6BitMask)
|
seqIdAndClientIpv6Flag := (seqIdLow31bits << clientIpv6Bit) | (clientIpv6Flag & clientIpv6BitMask)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ type InternalUuidInfo struct {
|
|||||||
|
|
||||||
// InternalUuidGenerator represents internal bundled uuid generator
|
// InternalUuidGenerator represents internal bundled uuid generator
|
||||||
type InternalUuidGenerator struct {
|
type InternalUuidGenerator struct {
|
||||||
uuidSeqNumbers [1 << internalUuidTypeBits]uint64
|
uuidSeqNumbers [1 << internalUuidTypeBits]atomic.Uint64
|
||||||
uuidServerId uint8
|
uuidServerId uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func (u *InternalUuidGenerator) GenerateUuids(idType UuidType, count uint8) []in
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
unixTime = uint64(time.Now().Unix())
|
unixTime = uint64(time.Now().Unix())
|
||||||
newLastSeqId = atomic.AddUint64(&u.uuidSeqNumbers[uuidType], uint64(count))
|
newLastSeqId = u.uuidSeqNumbers[uuidType].Add(uint64(count))
|
||||||
|
|
||||||
if newLastSeqId>>seqNumberIdBits == unixTime {
|
if newLastSeqId>>seqNumberIdBits == unixTime {
|
||||||
newFirstSeqId = newLastSeqId - uint64(count-1)
|
newFirstSeqId = newLastSeqId - uint64(count-1)
|
||||||
@@ -82,7 +82,7 @@ func (u *InternalUuidGenerator) GenerateUuids(idType UuidType, count uint8) []in
|
|||||||
newFirstSeqId = unixTime << seqNumberIdBits
|
newFirstSeqId = unixTime << seqNumberIdBits
|
||||||
newLastSeqId = newFirstSeqId + uint64(count-1)
|
newLastSeqId = newFirstSeqId + uint64(count-1)
|
||||||
|
|
||||||
if atomic.CompareAndSwapUint64(&u.uuidSeqNumbers[uuidType], currentSeqId, newLastSeqId) {
|
if u.uuidSeqNumbers[uuidType].CompareAndSwap(currentSeqId, newLastSeqId) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user