Saturday, May 4, 2024
HomeGolangTicker will it create new or anticipate the present occasion to complete?...

Ticker will it create new or anticipate the present occasion to complete? – Code Overview


Hello,
I’ve my following codes right here Go Playground – The Go Programming Language. What it suppose to do is each 10 seconds interval it would learn from a staging desk of mysql. If there’s a information then I acquired run few logics which includes placing all right into a transaction. My confusion if say one occasion is already operating and it took over 10 seconds and its nonetheless haven’t accomplished will the subsequent occasion begin to run or it would cease until the earlier one is accomplished?

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

Hello Jeff,
So since I would like it to be sync not async. Ought to I not put the phrase go after I name my operate ?

Sure.
If the sync course of takes greater than 10 seconds you’ll miss a tick.

Hello Ermanimer,
Sure thats how I’d need it to be trigger if begin one other thread then it’d overlap the database updates. So now I’m confuse in golang which is the fitting approach to go that it’ll miss if its taking greater than 10 seconds.

Hey,

It’s best to determine if this can be a sync or an async course of since I don’t precisely know what you are attempting to do :slightly_smiling_face:

Hello, Ermanimer,
It ought to be sync why trigger I dont need any new occasion to begin earlier than the earlier one is accomplished. So what’s your suggestion to not use the phrase go when calling it proper ?

You in all probability need a goroutine doing all your processing that receives work requests from a channel and notifies completion on one other channel. You may have a loop with choose between a timer and the notification channel that will ship extra work as soon as the timer has ticked and it will get notification of the earlier work has accomplished. That can assure that just one work request is being processed at a time and that it doesn’t run extra regularly than the timeout.

Hello Mje,
I’ve made some modifications to maintain the execution state like your talked about. Right here is my modified model of the codes Go Playground – The Go Programming Language. Is that this what you meant. So if its nonetheless operating and state is true then it simply return ?

No, you need a processing goroutine that reads work requests from a channel. With a single goroutine that does all of the processing, you gained’t get requests being processed concurrently.

Hello Jeff,
I’m a bit misplaced based mostly on my newest codes are you able to give me some hints what to alter or edit as per your suggestion.

Untested:

  requestChan := make(chan struct{}{}, somebuffersize)
  completedChan := make(chan struct{}{})
  go processor(requestChan, completedChan)
  
  ...
  ticked := false
  accomplished := false
  for {
    choose {
      case <-tick.ticker.C:
        ticked = true
      case <- completeChan:
        accomplished = true
    }
    if ticked && accomplished {
      // The time interval has elapsed and the processor is prepared for extra work.
      // This ought to be simplified to omit the completedChan 
      // by way of deciding on on an unbuffered requestCh.  I am going to depart that as a extra
      // superior train.
      ticked=false
      accomplished=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”.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments