lotusirous / go-concurrency-patterns Goto Github PK
View Code? Open in Web Editor NEWConcurrency patterns in Go
Concurrency patterns in Go
why does one worker complete eight tasks?
Hello, I have tested your ring buffer, and the output consisted of 5 values:
2024/04/03 07:39:49 5
2024/04/03 07:39:49 6
2024/04/03 07:39:49 7
2024/04/03 07:39:49 8
2024/04/03 07:39:49 9
However, the buffer for the channel was set to 4. Therefore, I find this response somewhat unclear.
This is a bug surely? Another routine could write between these two calls, and then this write would block.
A safer approach would be to drop the current write AND one from the ring buffer. This would ensure that no code ever blocks, but you are still guaranteed new data (which IMO is a big use-case for ring buffers).
This would ensure no back-pressure. It would make starvation technically possible very rarely by some complex timing and workload patterns if you have too many writers for the size of the ring buffer, but this is trivial to avoid by sizing the ring buffer appropriately.
So as long as your ring buffer isn't too small, I think it would behave more like what people would expect.
go-concurrency-patterns/6-select-timeout/main.go
Lines 25 to 35 in 2c16586
We always think timeout
as the time that one service has on response. In this case, timeout
's meaning should be ** we don't receive any message from c
channel for 5 seconds.
so maybe the right code is :
timeout := time.After(5 * time.Second)
for {
select {
case s := <-c:
fmt.Println(s)
timeout = time.After(5 * time.Second) // update timeout time
case <-timeout:
fmt.Println("channel has no response for 5 seconds")
return
}
}
the origin code seems that you want to receive from every channel for same time, like linux cfs scheduler. so maybe in that case we should use ticker
package main
import (
"fmt"
"time"
)
func boring(id int) <-chan string {
c := make(chan string)
go func() {
c <- "" // skip the first time
for i := 0; ; i++ {
c <- fmt.Sprintf("%d, %s", id, time.Now().Format("2006-01-02 15:04:05"))
time.Sleep(1 * time.Second)
}
}()
return c
}
func main() {
timeout := time.NewTicker(5 * time.Second)
c1 := boring(1)
c2 := boring(2)
jobchannels := []<-chan string{c1, c2}
i := 0
for {
select {
case s := <-jobchannels[i]:
fmt.Println(s)
case <-timeout.C:
fmt.Printf("%d has talk for 5 secs\n", i+1)
i = (i + 1) % len(jobchannels)
}
}
}
and it's output is
1, 2021-03-17 11:27:03
1, 2021-03-17 11:27:04
1, 2021-03-17 11:27:05
1, 2021-03-17 11:27:06
1, 2021-03-17 11:27:07
1 has talk for 5 secs
2, 2021-03-17 11:27:08
2, 2021-03-17 11:27:09
2, 2021-03-17 11:27:10
2, 2021-03-17 11:27:11
2, 2021-03-17 11:27:12
2 has talk for 5 secs
1, 2021-03-17 11:27:08
1, 2021-03-17 11:27:14
1, 2021-03-17 11:27:15
1, 2021-03-17 11:27:16
....
The main in item 14 https://github.com/lotusirous/go-concurrency-patterns/blob/main/14-adv-subscription/main.go is not implemented.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.