diff --git a/.github/workflows/modernize.yml b/.github/workflows/modernize.yml index 69b033f..a08fe2b 100644 --- a/.github/workflows/modernize.yml +++ b/.github/workflows/modernize.yml @@ -27,7 +27,7 @@ jobs: - uses: actions/setup-go@v6 with: # NOTE: Keep this in sync with the version from go.mod - go-version: "1.24.x" + go-version: "1.25.x" cache: false - name: modernize diff --git a/common.go b/common.go index 40ae15a..ae0cda0 100644 --- a/common.go +++ b/common.go @@ -7,8 +7,6 @@ package utils import ( "crypto/rand" "encoding/base64" - "encoding/binary" - "encoding/hex" "fmt" "math" "net" @@ -17,8 +15,6 @@ import ( "runtime" "slices" "strings" - "sync" - "sync/atomic" "github.com/google/uuid" ) @@ -42,50 +38,6 @@ var whitespaceTable = [256]bool{ ' ': true, // 32 - space } -// Copyright © 2014, Roger Peppe -// github.com/rogpeppe/fastuuid -// All rights reserved. - -var ( - uuidSeed [24]byte - uuidCounter uint64 - uuidSetup sync.Once -) - -// UUID generates an universally unique identifier (UUID) -func UUID() string { - // Setup seed & counter once - uuidSetup.Do(func() { - if _, err := rand.Read(uuidSeed[:]); err != nil { - panic(fmt.Errorf("utils: failed to seed UUID generator: %w", err)) - } - uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8]) - }) - // first 8 bytes differ, taking a slice of the first 16 bytes - x := atomic.AddUint64(&uuidCounter, 1) - _uuid := uuidSeed - binary.LittleEndian.PutUint64(_uuid[:8], x) - _uuid[6], _uuid[9] = _uuid[9], _uuid[6] - - // RFC4122 v4 - _uuid[6] = (_uuid[6] & 0x0f) | 0x40 - _uuid[8] = _uuid[8]&0x3f | 0x80 - - // create UUID representation of the first 128 bits - b := make([]byte, 36) - hex.Encode(b[0:8], _uuid[0:4]) - b[8] = '-' - hex.Encode(b[9:13], _uuid[4:6]) - b[13] = '-' - hex.Encode(b[14:18], _uuid[6:8]) - b[18] = '-' - hex.Encode(b[19:23], _uuid[8:10]) - b[23] = '-' - hex.Encode(b[24:], _uuid[10:16]) - - return UnsafeString(b) -} - // UUIDv4 returns a Random (Version 4) UUID. // The strength of the UUIDs is based on the strength of the crypto/rand package. func UUIDv4() string { diff --git a/common_test.go b/common_test.go index 58039ea..73b481f 100644 --- a/common_test.go +++ b/common_test.go @@ -5,9 +5,7 @@ package utils import ( - "crypto/rand" "errors" - "fmt" "net" "os" "testing" @@ -17,7 +15,7 @@ import ( func Test_FunctionName(t *testing.T) { t.Parallel() - require.Equal(t, "github.com/gofiber/utils/v2.Test_UUID", FunctionName(Test_UUID)) + require.Equal(t, "github.com/gofiber/utils/v2.Test_UUIDv4", FunctionName(Test_UUIDv4)) require.Equal(t, "github.com/gofiber/utils/v2.Test_FunctionName.func1", FunctionName(func() {})) dummyint := 20 @@ -50,31 +48,6 @@ func Test_FunctionName(t *testing.T) { require.Equal(t, "*utils.sampleStruct", FunctionName(&s)) } -func Test_UUID(t *testing.T) { - t.Parallel() - res := UUID() - require.Len(t, res, 36) - require.NotEqual(t, "00000000-0000-0000-0000-000000000000", res) -} - -func Test_UUID_Concurrency(t *testing.T) { - t.Parallel() - iterations := 1000 - var res string - ch := make(chan string, iterations) - results := make(map[string]string) - for i := 0; i < iterations; i++ { - go func() { - ch <- UUID() - }() - } - for i := 0; i < iterations; i++ { - res = <-ch - results[res] = res - } - require.Len(t, results, iterations) -} - func Test_UUIDv4(t *testing.T) { t.Parallel() res := UUIDv4() @@ -344,25 +317,6 @@ func Benchmark_ConvertToBytes(b *testing.B) { }) } -// go test -v -run=^$ -bench=Benchmark_UUID -benchmem -count=2 -func Benchmark_UUID(b *testing.B) { - var res string - b.Run("fiber", func(b *testing.B) { - for n := 0; n < b.N; n++ { - res = UUID() - } - require.Len(b, res, 36) - }) - b.Run("default", func(b *testing.B) { - rnd := make([]byte, 16) - _, _ = rand.Read(rnd) //nolint: errcheck // No need to check error - for n := 0; n < b.N; n++ { - res = fmt.Sprintf("%x-%x-%x-%x-%x", rnd[0:4], rnd[4:6], rnd[6:8], rnd[8:10], rnd[10:]) - } - require.Len(b, res, 36) - }) -} - // go test -v -run=^$ -bench=Benchmark_GenerateSecureToken -benchmem -count=2 func Benchmark_GenerateSecureToken(b *testing.B) { var res string