diff --git a/util/guid/guid.go b/util/guid/guid.go index 769cc1d4e..2746c31b9 100644 --- a/util/guid/guid.go +++ b/util/guid/guid.go @@ -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 diff --git a/util/guid/guid_number.go b/util/guid/guid_number.go deleted file mode 100644 index a2459cd86..000000000 --- a/util/guid/guid_number.go +++ /dev/null @@ -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<= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168) -} diff --git a/util/guid/guid_z_bench_test.go b/util/guid/guid_z_bench_test.go index 301ae20cc..07955a648 100644 --- a/util/guid/guid_z_bench_test.go +++ b/util/guid/guid_z_bench_test.go @@ -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() - } - }) -} diff --git a/util/guid/guid_z_unit_test.go b/util/guid/guid_z_unit_test.go index 24bce403b..1b35fdb9a 100644 --- a/util/guid/guid_z_unit_test.go +++ b/util/guid/guid_z_unit_test.go @@ -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() - }) -}