40
41
// NewIDMutex create a new IDLock
43
>
impl := &idMutexImpl{
44
>
numShard: numShard,
45
>
hashFn: hashFn,
46
>
shards: make(map[uint32]*idMutexShardImpl),
47
>
}
48
>
for i := range numShard {
49
>
impl.shards[i] = &idMutexShardImpl{
50
>
mutexInfos: make(map[any]*mutexInfo),
51
>
}
52
>
}
53
55
}
56
58
>
return &mutexInfo{
59
>
waitCount: 1,
60
>
}
61
>
}
62
63
// LockID lock by specific identifier
65
>
shard := idMutex.shards[idMutex.getShardIndex(identifier)]
66
>
67
>
shard.Lock()
68
>
mutexInfo, ok := shard.mutexInfos[identifier]
69
>
if !ok {
70
>
mutexInfo := newMutexInfo()
71
>
shard.mutexInfos[identifier] = mutexInfo
72
>
shard.Unlock()
73
>
mutexInfo.Lock()
74
>
return
75
>
}
76
78
>
shard.Unlock()
79
>
mutexInfo.Lock()
80
}
81
82
// UnlockID unlock by specific identifier
84
>
shard := idMutex.shards[idMutex.getShardIndex(identifier)]
85
>
86
>
shard.Lock()
87
>
defer shard.Unlock()
88
>
mutexInfo, ok := shard.mutexInfos[identifier]
89
>
if !ok {
90
panic("cannot find workflow lock")
91
}
93
>
if mutexInfo.waitCount == 1 {
94
>
delete(shard.mutexInfos, identifier)
95
>
} else {
97
>
}
98
}
99
101
>
return idMutex.hashFn(key) % idMutex.numShard
102
>
}