Skip to content

Commit e06fde8

Browse files
committed
Add staticcheck, simplify ci, fix races & issues
This commit adds staticcheck as a tool to be ran as part of CI. It also modifies the CI to be a single job to make caching better. Following the last commit, a data race has been fixed & the bytepool package now handles pointers correctly. Signed-off-by: David Bond <[email protected]>
1 parent fb74305 commit e06fde8

File tree

6 files changed

+43
-50
lines changed

6 files changed

+43
-50
lines changed

.github/workflows/go.yml

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ on:
2222
- '.github/workflows/go.yml'
2323

2424
jobs:
25-
mod:
25+
ci:
2626
runs-on: ubuntu-latest
2727
steps:
2828
- name: Checkout
@@ -34,44 +34,19 @@ jobs:
3434
go-version-file: go.mod
3535
cache: true
3636

37-
- name: Download Modules
37+
- name: Download modules
3838
run: go mod download
39+
40+
- name: Run static analysis
41+
run: go tool staticcheck ./...
3942

40-
test:
41-
runs-on: ubuntu-latest
42-
needs:
43-
- mod
44-
steps:
45-
- name: Checkout
46-
uses: actions/[email protected]
47-
48-
- name: Install Go
49-
uses: actions/setup-go@v6
50-
with:
51-
go-version-file: go.mod
52-
cache: true
53-
43+
- name: Run generators
44+
run: go generate ./...
45+
5446
- name: Run tests
5547
run: go test -race ./...
5648

