Search code examples
gogoroutineticker

Golang ticker concurrency issue


I have a function written in Golang as follows

func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {
//Gateway
logging.InfoLogger.Printf("StartTransactionsGatewayTicker:%v", participant.Participant)
ticker := time.NewTicker(1 * time.Second)
participant.TransactionGatewayTicker = ticker
go func() {
    for {
        select {
        case <-ticker.C:
            logging.InfoLogger.Printf("Tick at: %v", participant.Participant)
            participant.GetTransactions()
        }
    }
}()
}

I am calling the function in a loop as with 2 SimulationParticipant in the array. surprisingly the first participant is replaced by second participant and the GetTransactions always getting executed to the last item in the loop? How can I overcome this


Solution

  • Works for me (I am posting this code without seeing how you are calling StartTransactionsGatewayTicker, Donot downvote if not applicable :P ) :

    // [Timers](timers) are for when you want to do
    // something once in the future - _tickers_ are for when
    // you want to do something repeatedly at regular
    // intervals. Here's an example of a ticker that ticks
    // periodically until we stop it.
    
    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
    
            part1 := SimulationParticipant{}
        part1.id = "part1"
            part2 := SimulationParticipant{}
            part2.id = "part2"
            partSlice := make([]*SimulationParticipant,0)
            partSlice = append(partSlice, &part1, &part2)
    
            for _ , p := range partSlice {
                 p.StartTransactionsGatewayTicker()
            }
    
        // Tickers can be stopped like timers. Once a ticker
        // is stopped it won't receive any more values on its
        // channel. We'll stop ours after 16000ms.
        time.Sleep(16000 * time.Millisecond)
        part1.ticker.Stop()
        part2.ticker.Stop()
        fmt.Println("Ticker stopped")
    }
    
    type SimulationParticipant struct {
         id string
         ticker *time.Ticker
    }
    
    func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {
    
    ticker := time.NewTicker(1 * time.Second)
    participant.ticker = ticker
    go func() {
        for {
            select {
            case t := <-ticker.C:
                fmt.Println("Tick at", t,participant.id)
            }
        }
    }()
    }
    
    

    Output :

    Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part2
    Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part1
    Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part1
    Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part2
    Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part2
    Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part1
    Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part1
    Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part2
    Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part2
    Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part1
    Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part1
    Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part2
    Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part2
    Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part1
    Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part1
    Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part2
    Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part2
    Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part1
    Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part1
    Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part2
    Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part2
    Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part1
    Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part1
    Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part2
    Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part2
    Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part1
    Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part1
    Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part2
    Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part2
    Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part1
    Ticker stopped
    

    Playground : https://play.golang.org/p/yfHnrRK1iG8