Skip to content

Commit 74e1a69

Browse files
authored
Merge pull request #1009 from onflow/holyfuchs/atomic-greeting-generator
Make Greetings generator counter thread-safe
2 parents 2992f32 + 10d500f commit 74e1a69

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

test/greetings.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ package test
2020

2121
import (
2222
"math/rand"
23-
"time"
23+
"sync/atomic"
2424
)
2525

2626
var GreetingScript = []byte(`
2727
transaction(greeting: String) {
28-
execute {
29-
log(greeting.concat(", World!"))
28+
execute {
29+
log(greeting.concat(", World!"))
3030
}
3131
}
3232
`)
@@ -135,21 +135,19 @@ func init() {
135135
greetingMsgUK,
136136
greetingMsgES,
137137
}
138-
139-
rand.Seed(time.Now().Unix())
140138
}
141139

142140
type Greetings struct {
143-
count int
141+
count atomic.Uint32
144142
}
145143

146144
func GreetingGenerator() *Greetings {
147-
return &Greetings{0}
145+
return &Greetings{}
148146
}
149147

150148
func (g *Greetings) New() string {
151-
defer func() { g.count++ }()
152-
return greetings[g.count%len(greetings)]
149+
count := g.count.Add(1)
150+
return greetings[count%uint32(len(greetings))]
153151
}
154152

155153
func (g *Greetings) Random() string {

0 commit comments

Comments
 (0)