From 04416c74baf17ae96d462476d42a799e9e7b7ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 9 Nov 2024 01:25:30 +0900 Subject: [PATCH] fix: drop linkname to allow exec on go 1.23+ --- .github/workflows/test_and_lint.yml | 4 ++-- utils/crypto/rand.go | 14 -------------- utils/crypto/rand_1.21.go | 23 +++++++++++++++++++++++ utils/crypto/rand_1.22.go | 11 +++++++++++ 4 files changed, 36 insertions(+), 16 deletions(-) delete mode 100644 utils/crypto/rand.go create mode 100644 utils/crypto/rand_1.21.go create mode 100644 utils/crypto/rand_1.22.go diff --git a/.github/workflows/test_and_lint.yml b/.github/workflows/test_and_lint.yml index 2720847c..e63343fa 100644 --- a/.github/workflows/test_and_lint.yml +++ b/.github/workflows/test_and_lint.yml @@ -19,7 +19,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: '1.20' + go-version: '^1.23' cache: false - run: go get - run: go generate ./... @@ -34,7 +34,7 @@ jobs: uses: actions/setup-go@v5 with: cache: false - go-version: '1.20' + go-version: '^1.23' - run: go get - run: go generate ./... - name: Go vet diff --git a/utils/crypto/rand.go b/utils/crypto/rand.go deleted file mode 100644 index 675e5c3e..00000000 --- a/utils/crypto/rand.go +++ /dev/null @@ -1,14 +0,0 @@ -package crypto - -import ( - _ "unsafe" // required by go:linkname -) - -// randuint32 returns a lock free uint32 value. -// -//go:linkname randuint32 runtime.fastrand -func randuint32() uint32 - -func RandU32() uint32 { - return randuint32() -} diff --git a/utils/crypto/rand_1.21.go b/utils/crypto/rand_1.21.go new file mode 100644 index 00000000..1db54d2b --- /dev/null +++ b/utils/crypto/rand_1.21.go @@ -0,0 +1,23 @@ +//go:build !go1.22 + +package crypto + +import ( + _ "unsafe" // required by go:linkname +) + +// randuint32 returns a lock free uint32 value. +// +// Too much legacy code has go:linkname references +// to runtime.fastrand and friends, so keep these around for now. +// Code should migrate to math/rand/v2.Uint64, +// which is just as fast, but that's only available in Go 1.22+. +// It would be reasonable to remove these in Go 1.24. +// Do not call these from package runtime. +// +//go:linkname randuint32 runtime.fastrand +func randuint32() uint32 + +func RandU32() uint32 { + return randuint32() +} diff --git a/utils/crypto/rand_1.22.go b/utils/crypto/rand_1.22.go new file mode 100644 index 00000000..e29278fc --- /dev/null +++ b/utils/crypto/rand_1.22.go @@ -0,0 +1,11 @@ +//go:build go1.22 + +package crypto + +import ( + "math/rand/v2" +) + +func RandU32() uint32 { + return rand.Uint32() +}