-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker.go
927 lines (786 loc) · 22.8 KB
/
locker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
package lock
import (
"context"
"sync"
"sync/atomic"
"time"
"go.uber.org/zap"
)
// callback function is a function that should be executed right after it inserted into hashmap
// generally callback is responsible for the removing itself from the hashmap
// id - id of the lock
// notifyCh - channel to notify that all locks were removed
// stopCh - broadcast channel to stop all the callbacks associated with the resource
type callback func(id string, notifyCh chan<- struct{}, stopCh <-chan struct{})
// item represents callback element
type item struct {
// callback to remove the item
callback callback
// item's stop channel
stopCh chan struct{}
// item's update TTL channel
updateTTLCh chan int
}
// resource
type resource struct {
ownerID atomic.Pointer[string]
// writerCount is the exclusive lock counter (should be 1 or 0)
writerCount atomic.Uint64
// readerCount is the number of readers (writers must be 0)
readerCount atomic.Uint64
// lock with timeout based on a channel
resourceMu *reslock
// map with the actual locks by ID
locks sync.Map // map[string]*item
// notificationCh used to notify that all locks are expired and the user is free to get a new one
// this channel receives an event only if there are no locks (write/read)
// resource-based
notificationCh chan struct{}
// stopCh should not receive any events. It's used as a broadcast-on-close event to notify all existing locks to expire
stopCh chan struct{}
}
type locker struct {
// global mutex for the resources, this is a short-lived lock to create or get resource
globalMu *reslock
// logger
log *zap.Logger
// all resources stored here
resources map[string]*resource
}
func newLocker(log *zap.Logger) *locker {
return &locker{
log: log,
globalMu: newResLock(log),
resources: make(map[string]*resource, 5),
}
}
// lock used to acquire exclusive lock for the resource or promote read-lock to lock with the same ID
/*
Here may have the following scenarios:
- No resource associated with the resource ID, create resource, add callback if we have TTL, increase writers to 1.
- Resource exists, no locks associated with it -> add callback if we have TTL, increase writers to 1
- Resource exists, write lock already acquired.
Wait on a context and notification channel.
Notification will be sent when the last lock is released.
*/
func (l *locker) lock(ctx context.Context, res, id string, ttl int) bool {
// first - check if the resource exists,
// check if we have the resource
obt := l.globalMu.lock(ctx)
if !obt {
l.log.Debug("failed to acquire a global lock",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
r, ok := l.resources[res]
if !ok {
// write operation
l.log.Debug("no such lock resource, creating new",
zap.String("resource", res),
zap.String("id", id),
zap.Int("ttl microseconds", ttl),
)
rr := &resource{
resourceMu: newResLock(l.log),
notificationCh: make(chan struct{}, 1),
stopCh: make(chan struct{}, 1),
}
rr.ownerID.Store(ptrTo(id))
rr.writerCount.Store(1)
rr.readerCount.Store(0)
l.resources[res] = rr
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
rr.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, rr.notificationCh, rr.stopCh)
}()
l.globalMu.unlock()
return true
}
// after getting lock -> unlock the resource
l.globalMu.unlock()
// lock the resource
if !r.resourceMu.lock(ctx) {
l.log.Warn("can't acquire a lock for the resource, timeout exceeded",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
switch {
// we have a writer
case r.readerCount.Load() == 0 && r.writerCount.Load() == 1:
l.log.Debug("waiting to hold the mutex", zap.String("id", id))
// unlock the release mutex
r.resourceMu.unlockRelease()
l.log.Debug("release mutex unlocked",
zap.String("resource", res),
zap.String("id", id),
)
select {
// wait for the notification
case <-r.notificationCh:
l.log.Debug("previous lock was removed, received notification", zap.String("id", id))
// get release mutex back
if !r.resourceMu.lockRelease(ctx) {
l.log.Debug("can't acquire release mutex back, timeout exceeded",
zap.String("resource", res),
zap.String("id", id),
)
// unlock only the operation lock, since someone locked the release mutex
r.resourceMu.unlockOperation()
return false
}
l.log.Debug("got release mutex back", zap.String("id", id))
// inconsistent, still have readers/writers after notification
if r.writerCount.Load() != 0 && r.readerCount.Load() != 0 {
l.log.Error("inconsistent state, should be zero writers and zero readers",
zap.String("resource", res),
zap.String("id", id),
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()))
return false
}
r.ownerID.Store(ptrTo(id))
r.writerCount.Store(1)
r.readerCount.Store(0)
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
l.log.Debug("lock successfully acquired",
zap.String("resource", res),
zap.String("id", id),
)
return true
case <-ctx.Done():
l.log.Warn("lock notification wait timeout expired",
zap.String("resource", res),
zap.String("id", id),
)
// at this moment we're holding only the resource lock
r.resourceMu.unlockOperation()
return false
}
// we have readers
case r.writerCount.Load() == 0 && r.readerCount.Load() > 0:
// check if that's possible to elevate read lock permission to write
if r.readerCount.Load() == 1 {
l.log.Debug("checking readers to elevate rlock permissions, w==0, r>0",
zap.String("resource", res),
zap.String("id", id),
)
rr, okk := r.locks.Load(id)
if okk {
// promote lock from read to write
l.log.Debug("found read lock which can be promoted to write lock",
zap.String("resource", res),
zap.String("id", id),
)
// send stop signal to the particular lock, here we have only 1 lock, we can send stop signal
// instead of closing the channel
select {
case rr.(*item).stopCh <- struct{}{}:
default:
l.log.Debug("failed to send stop signal to the lock id, channel is full",
zap.String("resource", res),
zap.String("id", id),
)
}
}
r.resourceMu.unlockRelease()
// wait for the notification
l.log.Debug("waiting for the notification: r>0, w==0: r==0, w==0",
zap.String("resource", res),
zap.String("id", id),
)
select {
case <-r.notificationCh:
l.log.Debug("r==0 notification received",
zap.String("resource", res),
zap.String("id", id),
)
// get release mutex back
if !r.resourceMu.lockRelease(ctx) {
l.log.Debug("can't acquire release mutex back, timeout exceeded",
zap.String("resource", res),
zap.String("id", id),
)
// unlock only the operation lock, since someone locked the release mutex
r.resourceMu.unlockOperation()
return false
}
// inconsistent, still have readers/writers after notification
if r.writerCount.Load() != 0 && r.readerCount.Load() != 0 {
l.log.Error("inconsistent state, should be zero writers and zero readers",
zap.String("resource", res),
zap.String("id", id),
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()))
r.resourceMu.unlock()
return false
}
// store writer and remove reader
r.ownerID.Store(ptrTo(id))
r.writerCount.Store(1)
r.readerCount.Store(0)
// callback
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
return true
case <-ctx.Done():
l.log.Warn("lock notification wait timeout expired", zap.String("id", id))
// at this moment we're holding only operation lock, release lock was sent back
r.resourceMu.unlockOperation()
return false
}
}
// at this point we know that we have more than 1 read lock, so we can't promote them to lock
l.log.Debug("waiting for the readlocks to expire/release, w==0, r>0",
zap.String("resource", res),
zap.String("id", id))
select {
// we've got notification that no one holding this mutex anymore
case <-r.notificationCh:
l.log.Debug("no readers holding mutex anymore, proceeding with acquiring write lock", zap.String("id", id))
// store writer and remove reader
r.ownerID.Store(ptrTo(id))
r.writerCount.Store(1)
r.readerCount.Store(0)
// callback
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
return true
// timeout exceeded
case <-ctx.Done():
l.log.Warn("timeout expired, no lock acquired",
zap.String("resource", res),
zap.String("id", id),
)
r.resourceMu.unlockOperation()
return false
}
// noone holding any type of lock
case r.readerCount.Load() == 0 && r.writerCount.Load() == 0:
l.log.Debug("acquiring lock, w==0, r==0",
zap.String("resource", res),
zap.String("id", id))
// drain notifications channel, just to be sure
select {
case <-r.notificationCh:
default:
}
// store the writer
r.ownerID.Store(ptrTo(id))
r.writerCount.Store(1)
r.readerCount.Store(0)
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
return true
default:
l.log.Error("unknown readlock state",
zap.String("resource", res),
zap.String("id", id),
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()),
)
r.resourceMu.unlock()
return false
}
}
func (l *locker) lockRead(ctx context.Context, res, id string, ttl int) bool {
// first - check if the resource exists,
// check if we have the resource
obt := l.globalMu.lock(ctx)
if !obt {
l.log.Debug("failed to acquire a global lock",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
r, ok := l.resources[res]
if !ok {
// write operation
l.log.Debug("no such lock resource, creating new",
zap.String("resource", res),
zap.String("id", id),
zap.Int("ttl microseconds", ttl),
)
rr := &resource{
resourceMu: newResLock(l.log),
notificationCh: make(chan struct{}, 1),
stopCh: make(chan struct{}, 1),
}
rr.ownerID.Store(ptrTo(""))
rr.writerCount.Store(0)
rr.readerCount.Store(1)
l.resources[res] = rr
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
rr.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, rr.notificationCh, rr.stopCh)
}()
l.globalMu.unlock()
return true
}
// after getting lock -> unlock the resource
l.globalMu.unlock()
// lock the resource
if !r.resourceMu.lock(ctx) {
l.log.Warn("can't acquire a lock for the resource, timeout exceeded",
zap.String("res", res),
zap.String("id", id),
)
return false
}
// check tricky cases
switch {
// case when we have written lock
case r.writerCount.Load() == 1:
if r.readerCount.Load() > 0 {
l.log.Error("write<->read lock incosistend state, w==1, r>0",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlock()
return false
}
// we have to wait here
l.log.Debug("waiting to acquire a lock, w==1, r==0",
zap.String("resource", res),
zap.String("id", id))
// allow releasing mutexes
l.log.Debug("returning releaseMuCh mutex to temporarily allow releasing locks",
zap.String("resource", res),
zap.String("id", id))
// unlock release mutex
r.resourceMu.unlockRelease()
select {
case <-r.notificationCh:
// get release mutex back
if !r.resourceMu.lockRelease(ctx) {
l.log.Debug("can't acquire release mutex back, timeout exceeded",
zap.String("resource", res),
zap.String("id", id),
)
// unlock only the operation lock, since someone locked the release mutex
r.resourceMu.unlockOperation()
return false
}
// inconsistent, still have readers/writers after notification
if r.writerCount.Load() != 0 && r.readerCount.Load() != 0 {
l.log.Error("inconsistent state, should be zero writers and zero readers",
zap.String("resource", res),
zap.String("id", id),
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()))
r.resourceMu.unlock()
return false
}
r.writerCount.Store(0)
r.readerCount.Add(1)
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
return true
case <-ctx.Done():
l.log.Warn("failed to acquire a readlock, timeout exceeded, w==1, r==0",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlockOperation()
return false
}
// case when we don't have a writer and have 0 or more readers
case r.writerCount.Load() == 0:
l.log.Debug("adding read lock, w==0, r>=0",
zap.String("resource", res),
zap.String("id", id))
// increase readers
r.writerCount.Store(0)
r.readerCount.Add(1)
// we have TTL, create callback
callb, stopCbCh, updateTTLCh := l.makeLockCallback(res, id, ttl)
r.locks.Store(id, &item{
callback: callb,
stopCh: stopCbCh,
updateTTLCh: updateTTLCh,
})
// run the callback
go func() {
callb(id, r.notificationCh, r.stopCh)
}()
r.resourceMu.unlock()
return true
default:
l.log.Error("unknown readlock state",
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()),
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlock()
return false
}
}
func (l *locker) release(ctx context.Context, res, id string) bool {
// write operation
if !l.globalMu.lock(ctx) {
l.log.Debug("failed to acquire a global lock for release",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
// check if we have the resource
r, ok := l.resources[res]
if !ok {
l.log.Debug("no such resource",
zap.String("resource", res),
zap.String("id", id),
)
l.globalMu.unlock()
return false
}
l.globalMu.unlock()
if !r.resourceMu.lockRelease(ctx) {
l.log.Warn("failed to acquire a resource lock",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
if r.ownerID.Load() != nil && *r.ownerID.Load() != "" && *r.ownerID.Load() != id {
l.log.Debug("release called for the resource which is not owned by the caller",
zap.String("resource", res),
zap.String("id", id),
)
r.resourceMu.unlockRelease()
return false
}
rl, ok := r.locks.Load(id)
if !ok {
l.log.Warn("no such lock ID",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlockRelease()
return false
}
// notify close by closing stopCh for the particular ID
select {
case rl.(*item).stopCh <- struct{}{}:
l.log.Debug("force release notification sent",
zap.String("resource", res),
zap.String("id", id),
)
default:
}
l.log.Debug("lock successfully released",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlockRelease()
return true
}
func (l *locker) forceRelease(ctx context.Context, res string) bool {
// write operation
if !l.globalMu.lock(ctx) {
l.log.Debug("failed to acquire a global lock for release",
zap.String("resource", res),
)
return false
}
// check if we have the resource
r, ok := l.resources[res]
if !ok {
l.log.Debug("no such resource", zap.String("resource", res))
l.globalMu.unlock()
return false
}
l.globalMu.unlock()
if !r.resourceMu.lockRelease(ctx) {
l.log.Warn("failed to acquire a resource lock",
zap.String("resource", res),
)
return false
}
// broadcast release signal
r.locks.Range(func(key, value any) bool {
k := key.(string)
v := value.(*item)
select {
case v.stopCh <- struct{}{}:
l.log.Debug("force release notification sent", zap.String("id", k))
default:
}
return true
})
r.resourceMu.unlockRelease()
l.log.Debug("all force-release messages were sent", zap.String("resource", res))
return true
}
func (l *locker) exists(ctx context.Context, res, id string) bool {
// write operation
if !l.globalMu.lock(ctx) {
l.log.Debug("failed to acquire a global lock for release",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
defer l.globalMu.unlock()
r, ok := l.resources[res]
if !ok {
l.log.Warn("no such resource",
zap.String("resource", res),
zap.String("id", id))
return false
}
// Special case, check if we have any locks
if id == "*" {
if r.writerCount.Load() > 0 || r.readerCount.Load() > 0 {
return true
}
return false
}
if _, existsID := r.locks.Load(id); !existsID {
l.log.Warn("no such lock ID",
zap.String("resource", res),
zap.String("id", id))
return false
}
return true
}
func (l *locker) updateTTL(ctx context.Context, res, id string, ttl int) bool {
// write operation
if !l.globalMu.lock(ctx) {
l.log.Debug("failed to acquire a global lock for release",
zap.String("resource", res),
zap.String("id", id),
)
return false
}
r, ok := l.resources[res]
if !ok {
l.log.Warn("no such resource",
zap.String("resource", res),
zap.String("id", id))
l.globalMu.unlock()
return false
}
l.globalMu.unlock()
// lock the resource
if !r.resourceMu.lockRelease(ctx) {
l.log.Warn("can't acquire a lock for the resource, timeout exceeded",
zap.String("res", res),
zap.String("id", id),
)
return false
}
l.log.Debug("updateTTL started",
zap.String("resource", res),
zap.String("id", id))
if !ok {
l.log.Warn("no such resource",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlockRelease()
return false
}
rl, ok := r.locks.Load(id)
if !ok {
l.log.Warn("no such resource ID",
zap.String("resource", res),
zap.String("id", id))
r.resourceMu.unlockRelease()
return false
}
select {
case rl.(*item).updateTTLCh <- ttl:
l.log.Debug("lock/rlocl TTL was successfully updated",
zap.String("resource", res),
zap.String("id", id))
default:
l.log.Error("failed to send notification about TTL update",
zap.String("resource", res),
zap.String("id", id))
}
r.resourceMu.unlockRelease()
return true
}
func (l *locker) stop(ctx context.Context) {
l.log.Debug("received stop signal, acquiring lock/release mutexes")
// write operation
if !l.globalMu.lock(ctx) {
l.log.Debug("failed to acquire a global lock for release")
return
}
defer l.globalMu.unlock()
l.log.Debug("acquired stop mutex")
for k, v := range l.resources {
close(v.stopCh)
l.log.Debug("closed broadcast channed",
zap.String("resource", ""),
zap.String("id", k))
}
l.log.Debug("signal sent to all resources")
}
func (l *locker) makeLockCallback(res, id string, ttl int) (callback, chan struct{}, chan int) {
stopCbCh := make(chan struct{}, 1)
updateTTLCh := make(chan int, 1)
// at this point, when adding lock, we should not have the callback
return func(lockID string, notifCh chan<- struct{}, sCh <-chan struct{}) {
// case for the items without TTL. We should add such items to control their flow
cbttl := ttl
if cbttl == 0 {
cbttl = 31555952000000 // year
}
// TTL channel
ta := time.NewTicker(time.Microsecond * time.Duration(cbttl))
loop:
select {
case <-ta.C:
l.log.Debug("r/lock: ttl expired",
zap.String("resource", res),
zap.String("id", lockID),
zap.Int("ttl microseconds", cbttl),
)
ta.Stop()
// broadcast stop channel
case <-sCh:
l.log.Debug("r/lock: ttl removed, stop broadcast call",
zap.String("resource", res),
zap.String("id", lockID),
zap.Int("ttl microseconds", cbttl),
)
ta.Stop()
// item stop channel
case <-stopCbCh:
l.log.Debug("r/lock: ttl removed, stop callback call",
zap.String("resource", res),
zap.String("id", lockID),
zap.Int("ttl microseconds", cbttl),
)
ta.Stop()
case newTTL := <-updateTTLCh:
// if the new TTL is 0, we should treat it as unlimited
if newTTL == 0 {
newTTL = 31555952000000 // year
}
l.log.Debug("r/lock: ttl was updated",
zap.String("resource", res),
zap.String("id", id),
zap.Int("new ttl microseconds", newTTL))
// update the initial ttl
cbttl = newTTL
ta.Reset(time.Microsecond * time.Duration(cbttl))
// in case of TTL, we don't need to remove the item, only update TTL
goto loop
}
// unlimited but should not be long
if !l.globalMu.lock(context.Background()) {
l.log.Debug("failed to acquire a global lock for release",
zap.String("resource", res),
zap.String("id", id),
)
return
}
defer l.globalMu.unlock()
// remove the item
// we need to protect a bunch of the atomic operations here per-resource
r, ok := l.resources[res]
if !ok {
l.log.Warn("no such resource, TTL expired",
zap.String("resource", res),
zap.String("id", id))
return
}
r.locks.Delete(id)
if r.writerCount.Load() == 1 {
// clear owner, only a writer might be an owner
r.ownerID.Store(ptrTo(""))
r.writerCount.Store(0)
r.readerCount.Store(0)
}
if r.readerCount.Load() > 0 {
// reduce the number of readers
r.readerCount.Add(^uint64(0))
}
l.log.Debug("current writers and readers count",
zap.String("resource", res),
zap.String("deleted id", id),
zap.Uint64("writers", r.writerCount.Load()),
zap.Uint64("readers", r.readerCount.Load()),
)
// we also have to check readers and writers to send notification
if r.writerCount.Load() == 0 && r.readerCount.Load() == 0 {
// only one resource, remove it
// and send a notification to the notification channel
select {
case notifCh <- struct{}{}:
l.log.Debug("deleting the last lock for the resource, sending notification",
zap.String("resource", res),
zap.String("id", id))
default:
l.log.Debug("deleting the last lock for the resource, failed to send notification",
zap.String("resource", res),
zap.String("id", id))
break
}
}
}, stopCbCh, updateTTLCh
}
func ptrTo[T any](val T) *T {
return &val
}