chore: for backward compatibility (#4852)

Signed-off-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
Kevin Wan
2025-05-11 20:19:00 +08:00
committed by GitHub
parent 82fe802e81
commit ec989b2e2a
4 changed files with 70 additions and 0 deletions

9
core/syncx/once.go Normal file
View File

@@ -0,0 +1,9 @@
package syncx
import "sync"
// Once returns a func that guarantees fn can only called once.
// Deprecated: use sync.OnceFunc instead.
func Once(fn func()) func() {
return sync.OnceFunc(fn)
}

33
core/syncx/once_test.go Normal file
View File

@@ -0,0 +1,33 @@
package syncx
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOnce(t *testing.T) {
var v int
add := Once(func() {
v++
})
for i := 0; i < 5; i++ {
add()
}
assert.Equal(t, 1, v)
}
func BenchmarkOnce(b *testing.B) {
var v int
add := Once(func() {
v++
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
add()
}
assert.Equal(b, 1, v)
}