tarndt / sema Goto Github PK
View Code? Open in Web Editor NEWMultiple Binary/Counting/Count+Timeout semaphore implementations in Go.
License: Mozilla Public License 2.0
Multiple Binary/Counting/Count+Timeout semaphore implementations in Go.
License: Mozilla Public License 2.0
The below code only appears to work if I use the channel implementation of TimeoutCountingSema
. Basically the code keeps a map of TimeoutCountingSema
objects that are locked with a timeout and then unlocked.
After a minimal amount of investigation, it seems like the first lock (for a given ID) is acquired and the protected function (fnc
) runs, and then the code to unlock is called and Do
returns, but then subsequent attempts to acquire the lock for the same ID fail and the code hangs indefinitely. Just calling UseChannelBasedImpl()
causes the code to run as expected.
type redirectJobSynchronizer struct {
mapLock sync.RWMutex
// Map of per-originID locks
idToLock map[int64]sema.TimeoutCountingSema
}
func (this *redirectJobSynchronizer) Do(id int64, fnc func() error) error {
this.mapLock.RLock()
idSema, ok := this.idToLock[id]
this.mapLock.RUnlock()
if !ok {
this.mapLock.Lock()
if idSema, ok = this.idToLock[id]; !ok {
idSema = sema.NewTimeoutSema(1, 10*time.Second)
this.idTpLock[id] = idSema
}
this.mapLock.Unlock()
}
//Aquire lock
if idSema.P() {
defer idSema.V()
return fnc()
}
return errors.New("timeout")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.