GithubHelp home page GithubHelp logo

go-concurrency-patterns's Introduction

Go Concurrency Patterns

This repository collects common concurrency patterns in Golang

Materials

Context:

Name Description Playground
1-boring A hello world to goroutine play
2-chan A hello world to go channel play
3-generator A python-liked generator play
4-fanin Fan in pattern play
5-restore-sequence Restore sequence play
6-select-timeout Add Timeout to a goroutine play
7-quit-signal Quit signal play
8-daisy-chan Daisy chan pattern play
9-google1.0 Build a concurrent google search from the ground-up play
10-google2.0 Build a concurrent google search from the ground-up play
11-google2.1 Build a concurrent google search from the ground-up play
12-google3.0 Build a concurrent google search from the ground-up play
13-adv-pingpong A sample ping-pong table implemented in goroutine play
14-adv-subscription Subscription play
15-bounded-parallelism Bounded parallelism play
16-context How to user context in HTTP client and server play
17-ring-buffer-channel Ring buffer channel play
18-worker-pool worker pool pattern play

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

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

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.

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.

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.