From 70d98dd84ee49a4227fe54aba63f2f27a873e8ad Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Mon, 2 Sep 2024 13:30:23 +0800 Subject: [PATCH] build(go): shift Go support window to [1.22, 1.23] (#820) 1. Upgrading `go.mod` to `1.22` 2. Using new `for` syntax like `for i := range N` 3. Migrating random generator from package [`math/rand`](https://pkg.go.dev/math/rand) to package [`math/rand/v2`](https://pkg.go.dev/math/rand/v2@go1.22.0) References: - [Go 1.22 release notes](https://tip.golang.org/doc/go1.22) - [Secure Randomness in Go 1.22](https://tip.golang.org/blog/chacha8rand) Resolves: #812 Signed-off-by: Lixia (Sylvia) Lei --- .github/workflows/build.yml | 2 +- .github/workflows/codeql-analysis.yml | 2 +- README.md | 2 +- go.mod | 2 +- registry/remote/auth/scope.go | 2 +- registry/remote/retry/policy.go | 8 ++++---- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 10f5f9323..6831b6f54 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: ['1.21', '1.22'] + go-version: ['1.22', '1.23'] fail-fast: true steps: - name: Checkout diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0dd22daa7..59a7c0cc7 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -35,7 +35,7 @@ jobs: security-events: write strategy: matrix: - go-version: ['1.21', '1.22'] + go-version: ['1.22', '1.23'] fail-fast: false steps: - name: Checkout repository diff --git a/README.md b/README.md index 7c3013c7a..1bbeb34da 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The ORAS Go library follows [Semantic Versioning](https://semver.org/), where br The version `2` is actively developed in the [`main`](https://github.com/oras-project/oras-go/tree/main) branch with all new features. > [!Note] -> The `main` branch follows [Go's Security Policy](https://github.com/golang/go/security/policy) and supports the two latest versions of Go (currently `1.21` and `1.22`). +> The `main` branch follows [Go's Security Policy](https://github.com/golang/go/security/policy) and supports the two latest versions of Go (currently `1.22` and `1.23`). Examples for common use cases can be found below: diff --git a/go.mod b/go.mod index c7bdde76f..f016aadbb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module oras.land/oras-go/v2 -go 1.21 +go 1.22 require ( github.com/opencontainers/go-digest v1.0.0 diff --git a/registry/remote/auth/scope.go b/registry/remote/auth/scope.go index d81cc0d4f..b2c9bf390 100644 --- a/registry/remote/auth/scope.go +++ b/registry/remote/auth/scope.go @@ -303,7 +303,7 @@ func cleanActions(actions []string) []string { // slow path slices.Sort(actions) n := 0 - for i := 0; i < len(actions); i++ { + for i := range len(actions) { if actions[i] == "*" { return []string{"*"} } diff --git a/registry/remote/retry/policy.go b/registry/remote/retry/policy.go index fe7fadee7..38e015ccf 100644 --- a/registry/remote/retry/policy.go +++ b/registry/remote/retry/policy.go @@ -18,7 +18,7 @@ package retry import ( "hash/maphash" "math" - "math/rand" + "math/rand/v2" "net" "net/http" "strconv" @@ -88,7 +88,7 @@ type Backoff func(attempt int, resp *http.Response) time.Duration // jitter. The backoff is calculated as: // // temp = backoff * factor ^ attempt -// interval = temp * (1 - jitter) + rand.Int63n(2 * jitter * temp) +// interval = temp * (1 - jitter) + rand.Int64N(2 * jitter * temp) // // The HTTP response is checked for a Retry-After header. If it is present, the // value is used as the backoff duration. @@ -96,7 +96,7 @@ func ExponentialBackoff(backoff time.Duration, factor, jitter float64) Backoff { return func(attempt int, resp *http.Response) time.Duration { var h maphash.Hash h.SetSeed(maphash.MakeSeed()) - rand := rand.New(rand.NewSource(int64(h.Sum64()))) + rand := rand.New(rand.NewPCG(h.Sum64(), 0)) // check Retry-After if resp != nil && resp.StatusCode == http.StatusTooManyRequests { @@ -109,7 +109,7 @@ func ExponentialBackoff(backoff time.Duration, factor, jitter float64) Backoff { // do exponential backoff with jitter temp := float64(backoff) * math.Pow(factor, float64(attempt)) - return time.Duration(temp*(1-jitter)) + time.Duration(rand.Int63n(int64(2*jitter*temp))) + return time.Duration(temp*(1-jitter)) + time.Duration(rand.Int64N(int64(2*jitter*temp))) } }