57-
generate:
58-
runs-on: ubuntu-latest
59-
needs:
60-
- mod
61-
steps:
62-
- name: Checkout
63-
uses: actions/[email protected]
64-
65-
- name: Install Go
66-
uses: actions/setup-go@v6
67-
with:
68-
go-version-file: go.mod
69-
cache: true
70-
71-
- name: Generate
72-
run: go generate ./...
73-
74-
- name: Check Changes
49+
- name: Check for changed files
7550
run: |
7651
git add .
7752
git diff --staged --exit-code

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
connectrpc.com/connect v1.19.1 // indirect
2929
connectrpc.com/otelconnect v0.8.0 // indirect
3030
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
31+
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
3132
github.com/Microsoft/go-winio v0.6.2 // indirect
3233
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
3334
github.com/bufbuild/buf v1.58.0 // indirect
@@ -92,6 +93,7 @@ require (
9293
go.uber.org/multierr v1.11.0 // indirect
9394
go.uber.org/zap v1.27.0 // indirect
9495
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9 // indirect
96+
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 // indirect
9597
golang.org/x/mod v0.28.0 // indirect
9698
golang.org/x/net v0.46.0 // indirect
9799
golang.org/x/sys v0.37.0 // indirect
@@ -102,11 +104,13 @@ require (
102104
google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff // indirect
103105
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect
104106
gopkg.in/yaml.v3 v3.0.1 // indirect
107+
honnef.co/go/tools v0.6.1 // indirect
105108
pluginrpc.com/pluginrpc v0.5.0 // indirect
106109
)
107110

108111
tool (
109112
github.com/bufbuild/buf/cmd/buf
110113
google.golang.org/grpc/cmd/protoc-gen-go-grpc
111114
google.golang.org/protobuf/cmd/protoc-gen-go
115+
honnef.co/go/tools/cmd/staticcheck
112116
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ connectrpc.com/otelconnect v0.8.0 h1:a4qrN4H8aEE2jAoCxheZYYfEjXMgVPyL9OzPQLBEFXU
3030
connectrpc.com/otelconnect v0.8.0/go.mod h1:AEkVLjCPXra+ObGFCOClcJkNjS7zPaQSqvO0lCyjfZc=
3131
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
3232
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
33+
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
34+
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3335
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
3436
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
3537
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
@@ -212,6 +214,8 @@ golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
212214
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
213215
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9 h1:TQwNpfvNkxAVlItJf6Cr5JTsVZoC/Sj7K3OZv2Pc14A=
214216
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9/go.mod h1:TwQYMMnGpvZyc+JpB/UAuTNIsVJifOlSkrZkhcvpVUk=
217+
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=
218+
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
215219
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
216220
golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
217221
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
@@ -232,6 +236,8 @@ golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2M
232236
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
233237
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
234238
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
239+
golang.org/x/tools/go/expect v0.1.1-deprecated h1:jpBZDwmgPhXsKZC6WhL20P4b/wmnpsEAGHaNy0n/rJM=
240+
golang.org/x/tools/go/expect v0.1.1-deprecated/go.mod h1:eihoPOH+FgIqa3FpoTwguz/bVUSGBlGQU67vpBeOrBY=
235241
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
236242
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
237243
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
@@ -253,5 +259,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
253259
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
254260
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
255261
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
262+
honnef.co/go/tools v0.6.1 h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=
263+
honnef.co/go/tools v0.6.1/go.mod h1:3puzxxljPCe8RGJX7BIy1plGbxEOZni5mR2aXe3/uk4=
256264
pluginrpc.com/pluginrpc v0.5.0 h1:tOQj2D35hOmvHyPu8e7ohW2/QvAnEtKscy2IJYWQ2yo=
257265
pluginrpc.com/pluginrpc v0.5.0/go.mod h1:UNWZ941hcVAoOZUn8YZsMmOZBzbUjQa3XMns8RQLp9o=

internal/bytepool/bytepool.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ func New(length int) *Pool {
1717
return &Pool{
1818
Pool: sync.Pool{
1919
New: func() any {
20-
return make([]byte, length)
20+
b := make([]byte, length)
21+
return &b
2122
},
2223
},
2324
}
2425
}
2526

2627
// Get a byte slice.
27-
func (p *Pool) Get() []byte {
28-
buf := p.Pool.Get().([]byte)
28+
func (p *Pool) Get() *[]byte {
29+
buf := p.Pool.Get().(*[]byte)
2930

3031
return buf
3132
}
3233

3334
// Put a byte slice back into the pool for later use.
34-
func (p *Pool) Put(buf []byte) {
35+
func (p *Pool) Put(buf *[]byte) {
3536
p.Pool.Put(buf)
3637
}

staticcheck.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
checks = ["all", "-ST1000"]

whisper.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
mathrand "math/rand/v2"
2626
"net"
2727
"sync"
28+
"sync/atomic"
2829
"time"
2930

3031
"golang.org/x/crypto/hkdf"
@@ -67,9 +68,9 @@ type (
6768

6869
// Used to signal the node is up and running
6970
ready chan struct{}
70-
listeningUDP bool
71-
listeningTCP bool
72-
gossiping bool
71+
listeningUDP atomic.Bool
72+
listeningTCP atomic.Bool
73+
gossiping atomic.Bool
7374
}
7475
)
7576

@@ -147,15 +148,17 @@ func (n *Node) Ready(ctx context.Context) error {
147148
}
148149

149150
func (n *Node) reportReadiness() {
150-
if n.gossiping && n.listeningUDP && n.listeningTCP {
151+
if n.gossiping.Load() && n.listeningUDP.Load() && n.listeningTCP.Load() {
151152
n.ready <- struct{}{}
152153
}
153154
}
154155

156+
// ID returns the node's identifier.
155157
func (n *Node) ID() uint64 {
156158
return n.id
157159
}
158160

161+
// Address returns the address the node is advertising to peers.
159162
func (n *Node) Address() string {
160163
return n.address
161164
}
@@ -311,7 +314,7 @@ func (n *Node) listenTCP(ctx context.Context) error {
311314
group, ctx := errgroup.WithContext(ctx)
312315

313316
group.Go(func() error {
314-
n.listeningTCP = true
317+
n.listeningTCP.Store(true)
315318
n.reportReadiness()
316319

317320
return server.Serve(tcp)
@@ -320,7 +323,7 @@ func (n *Node) listenTCP(ctx context.Context) error {
320323
group.Go(func() error {
321324
<-ctx.Done()
322325

323-
n.listeningTCP = false
326+
n.listeningTCP.Store(true)
324327
server.GracefulStop()
325328
return tcp.Close()
326329
})
@@ -334,7 +337,7 @@ func (n *Node) listenUDP(ctx context.Context) error {
334337
return fmt.Errorf("failed to listen on port %d: %w", n.port, err)
335338
}
336339

337-
n.listeningUDP = true
340+
n.listeningUDP.Store(true)
338341
n.reportReadiness()
339342

340343
var (
@@ -347,7 +350,7 @@ func (n *Node) listenUDP(ctx context.Context) error {
347350
for {
348351
select {
349352
case <-ctx.Done():
350-
n.listeningUDP = false
353+
n.listeningUDP.Store(false)
351354
return udp.Close()
352355
default:
353356
if err = udp.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
@@ -357,7 +360,7 @@ func (n *Node) listenUDP(ctx context.Context) error {
357360

358361
buf := n.bytes.Get()
359362

360-
length, _, err := udp.ReadFromUDP(buf)
363+
length, _, err := udp.ReadFromUDP(*buf)
361364
switch {
362365
case errors.As(err, &netErr) && netErr.Timeout():
363366
n.bytes.Put(buf)
@@ -368,12 +371,13 @@ func (n *Node) listenUDP(ctx context.Context) error {
368371
continue
369372
}
370373

374+
payload := *buf
371375
group.Add(1)
372376
go func(payload []byte) {
373377
defer group.Done()
374378
defer n.bytes.Put(buf)
375379
n.handlePeerMessage(ctx, payload)
376-
}(buf[:length])
380+
}(payload[:length])
377381
}
378382
}
379383
}
@@ -570,13 +574,13 @@ func (n *Node) gossip(ctx context.Context) error {
570574
checkTicker := time.NewTicker(time.Minute / 2)
571575
defer checkTicker.Stop()
572576

573-
n.gossiping = true
577+
n.gossiping.Store(true)
574578
n.reportReadiness()
575579

576580
for {
577581
select {
578582
case <-ctx.Done():
579-
n.gossiping = false
583+
n.gossiping.Store(false)
580584
return ctx.Err()
581585
case <-stateTicker.C:
582586
if err := n.shareState(ctx); err != nil {

0 commit comments

Comments
 (0)