@@ -28,6 +28,7 @@ const (
2828 defaultLeaseDuration = 10 * time .Second
2929 loggingPrefix = "[k8s_locker] "
3030 defaultNamespace = "default"
31+ origKeyName = "original-key"
3132)
3233
3334func init () {
@@ -38,9 +39,6 @@ func init() {
3839 acquiredlocks : make (map [string ]* lock ),
3940 attemtinglocks : make (map [string ]* lock ),
4041 logger : log .New (io .Discard , loggingPrefix , utils .DefaultLoggingFlags ),
41- services : make (map [string ]context.CancelFunc ),
42- km : new (sync.RWMutex ),
43- keyMapping : make (map [string ]string ),
4442 }
4543 })
4644}
@@ -52,11 +50,8 @@ type k8sLocker struct {
5250 m * sync.RWMutex
5351 acquiredlocks map [string ]* lock
5452 attemtinglocks map [string ]* lock
55- services map [string ]context.CancelFunc
56- //
57- identity string // hostname
58- km * sync.RWMutex
59- keyMapping map [string ]string
53+
54+ identity string // hostname
6055}
6156
6257type config struct {
@@ -98,12 +93,12 @@ func (k *k8sLocker) Init(ctx context.Context, cfg map[string]interface{}, opts .
9893
9994func (k * k8sLocker ) Lock (ctx context.Context , key string , val []byte ) (bool , error ) {
10095 nkey := strings .ReplaceAll (key , "/" , "-" )
101- k .km .Lock ()
102- k .keyMapping [nkey ] = key
103- k .km .Unlock ()
10496 doneChan := make (chan struct {})
10597 l := & coordinationv1.Lease {
10698 ObjectMeta : metav1.ObjectMeta {
99+ Annotations : map [string ]string {
100+ origKeyName : key ,
101+ },
107102 Name : nkey ,
108103 Namespace : k .Cfg .Namespace ,
109104 Labels : map [string ]string {
@@ -203,12 +198,8 @@ func (k *k8sLocker) Lock(ctx context.Context, key string, val []byte) (bool, err
203198func (k * k8sLocker ) KeepLock (ctx context.Context , key string ) (chan struct {}, chan error ) {
204199 doneChan := make (chan struct {})
205200 errChan := make (chan error )
206- k .km .RLock ()
207- nkey , ok := k .keyMapping [key ]
208- k .km .RUnlock ()
209- if ! ok {
210- nkey = strings .ReplaceAll (key , "/" , "-" )
211- }
201+ nkey := strings .ReplaceAll (key , "/" , "-" )
202+
212203 go func () {
213204 defer close (doneChan )
214205 ticker := time .NewTicker (k .Cfg .RenewPeriod )
@@ -260,12 +251,7 @@ func (k *k8sLocker) KeepLock(ctx context.Context, key string) (chan struct{}, ch
260251}
261252
262253func (k * k8sLocker ) Unlock (ctx context.Context , key string ) error {
263- k .km .RLock ()
264- nkey , ok := k .keyMapping [key ]
265- k .km .RUnlock ()
266- if ! ok {
267- nkey = strings .ReplaceAll (key , "/" , "-" )
268- }
254+ nkey := strings .ReplaceAll (key , "/" , "-" )
269255 k .m .Lock ()
270256 defer k .m .Unlock ()
271257 k .unlock (ctx , nkey )
0 commit comments