Skip to content

Commit afef0a6

Browse files
authored
ai/live: Update to golang 1.25 (#3745)
The golang 1.25 update turned out into something of an ordeal, so splitting it up from #3739 to allow for better review. While everything compiled in 1.25, some tests and CI steps broke. * Go lint needed to be updated to v2+ to support golang 1.25 * Bumping go vet from v1 to v2 entailed a bunch of CLI changes. Tried to make those 1-to-1 as much as possible, however: Go fmt now needs to be invoked separately for golint. We check go fmt in the CI step right before lint ... so just remove the go fmt step? The revive linter flagged a ton of missing comments. But it seems the linter only applies to the `pm` and `verification` packages which hardly change. Rather than update those packages, remove the use of the `revive` linter. * Some linting step (not sure which one? vet?) now complains about format string functions being used incorrectly, eg using a variable in place of a format string literal. Those were fixed in the code. * The `rand.Seed` function apparently became a no-op as of golang 1.24 and that broke most of our tests that use the RNG. Removed the init() function that sets the seed, and added a package level RNG context (using the same seed as the old init) and updated any failing tests to use that context. Not all uses of the RNG were updated, just ones that broke tests. The global RNG is safely initialized so we can continue to use it by default in the code that still uses it without test coverage. * Fix race condition in discovery uncovered by golang 1.25
1 parent 08a656f commit afef0a6

File tree

16 files changed

+42
-43
lines changed

16 files changed

+42
-43
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ jobs:
108108
git diff --exit-code
109109
110110
- name: Lint
111-
uses: golangci/golangci-lint-action@v4
111+
uses: golangci/golangci-lint-action@v8
112112
with:
113-
version: v1.61.0
113+
version: v2.4.0
114114
skip-pkg-cache: true
115-
args: '--out-format=colored-line-number --disable-all --enable=gofmt --enable=govet --enable=revive --timeout=4m pm verification'
115+
args: '--output.text.path=stdout --output.text.colors=true --output.text.print-issued-lines=true --default=none --enable=govet --timeout=4m pm verification'
116116

117117
- name: Run Revive Action by building from repository
118118
uses: docker://morphy/revive-action:v2

cmd/livepeer/starter/starter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ func StartLivepeer(ctx context.Context, cfg LivepeerConfig) {
748748
}
749749
}
750750
} else {
751-
glog.Exit(fmt.Errorf(err.Error()))
751+
glog.Exit(err)
752752
}
753753

754754
//Get the Eth client connection information

