Add stateful goroutines

This commit is contained in:
Sangeeth Sudheer 2024-05-01 17:32:24 +05:30
parent 58ffd80d36
commit 5e76002027
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 82 additions and 2 deletions

View File

@ -16,7 +16,8 @@ package main
// import "git.sangeeth.dev/gobyexample/waitgroups" // import "git.sangeeth.dev/gobyexample/waitgroups"
// import "git.sangeeth.dev/gobyexample/ratelimiting" // import "git.sangeeth.dev/gobyexample/ratelimiting"
// import "git.sangeeth.dev/gobyexample/atomics" // import "git.sangeeth.dev/gobyexample/atomics"
import "git.sangeeth.dev/gobyexample/mutex" // import "git.sangeeth.dev/gobyexample/mutex"
import "git.sangeeth.dev/gobyexample/statefulgoroutines"
func main() { func main() {
// runes.Runes() // runes.Runes()
@ -35,5 +36,6 @@ func main() {
// waitgroups.WaitGroups() // waitgroups.WaitGroups()
// ratelimiting.RateLimiting() // ratelimiting.RateLimiting()
// atomics.Atomics() // atomics.Atomics()
mutex.Mutex() // mutex.Mutex()
statefulgoroutines.StatefulGoroutines()
} }

View File

@ -0,0 +1,78 @@
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())
}