@@ -417,7 +417,10 @@ func (t *trackingSlotSupplier) ReserveSlot(
417
417
return nil , fmt .Errorf ("slot supplier returned nil permit" )
418
418
}
419
419
t .issuedSlotsAtomic .Add (1 )
420
- t .publishMetrics (false )
420
+ t .slotsMutex .Lock ()
421
+ usedSlots := len (t .usedSlots )
422
+ t .slotsMutex .Unlock ()
423
+ t .publishMetrics (usedSlots )
421
424
return permit , nil
422
425
}
423
426
@@ -432,7 +435,10 @@ func (t *trackingSlotSupplier) TryReserveSlot(data *slotReservationData) *SlotPe
432
435
})
433
436
if permit != nil {
434
437
t .issuedSlotsAtomic .Add (1 )
435
- t .publishMetrics (false )
438
+ t .slotsMutex .Lock ()
439
+ usedSlots := len (t .usedSlots )
440
+ t .slotsMutex .Unlock ()
441
+ t .publishMetrics (usedSlots )
436
442
}
437
443
return permit
438
444
}
@@ -442,42 +448,39 @@ func (t *trackingSlotSupplier) MarkSlotUsed(permit *SlotPermit) {
442
448
panic ("Cannot mark nil permit as used" )
443
449
}
444
450
t .slotsMutex .Lock ()
445
- defer t .slotsMutex .Unlock ()
446
451
t .usedSlots [permit ] = struct {}{}
452
+ usedSlots := len (t .usedSlots )
453
+ t .slotsMutex .Unlock ()
447
454
t .inner .MarkSlotUsed (& slotMarkUsedContextImpl {
448
455
permit : permit ,
449
456
logger : t .logger ,
450
457
metrics : t .metrics ,
451
458
})
452
- t .publishMetrics (true )
459
+ t .publishMetrics (usedSlots )
453
460
}
454
461
455
462
func (t * trackingSlotSupplier ) ReleaseSlot (permit * SlotPermit , reason SlotReleaseReason ) {
456
463
if permit == nil {
457
464
panic ("Cannot release with nil permit" )
458
465
}
459
466
t .slotsMutex .Lock ()
460
- defer t .slotsMutex .Unlock ()
467
+ delete (t .usedSlots , permit )
468
+ usedSlots := len (t .usedSlots )
469
+ t .slotsMutex .Unlock ()
461
470
t .inner .ReleaseSlot (& slotReleaseContextImpl {
462
471
permit : permit ,
463
472
reason : reason ,
464
473
logger : t .logger ,
465
474
metrics : t .metrics ,
466
475
})
467
476
t .issuedSlotsAtomic .Add (- 1 )
468
- delete (t .usedSlots , permit )
469
477
if permit .extraReleaseCallback != nil {
470
478
permit .extraReleaseCallback ()
471
479
}
472
- t .publishMetrics (true )
480
+ t .publishMetrics (usedSlots )
473
481
}
474
482
475
- func (t * trackingSlotSupplier ) publishMetrics (lockAlreadyHeld bool ) {
476
- if ! lockAlreadyHeld {
477
- t .slotsMutex .Lock ()
478
- defer t .slotsMutex .Unlock ()
479
- }
480
- usedSlots := len (t .usedSlots )
483
+ func (t * trackingSlotSupplier ) publishMetrics (usedSlots int ) {
481
484
if t .inner .MaxSlots () != 0 {
482
485
t .taskSlotsAvailableGauge .Update (float64 (t .inner .MaxSlots () - usedSlots ))
483
486
}
0 commit comments