86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"qoobing.com/gomod/log"
|
|
"qoobing.com/gomod/uid"
|
|
)
|
|
|
|
type MemCreatorHelper struct {
|
|
start int64
|
|
length int
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func NewMemCreatorHelper() *MemCreatorHelper {
|
|
return &MemCreatorHelper{
|
|
lock: sync.Mutex{},
|
|
start: 0,
|
|
length: 100,
|
|
}
|
|
}
|
|
|
|
// CreatePrefix create id prefix
|
|
func (mch *MemCreatorHelper) CreatePrefix(parts ...interface{}) string {
|
|
return "XXXPPPYYY"
|
|
}
|
|
|
|
// CreateRange create id range from database table t_id.
|
|
func (mch *MemCreatorHelper) CreateRange(prefix string) (start int64, length int, err error) {
|
|
mch.lock.Lock()
|
|
defer mch.lock.Unlock()
|
|
fmt.Println("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCreateRange")
|
|
|
|
mch.start += int64(mch.length)
|
|
if mch.start == 300 {
|
|
panic("test panic: 3")
|
|
} else if mch.start == 7000 {
|
|
return mch.start, mch.length, errors.New("tesk error: 7")
|
|
}
|
|
return mch.start, mch.length, nil
|
|
}
|
|
|
|
// // CreateMixedId create finnal transaction id
|
|
func (mch *MemCreatorHelper) CreateMixedId(prefix string, id int64, parts ...interface{}) string {
|
|
return fmt.Sprintf("id-%s-%d", prefix, id)
|
|
}
|
|
|
|
var helper = NewMemCreatorHelper()
|
|
var idcreator = uid.NewIdCreator("test", helper)
|
|
|
|
func getId(i, ii int) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
fmt.Println("panic recover:", r)
|
|
}
|
|
}()
|
|
fmt.Printf("%d-%d: %s\n", i, ii, idcreator.GetId())
|
|
}
|
|
|
|
func main() {
|
|
c := atomic.Uint64{}
|
|
st := time.Now()
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1000)
|
|
for i := 0; i < 1000; i++ {
|
|
go func(i int) {
|
|
log.SetLogid(fmt.Sprintf("main idcreator%07d", i))
|
|
for ii := 0; ii < 1000; ii++ {
|
|
getId(i, ii)
|
|
c.Add(uint64(1))
|
|
}
|
|
wg.Done()
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
et := time.Now()
|
|
fmt.Println("cost:", et.Sub(st))
|
|
fmt.Println("helper:", helper)
|
|
fmt.Println("all done, total id number is:", c.Load())
|
|
}
|