Code Monkey home page Code Monkey logo

go-concurrency-patterns's People

Contributors

dmitryburov avatar lotusirous avatar mariozig avatar theruziev avatar tomoyamachi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-concurrency-patterns's Issues

The current implementation of the ring buffer is not very clear.

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.

Different approach for ring buffer

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.

timeout example is unsuitable

// timeout for the whole conversation
timeout := time.After(5 * time.Second)
for {
select {
case s := <-c:
fmt.Println(s)
case <-timeout:
fmt.Println("You talk too much.")
return
}
}

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
....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.