FAQ
Trying to figure out the idiomatic way of coordinating state management
among interacting goroutines using sync.Cond.

Scenario: a process goroutine makes a 'service' call that may be time
intensive and/or start another goroutine to select/wait on an external
event. Whether the process will wait on the event occurrence is entirely
conditional on whatever the service implementation is. A background timer
provides protection against the service call taking too long and/or too
long a delay in the event occurrence -- should run a goroutine to adjust
the process status to 'fail' and ensure that the process then runs
accordingly.

Given the variability of the service implementations, adding a complex of
notification channels seems somehow inappropriate. Thought using sync.Cond
might be the solution, but am not seeing how to manage the locking
conflicts on p. Specifically, in the Wait 'case', locking p appears to
conflict with the ability to Wait on the readyLock -- there can be many
different instances of p running, so each needs its own instance of the
readyLock. This of course presumes that locking within the Wait case is
required -- is even this presumption correct?

Obviously, I am missing something, or is sync.Cond just not the right way.
Back to notification channels or is there some better design?

Thanks,
Gerald


type Process struct {
     sync.Mutex
     // ...
     level cond.Level // leveled run condition

     readyMutex sync.Mutex // wait point locker
     readyLock *sync.Cond // wait point
}

     // ....
     p.readyLock = sync.NewCond(&p.readyMutex)
     // ....


func (p *Process) start() {
     p.level = cond.Running // enum: Running, Waiting, Failed
     p.startTimeoutTimer() // failsafe -> p.level = Failed, call
p.readyLock.Signal()
     ret := p.service() // may start goroutine that may call
p.readyLock.Signal()
     action := p.next(ret) // enum: Run, Wait, Die
     switch action {

     case Wait:
         p.level = cond.Waiting
         p.readyLock.Wait()
         if p.level == cond.Waiting {
             p.level = cond.Running
             p.continue()
         } else {
             // do nothing: something else happened
         }

     case Run:
         // ....
}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 17, '15 at 8:43p
activeNov 18, '15 at 8:00p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase