gobyexample/statefulgoroutines/statefulgoroutines.go

79 lines
1.2 KiB
Go
Raw Normal View History

2024-05-01 12:02:24 +00:00
package statefulgoroutines
import (
"fmt"
"math/rand"
"sync/atomic"
"time"
)
type readOp struct {
key int
resp chan int
}
type writeOp struct {
key int
value int
resp chan int
}
func StatefulGoroutines() {
var readCounts, writeCounts atomic.Uint64
reads := make(chan readOp)
writes := make(chan writeOp)
go func() {
state := map[int]int{}
for {
select {
case r := <-reads:
r.resp <- state[r.key]
case w := <-writes:
state[w.key] = w.value
w.resp <- state[w.key]
}
}
}()
// Spawn goroutines to do reads
for range 100 {
go func() {
for {
r := readOp{
key: rand.Intn(5),
resp: make(chan int),
}
reads <- r
<-r.resp
readCounts.Add(1)
time.Sleep(time.Millisecond)
}
}()
}
// Spawn goroutines to do writes
for range 10 {
go func() {
for {
w := writeOp{
key: rand.Intn(5),
value: rand.Intn(100),
resp: make(chan int),
}
writes <- w
<-w.resp
writeCounts.Add(1)
time.Sleep(time.Millisecond)
}
}()
}
time.Sleep(time.Second)
fmt.Printf("# of reads => %d\n", readCounts.Load())
fmt.Printf("# of writes => %d\n", writeCounts.Load())
}