cmd/livepeer_cli/wizard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (w *wizard) readDefaultInt(def int) int {
164164

165165
func (w *wizard) readBigInt(prompt string) *big.Int {
166166
for {
167-
fmt.Printf(fmt.Sprintf("%s - > ", prompt))
167+
fmt.Print(fmt.Sprintf("%s - > ", prompt))
168168
text, err := w.in.ReadString('\n')
169169
if err != nil {
170170
log.Crit("Failed to read user input", "err", err)

cmd/livepeer_cli/wizard_transcoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func (w *wizard) setMaxSessions() {
427427
}
428428
result, ok := httpPostWithParams(fmt.Sprintf("http://%v:%v/setMaxSessions", w.host, w.httpPort), data)
429429
if ok {
430-
fmt.Printf(result)
430+
fmt.Print(result)
431431
return
432432
} else {
433433
fmt.Printf("Error setting max sessions: %v", result)

common/util.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"context"
5+
"encoding/binary"
56
"encoding/hex"
67
"encoding/json"
78
"fmt"
@@ -88,10 +89,6 @@ var (
8889
}
8990
)
9091

91-
func init() {
92-
rand.Seed(time.Now().UnixNano())
93-
}
94-
9592
func ParseBigInt(num string) (*big.Int, error) {
9693
bigNum := new(big.Int)
9794
_, ok := bigNum.SetString(num, 10)
@@ -312,6 +309,9 @@ func ratToFixed(rat *big.Rat, scalingFactor int64) (int64, error) {
312309
return fp, nil
313310
}
314311

312+
// Package-level RNG; tests can override it.
313+
var PkgRNG = rand.New(rand.NewSource(time.Now().UnixNano()))
314+
315315
// RandomIDGenerator generates random hexadecimal string of specified length
316316
// defined as variable for unit tests
317317
var RandomIDGenerator = func(length uint) string {
@@ -320,9 +320,7 @@ var RandomIDGenerator = func(length uint) string {
320320

321321
var RandomBytesGenerator = func(length uint) []byte {
322322
x := make([]byte, length, length)
323-
for i := 0; i < len(x); i++ {
324-
x[i] = byte(rand.Uint32())
325-
}
323+
PkgRNG.Read(x)
326324
return x
327325
}
328326

@@ -335,6 +333,10 @@ var RandomUintUnder = func(max uint) uint {
335333
return uint(rand.Uint32()) % max
336334
}
337335

336+
func RandomUint64() uint64 {
337+
return binary.LittleEndian.Uint64(RandomBytesGenerator(8))
338+
}
339+
338340
func ToInt64(val *big.Int) int64 {
339341
if val.Cmp(big.NewInt(maxInt64)) > 0 {
340342
return maxInt64

core/os.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func downloadDataHTTP(ctx context.Context, uri string) ([]byte, error) {
8484
defer resp.Body.Close()
8585
if resp.StatusCode != 200 {
8686
clog.Errorf(ctx, "Non-200 response for status=%v uri=%s", resp.Status, uri)
87-
return nil, fmt.Errorf(resp.Status)
87+
return nil, fmt.Errorf("%v", resp.Status)
8888
}
8989
body, err := common.ReadAtMost(resp.Body, common.MaxSegSize)
9090
if err != nil {

core/stream_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,18 @@ func TestSegmentFlatten(t *testing.T) {
4848
}
4949

5050
func TestRandomIdGenerator(t *testing.T) {
51-
rand.Seed(123)
51+
common.PkgRNG = rand.New(rand.NewSource(123))
5252
res := common.RandomIDGenerator(DefaultManifestIDLength)
53-
if res != "17b336b6" {
54-
t.Error("Unexpected RNG result")
55-
}
53+
assert.Equal(t, "f1405ced", res, "Unexpected RNG result")
5654
}
5755

5856
func TestStreamID(t *testing.T) {
59-
rand.Seed(123)
57+
common.PkgRNG = rand.New(rand.NewSource(123))
6058
mid := RandomManifestID()
6159
profile := ffmpeg.P144p30fps16x9
6260

6361
// Test random manifest ID generation
64-
if string(mid) != "17b336b6" {
65-
t.Error("Unexpected ManifestID ", mid)
66-
}
62+
assert.Equal(t, ManifestID("f1405ced"), mid, "Unexpected ManifestID")
6763

6864
// Test normal cases
6965
id := MakeStreamID(mid, &profile)

discovery/wh_discovery.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ func (w *webhookPool) getInfos() ([]common.OrchestratorLocalInfo, error) {
5959
}
6060

6161
hash := ethcommon.BytesToHash(crypto.Keccak256(body))
62+
w.mu.Lock()
6263
if hash == w.responseHash {
63-
w.mu.Lock()
6464
w.lastRequest = time.Now()
6565
pool = w.pool // may have been reset since beginning
6666
w.mu.Unlock()
6767
return pool.GetInfos(), nil
6868
}
69+
w.mu.Unlock()
6970

7071
infos, err := deserializeWebhookJSON(body)
7172
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/livepeer/go-livepeer
22

3-
go 1.23.2
3+
go 1.25.0
44

55
require (
66
contrib.go.opencensus.io/exporter/prometheus v0.4.2

server/ai_http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"encoding/base64"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"image"
1213
"io"
@@ -703,7 +704,7 @@ func (h *lphttp) AIResults() http.Handler {
703704
glog.Errorf("Unable to read ai worker error body taskId=%v err=%q", tid, err)
704705
workerResult.Err = err
705706
} else {
706-
workerResult.Err = fmt.Errorf(string(body))
707+
workerResult.Err = errors.New(string(body))
707708
}
708709
glog.Errorf("AI Worker error for taskId=%v err=%q", tid, workerResult.Err)
709710
orch.AIResults(tid, &workerResult)

0 commit comments

Comments
 (0)