Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Sep 1, 2023
1 parent b0fc575 commit 158563f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/requestid/default_request_id_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type RequestIdInfo struct {
type DefaultRequestIdGenerator struct {
serverUniqId uint16
instanceUniqId uint16
requestSeqId uint32
requestSeqId atomic.Uint32
}

// NewDefaultRequestIdGenerator returns a new default request id generator
Expand Down Expand Up @@ -154,7 +154,7 @@ func (r *DefaultRequestIdGenerator) getRequestId(serverUniqId uint16, instanceUn

secondsAndRandomNumber := (secondsLow17bits << randomNumberBits) | randomNumberLow15bits

seqId := atomic.AddUint32(&r.requestSeqId, 1)
seqId := r.requestSeqId.Add(1)
seqIdLow31bits := seqId & reqSeqNumberBitsMask

seqIdAndClientIpv6Flag := (seqIdLow31bits << clientIpv6Bit) | (clientIpv6Flag & clientIpv6BitMask)
Expand Down
6 changes: 3 additions & 3 deletions pkg/uuid/internal_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type InternalUuidInfo struct {

// InternalUuidGenerator represents internal bundled uuid generator
type InternalUuidGenerator struct {
uuidSeqNumbers [1 << internalUuidTypeBits]uint64
uuidSeqNumbers [1 << internalUuidTypeBits]atomic.Uint64
uuidServerId uint8
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func (u *InternalUuidGenerator) GenerateUuids(idType UuidType, count uint8) []in

for {
unixTime = uint64(time.Now().Unix())
newLastSeqId = atomic.AddUint64(&u.uuidSeqNumbers[uuidType], uint64(count))
newLastSeqId = u.uuidSeqNumbers[uuidType].Add(uint64(count))

if newLastSeqId>>seqNumberIdBits == unixTime {
newFirstSeqId = newLastSeqId - uint64(count-1)
Expand All @@ -82,7 +82,7 @@ func (u *InternalUuidGenerator) GenerateUuids(idType UuidType, count uint8) []in
newFirstSeqId = unixTime << seqNumberIdBits
newLastSeqId = newFirstSeqId + uint64(count-1)

if atomic.CompareAndSwapUint64(&u.uuidSeqNumbers[uuidType], currentSeqId, newLastSeqId) {
if u.uuidSeqNumbers[uuidType].CompareAndSwap(currentSeqId, newLastSeqId) {
break
}
}
Expand Down

0 comments on commit 158563f

Please sign in to comment.