Best way to implement global counters for highly concurrent applications?

前端 未结 9 1194
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 02:07

What is the best way to implement global counters for a highly concurrent application? In my case I may have 10K-20K go routines performing \"work\", and I want to count th

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 02:45

    The last one was close:

    package main
    
    import "fmt"
    
    func main() {
        ch := make(chan int, 3)
        go GoCounterRoutine(ch)
        go GoWorkerRoutine(1, ch)
        // not run as goroutine because mein() would just end
        GoWorkerRoutine(2, ch)
    
    }
    
    // started somewhere else
    func GoCounterRoutine(ch chan int) {
        counter := 0
        for {
            ch <- counter
            counter += 1
        }
    }
    
    func GoWorkerRoutine(n int, ch chan int) {
        var seq int
        for seq := range ch {
            // do work:
            fmt.Println(n, seq)
        }
    }
    

    This introduces a single point of failure: if the counter goroutine dies, everything is lost. This may not be a problem if all goroutine are executed on one computer, but may become a problem if they are scattered over the network. To make the counter immune to failures of single nodes in the cluster, special algorithms have to be used.

提交回复
热议问题