pgraph: semaphore: Refactor semaphore size function and test
This commit is contained in:
@@ -36,19 +36,10 @@ func (g *Graph) SemaLock(semas []string) error {
|
|||||||
var reterr error
|
var reterr error
|
||||||
sort.Strings(semas) // very important to avoid deadlock in the dag!
|
sort.Strings(semas) // very important to avoid deadlock in the dag!
|
||||||
for _, id := range semas {
|
for _, id := range semas {
|
||||||
|
|
||||||
size := 1 // default semaphore size
|
|
||||||
// valid id's include "some_id", "hello:42" and ":13"
|
|
||||||
if index := strings.LastIndex(id, SemaSep); index > -1 && (len(id)-index+len(SemaSep)) >= 1 {
|
|
||||||
// NOTE: we only allow size > 0 here!
|
|
||||||
if i, err := strconv.Atoi(id[index+len(SemaSep):]); err == nil && i > 0 {
|
|
||||||
size = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g.slock.Lock() // semaphore creation lock
|
g.slock.Lock() // semaphore creation lock
|
||||||
sema, ok := g.semas[id] // lookup
|
sema, ok := g.semas[id] // lookup
|
||||||
if !ok {
|
if !ok {
|
||||||
|
size := SemaSize(id) // defaults to 1
|
||||||
g.semas[id] = semaphore.NewSemaphore(size)
|
g.semas[id] = semaphore.NewSemaphore(size)
|
||||||
sema = g.semas[id]
|
sema = g.semas[id]
|
||||||
}
|
}
|
||||||
@@ -78,3 +69,17 @@ func (g *Graph) SemaUnlock(semas []string) error {
|
|||||||
}
|
}
|
||||||
return reterr
|
return reterr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SemaSize returns the size integer associated with the semaphore id. It
|
||||||
|
// defaults to 1 if not found.
|
||||||
|
func SemaSize(id string) int {
|
||||||
|
size := 1 // default semaphore size
|
||||||
|
// valid id's include "some_id", "hello:42" and ":13"
|
||||||
|
if index := strings.LastIndex(id, SemaSep); index > -1 && (len(id)-index+len(SemaSep)) >= 1 {
|
||||||
|
// NOTE: we only allow size > 0 here!
|
||||||
|
if i, err := strconv.Atoi(id[index+len(SemaSep):]); err == nil && i > 0 {
|
||||||
|
size = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,6 +23,19 @@ import (
|
|||||||
"github.com/purpleidea/mgmt/resources"
|
"github.com/purpleidea/mgmt/resources"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestSemaSize(t *testing.T) {
|
||||||
|
pairs := map[string]int{
|
||||||
|
"id:42": 42,
|
||||||
|
":13": 13,
|
||||||
|
"some_id": 1,
|
||||||
|
}
|
||||||
|
for id, size := range pairs {
|
||||||
|
if i := SemaSize(id); i != size {
|
||||||
|
t.Errorf("sema id `%s`, expected: `%d`, got: `%d`", id, size, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewNoopResTestSema(name string, semas []string) *NoopResTest {
|
func NewNoopResTestSema(name string, semas []string) *NoopResTest {
|
||||||
obj := &NoopResTest{
|
obj := &NoopResTest{
|
||||||
NoopRes: resources.NoopRes{
|
NoopRes: resources.NoopRes{
|
||||||
|
|||||||
Reference in New Issue
Block a user