2016-07-20 01:37:28 +00:00
|
|
|
package locksutil
|
|
|
|
|
|
|
|
import (
|
2017-03-07 16:21:32 +00:00
|
|
|
"crypto/md5"
|
2016-07-20 01:37:28 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
const (
|
|
|
|
LockCount = 256
|
|
|
|
)
|
2016-07-20 08:48:35 +00:00
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
type LockEntry struct {
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
2016-08-30 20:36:58 +00:00
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
func CreateLocks() []*LockEntry {
|
|
|
|
ret := make([]*LockEntry, LockCount)
|
|
|
|
for i := range ret {
|
|
|
|
ret[i] = new(LockEntry)
|
2016-07-20 01:37:28 +00:00
|
|
|
}
|
2017-03-07 16:21:32 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func LockIndexForKey(key string) uint8 {
|
|
|
|
hf := md5.New()
|
|
|
|
hf.Write([]byte(key))
|
|
|
|
return uint8(hf.Sum(nil)[0])
|
|
|
|
}
|
2016-07-20 08:48:35 +00:00
|
|
|
|
2017-03-07 16:21:32 +00:00
|
|
|
func LockForKey(locks []*LockEntry, key string) *LockEntry {
|
|
|
|
return locks[LockIndexForKey(key)]
|
2016-07-20 01:37:28 +00:00
|
|
|
}
|