Ticker will it create new or wait for the existing instance to finish?

Hi,
I have my following codes here Go Playground - The Go Programming Language. What it suppose to do is every 10 seconds interval it will read from a staging table of mysql. If there is a data then I got run few logics which involves putting all into a transaction. My confusion if say one instance is already running and it took over 10 seconds and its still have not completed will the next instance start to run or it will stop till the previous one is completed?

goroutines are asynchronous
https://go.dev/tour/concurrency/1

Hi Jeff,
So since I need it to be sync not async. Should I not put the word go when I call my function ?

Yes.
If the sync process takes more than 10 seconds you will miss a tick.

Hi Ermanimer,
Yes thats how I would want it to be cause if start another thread then it might overlap the database updates. So now I am confuse in golang which is the right way to go that it will miss if its taking more than 10 seconds.

Hello,

You should decide if this is a sync or an async process since I don’t exactly know what you are trying to do :slightly_smiling_face:

Hi, Ermanimer,
It should be sync why cause I dont want any new instance to start before the previous one is completed. So what is your suggestion not to use the word go when calling it right ?

You probably want a goroutine doing your processing that receives work requests from a channel and notifies completion on another channel. You could have a loop with select between a timer and the notification channel that would send more work once the timer has ticked and it gets notification of the previous work has completed. That will guarantee that only one work request is being processed at a time and that it doesn’t run more frequently than the timeout.

Hi Mje,
I have made some changes to keep the execution state like your mentioned. Here is my modified version of the codes Go Playground - The Go Programming Language. Is this what you meant. So if its still running and state is true then it just return ?

No, you want a processing goroutine that reads work requests from a channel. With a single goroutine that does all the processing, you won’t get requests being processed simultaneously.

Hi Jeff,
I am a bit lost based on my latest codes can you give me some hints what to change or edit as per your suggestion.

Untested:

  requestChan := make(chan struct{}{}, somebuffersize)
  completedChan := make(chan struct{}{})
  go processor(requestChan, completedChan)
  
  ...
  ticked := false
  completed := false
  for {
    select {
      case <-tick.ticker.C:
        ticked = true
      case <- completeChan:
        completed = true
    }
    if ticked && completed {
      // The time period has elapsed and the processor is ready for more work.
      // This should be simplified to omit the completedChan 
      // via selecting on an unbuffered requestCh.  I'll leave that as a more
      // advanced exercise.
      ticked=false
      completed=false
      requestChan <- struct{}{}
    }
  }
}
...

func processor(requestCh <-chan stuct[}{}, completeCh chan<- struct{}{}) {
   for _ := range requestCh {
      process()
      completCh <- struct{}{}
  }
}

Hi Jeff,
I have tried to worked out your codes its here Go Playground - The Go Programming Language. I am glad now I went on to learn about channel good concept but I am still new to it. Few things in the codes I fixed one is this requestChan := make(chan struct{}{}, somebuffersize) I made it to requestChan := make(chan struct{}). I had to remove the second curly brackets and also the somebuffersize. Now I am only stucked is here no new variables on left side of := this error refers to for _ := range requestCh {.

You probably want this to be a buffered channel so that the goroutine that sends requests won’t block. It probably won’t block the way that it is with the main waiting for a message from the completion chan before sending, but just in case.

Remove the :

Hi Jeff,
How to decide to put in buffer or not ? Secondly actually I dont quite this logic for _ = range requestCh. Because this requestCh is something empty right so how does it loop ? What is the start and end value to this ? Here is the latest code no errors Go Playground - The Go Programming Language but it says now “timeout running program”.

All your questions are answered here

https://go.dev/tour/concurrency/2
https://go.dev/tour/concurrency/3
https://go.dev/tour/concurrency/4

Change the initial value of completed to true and break out of the loop after some number of ticks so its not an infinite loop and it works fine.

I reduced the tick interval so it runs quicker too.

Hi Jeff,
Ok I want to learn further to understand it.

  1. A Tour of Go its say buffer to me it looks like you build an array kind of thing then assignment based on the index is it ?

  2. Based on this codes completeCh ← struct{}{} I wanted to understand further on this
    fmt.Println("struct{}{} ", struct{}{}) what I get is {} so does it mean false ?

  3. Why is it that certain places its just struct{} and at some places its struct{}{}. From my readings it says it empty structure ?