mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
temporaryly remove guid.N
This commit is contained in:
@ -11,10 +11,4 @@
|
||||
// This package only provides unique number generation for simple, convenient and most common
|
||||
// usage purpose, but does not provide strict global unique number generation. Please refer
|
||||
// to UUID algorithm for global unique number generation if necessary.
|
||||
//
|
||||
// Unique Number ID:
|
||||
// An Improved SnowFlake ID is inspired by Twitter's Snowflake, which is composed of:
|
||||
// 39 bits for time in units of 10 msec
|
||||
// 16 bits for a machine id
|
||||
// 8 bits for a sequence number.
|
||||
package guid
|
||||
|
||||
@ -1,74 +0,0 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package guid
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
BitLenTime = 39 // Bit length of time.
|
||||
BitLenSequence = 8 // Bit length of sequence number.
|
||||
BitLenMachineID = 63 - BitLenTime - BitLenSequence // Bit length of machine id.
|
||||
)
|
||||
|
||||
var (
|
||||
// MachineId is 16 bits number composed with the last two parts of the first local private ip.
|
||||
// You can change it as your custom machine id in boot time, but do not change it in runtime.
|
||||
// Note that if there's no net card on the machine, it panics when the process boots.
|
||||
MachineId uint16
|
||||
|
||||
// sequenceNumber is used for internal concurrent-safe sequence number counting.
|
||||
sequenceNumber = gtype.NewInt()
|
||||
)
|
||||
|
||||
func init() {
|
||||
if MachineId != 0 {
|
||||
return
|
||||
}
|
||||
ip, err := getPrivateIPv4()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
MachineId = uint16(ip[2])<<8 | uint16(ip[3])
|
||||
}
|
||||
|
||||
// I creates and returns an uint64 id which using improved SnowFlake algorithm.
|
||||
// An Improved SnowFlake ID is composed of:
|
||||
// 39 bits for time in units of 10 msec
|
||||
// 16 bits for a machine id
|
||||
// 8 bits for a sequence number.
|
||||
func I() uint64 {
|
||||
return uint64(time.Now().UnixNano())<<(BitLenMachineID+BitLenSequence) | uint64(MachineId<<BitLenSequence) | uint64(sequenceNumber.Add(1)%0xFF)
|
||||
}
|
||||
|
||||
func getPrivateIPv4() (net.IP, error) {
|
||||
as, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range as {
|
||||
ipNet, ok := a.(*net.IPNet)
|
||||
if !ok || ipNet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := ipNet.IP.To4()
|
||||
if isPrivateIPv4(ip) {
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("no private ip address")
|
||||
}
|
||||
|
||||
func isPrivateIPv4(ip net.IP) bool {
|
||||
return ip != nil &&
|
||||
(ip[0] == 10 || ip[0] == 172 && (ip[1] >= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168)
|
||||
}
|
||||
@ -36,11 +36,3 @@ func Benchmark_S_Data_2(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Benchmark_I(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
guid.I()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ package guid_test
|
||||
import (
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/util/guid"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
@ -33,25 +32,3 @@ func Test_S_Data(t *testing.T) {
|
||||
t.Assert(len(guid.S([]byte("123"))), 32)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_I(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
set = gset.NewSet(true)
|
||||
wg = sync.WaitGroup{}
|
||||
ch = make(chan struct{})
|
||||
)
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
<-ch
|
||||
for i := 0; i < 100000; i++ {
|
||||
t.Assert(set.AddIfNotExist(guid.I()), true)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
close(ch)
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user