Hi all,

while refactoring an old application I found this code:

type concurrentChecks struct {
mutex        sync.Mutex
activeChecks map[string]int
}

// addCheck waits until the processing for an item with the same token is 
finished
func (c *concurrentChecks) addCheck(token string) {
for {
c.mutex.Lock()
if _, ok := c.activeChecks[token]; !ok {
c.activeChecks[token] = 1
c.mutex.Unlock()
return
}
c.mutex.Unlock()
time.Sleep(20 * time.Millisecond)
}
}

func (c *concurrentChecks) removeCheck(token string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.activeChecks, token)
}

In this application there are different goroutines, each goroutine process 
some input data and extract a string token.  Different tokens must be 
processed in parallel, but if 2 or more goroutines get the same token they 
must be serialized. The above code is ugly but it works, replacing it using 
channels or a sync.Map seems not so easy. I would like to avoid something 
like this https://github.com/atedja/go-multilock/blob/master/multilock.go

Do you have suggestions to achieve the same result in a more idiomatic way?

thanks 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d78c5deb-8100-4d37-82a9-10f168e81ef7%40googlegroups.com.

Reply via email to