feat: order id generator.
This commit is contained in:
47
order_id/id.go
Normal file
47
order_id/id.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package order_id
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/tx7do/kratos-utils/trans"
|
||||
)
|
||||
|
||||
type idCounter uint32
|
||||
|
||||
func (c *idCounter) Increase() uint32 {
|
||||
cur := *c
|
||||
atomic.AddUint32((*uint32)(c), 1)
|
||||
atomic.CompareAndSwapUint32((*uint32)(c), 1000, 0)
|
||||
return uint32(cur)
|
||||
}
|
||||
|
||||
var orderIdIndex idCounter
|
||||
|
||||
// GenerateOrderIdWithRandom 生成20位订单号,前缀+时间+随机数
|
||||
func GenerateOrderIdWithRandom(prefix string, split string, tm *time.Time) string {
|
||||
if tm == nil {
|
||||
tm = trans.Time(time.Now())
|
||||
}
|
||||
|
||||
index := rand.Intn(1000)
|
||||
|
||||
return fmt.Sprintf("%s%s%.4d%s%.2d%s%.2d%s%.2d%s%.2d%s%.2d%s%.4d", prefix, split,
|
||||
tm.Year(), split, tm.Month(), split, tm.Day(), split,
|
||||
tm.Hour(), split, tm.Minute(), split, tm.Second(), split, index)
|
||||
}
|
||||
|
||||
// GenerateOrderIdWithIncreaseIndex 生成20位订单号,前缀+时间+自增长索引
|
||||
func GenerateOrderIdWithIncreaseIndex(prefix string, split string, tm *time.Time) string {
|
||||
if tm == nil {
|
||||
tm = trans.Time(time.Now())
|
||||
}
|
||||
|
||||
index := orderIdIndex.Increase()
|
||||
|
||||
return fmt.Sprintf("%s%s%.4d%s%.2d%s%.2d%s%.2d%s%.2d%s%.2d%s%.4d", prefix, split,
|
||||
tm.Year(), split, tm.Month(), split, tm.Day(), split,
|
||||
tm.Hour(), split, tm.Minute(), split, tm.Second(), split, index)
|
||||
}
|
||||
63
order_id/id_test.go
Normal file
63
order_id/id_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package order_id
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/tx7do/kratos-utils/trans"
|
||||
)
|
||||
|
||||
func TestGenerateOrderIdWithRandom(t *testing.T) {
|
||||
fmt.Println(GenerateOrderIdWithRandom("PT", "-", trans.Time(time.Now())))
|
||||
|
||||
tm := time.Now()
|
||||
var ids map[string]bool
|
||||
ids = make(map[string]bool)
|
||||
count := 100
|
||||
for i := 0; i < count; i++ {
|
||||
ids[GenerateOrderIdWithRandom("PT", "", trans.Time(tm))] = true
|
||||
}
|
||||
assert.Equal(t, count, len(ids))
|
||||
}
|
||||
|
||||
func TestGenerateOrderIdWithIndex(t *testing.T) {
|
||||
tm := time.Now()
|
||||
|
||||
fmt.Println(GenerateOrderIdWithIncreaseIndex("PT", "", trans.Time(tm)))
|
||||
|
||||
ids := make(map[string]bool)
|
||||
count := 100
|
||||
for i := 0; i < count; i++ {
|
||||
ids[GenerateOrderIdWithIncreaseIndex("PT", "", trans.Time(tm))] = true
|
||||
}
|
||||
assert.Equal(t, count, len(ids))
|
||||
}
|
||||
|
||||
func TestGenerateOrderIdWithIndexThread(t *testing.T) {
|
||||
tm := time.Now()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var ids sync.Map
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for i := 0; i < 100; i++ {
|
||||
id := GenerateOrderIdWithIncreaseIndex("PT", "", trans.Time(tm))
|
||||
ids.Store(id, true)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
aLen := 0
|
||||
ids.Range(func(k, v interface{}) bool {
|
||||
aLen++
|
||||
return true
|
||||
})
|
||||
assert.Equal(t, 1000, aLen)
|
||||
}
|
||||
Reference in New Issue
Block a user