gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/myx/identx/ident.go (about) 1 package identx 2 3 import ( 4 "gitee.com/zhongguo168a/gocodes/datax/stringx" 5 "gitee.com/zhongguo168a/gocodes/myx/scale" 6 "math/rand" 7 "sync" 8 "sync/atomic" 9 "time" 10 ) 11 12 func init() { 13 rander = rand.New(rand.NewSource(time.Now().UnixNano())) 14 lastTimestamp = timeGen() 15 updateTime() 16 resetObjectIDCounter() 17 18 //SetNode(3844 + rander.Intn(238328-3844)) 19 } 20 21 var rander *rand.Rand 22 var objectIDCounter = uint32(0) 23 var mutex sync.Mutex 24 25 //func readRandomUint32() uint32 { 26 // var b [4]byte 27 // _, err := io.ReadFull(rand.Reader, b[:]) 28 // if err != nil { 29 // panic(fmt.Errorf("cannot initialize objectid package with crypto.rand.Reader: %v", err)) 30 // } 31 // 32 // return (uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24) 33 //} 34 // 35 //func readRandomUint16() uint16 { 36 // var b [2]byte 37 // _, err := io.ReadFull(rand.Reader, b[:]) 38 // if err != nil { 39 // panic(fmt.Errorf("cannot initialize objectid package with crypto.rand.Reader: %v", err)) 40 // } 41 // 42 // return (uint16(b[0]) << 0) | (uint16(b[1]) << 8) 43 //} 44 45 func resetObjectIDCounter() { 46 objectIDCounter = uint32(3844 + rander.Intn(65535-3844)) 47 } 48 49 func SetNode(val string) { 50 //node = scale.DecimalTo(val, scaleN) 51 //node = stringx.FillFirst(node, 3, "0") 52 node = val 53 } 54 55 func ParseTime(ident string) int64 { 56 lastident := make([]byte, len(ident)) 57 lastident[0] = ident[0] 58 for i := 1; i < len(lastident); i++ { 59 c := ident[i] 60 lastident[i] = normalChar(ident[0], c) 61 } 62 ts := string(lastident[:6]) 63 ts = stringx.Reverse(ts) 64 return 0 65 } 66 67 var ( 68 node string 69 lastTimestamp int64 70 71 // 固定长度6 72 timeString string 73 scaleN = 62 74 // 每N次更新一次 [objectIDCounter] 75 countChange = daySecond 76 ) 77 78 const daySecond = 10000 79 80 func updateTime() { 81 timeString = scale.DecimalTo(int(lastTimestamp), scaleN) 82 timeString = stringx.FillFirst(timeString, 6, "0") 83 timeString = stringx.Reverse(timeString) 84 } 85 86 func timeGen() int64 { 87 //return 1637829423 88 //return slowtime.Now().Unix() 89 return time.Now().Unix() 90 } 91 92 // GenOne [tag] 用于标识字符串的用途 93 func GenOne() string { 94 mutex.Lock() 95 defer mutex.Unlock() 96 now := timeGen() 97 if now > lastTimestamp { 98 lastTimestamp = now 99 resetObjectIDCounter() 100 updateTime() 101 102 } 103 count := atomic.AddUint32(&objectIDCounter, 1) 104 countString := scale.DecimalTo(int(count), scaleN) 105 countLength := scale.DecimalTo(len(countString), scaleN) 106 //countString = stringx.FillFirst(countString, 4, "0") 107 ident := timeString + countLength + countString + node 108 //lastident := make([]byte, len(ident)) 109 //ruleChar := ident[0] 110 //lastident[0] = ruleChar 111 //for i := 1; i < len(lastident); i++ { 112 // c := ident[i] 113 // lastident[i] = confuseChar(ruleChar, c) 114 //} 115 //if _, ok := congfu.Load(string(lastident)); ok { 116 // fmt.Println("chongfu2", ident, string(lastident)) 117 //} 118 //congfu.Store(string(lastident), true) 119 //ident = string(lastident) 120 121 // 6+1+4 = 12位 122 return ident 123 }