@@ -320,15 +320,15 @@ func (t *tokenCounter) TryDeduct(
320
320
expensiveLog = true
321
321
}
322
322
t .mu .Lock ()
323
- defer t .mu .Unlock ()
324
323
325
- tokensAvailable := t .tokensLocked (wc )
324
+ tokensAvailable := t .tokensLocked (wc ) // nolint:deferunlockcheck
326
325
if tokensAvailable <= 0 {
326
+ t .mu .Unlock () // nolint:deferunlockcheck
327
327
return 0
328
328
}
329
329
330
330
adjust := min (tokensAvailable , tokens )
331
- t .adjustLocked (ctx , wc , - adjust , now , flag , expensiveLog )
331
+ t .adjustLockedAndUnlock (ctx , wc , - adjust , now , flag , expensiveLog )
332
332
return adjust
333
333
}
334
334
@@ -599,39 +599,45 @@ func (t *tokenCounter) adjust(
599
599
if log .V (2 ) {
600
600
expensiveLog = true
601
601
}
602
- func () {
603
- t .mu .Lock ()
604
- defer t .mu .Unlock ()
605
- t .adjustLocked (ctx , class , delta , now , flag , expensiveLog )
606
- }()
607
-
602
+ t .mu .Lock ()
603
+ t .adjustLockedAndUnlock (ctx , class , delta , now , flag , expensiveLog ) // nolint:deferunlockcheck
608
604
}
609
605
610
- func (t * tokenCounter ) adjustLocked (
606
+ func (t * tokenCounter ) adjustLockedAndUnlock (
611
607
ctx context.Context ,
612
608
class admissionpb.WorkClass ,
613
609
delta kvflowcontrol.Tokens ,
614
610
now time.Time ,
615
611
flag TokenAdjustFlag ,
616
612
expensiveLog bool ,
617
613
) {
614
+ t .mu .AssertHeld ()
618
615
var adjustment , unaccounted tokensPerWorkClass
619
- switch class {
620
- case admissionpb .RegularWorkClass :
621
- adjustment .regular , unaccounted .regular =
622
- t .mu .counters [admissionpb .RegularWorkClass ].adjustTokensLocked (
623
- ctx , delta , now , false /* isReset */ , flag )
616
+ // Only populated when expensiveLog is true.
617
+ var regularTokens , elasticTokens kvflowcontrol.Tokens
618
+ func () {
619
+ defer t .mu .Unlock ()
620
+ switch class {
621
+ case regular :
622
+ adjustment .regular , unaccounted .regular =
623
+ t .mu .counters [regular ].adjustTokensLocked (
624
+ ctx , delta , now , false /* isReset */ , flag )
624
625
// Regular {deductions,returns} also affect elastic flow tokens.
625
- adjustment .elastic , unaccounted .elastic =
626
- t .mu .counters [admissionpb .ElasticWorkClass ].adjustTokensLocked (
627
- ctx , delta , now , false /* isReset */ , flag )
628
-
629
- case admissionpb .ElasticWorkClass :
630
- // Elastic {deductions,returns} only affect elastic flow tokens.
631
- adjustment .elastic , unaccounted .elastic =
632
- t .mu .counters [admissionpb .ElasticWorkClass ].adjustTokensLocked (
633
- ctx , delta , now , false /* isReset */ , flag )
634
- }
626
+ adjustment .elastic , unaccounted .elastic =
627
+ t .mu .counters [elastic ].adjustTokensLocked (
628
+ ctx , delta , now , false /* isReset */ , flag )
629
+
630
+ case elastic :
631
+ // Elastic {deductions,returns} only affect elastic flow tokens.
632
+ adjustment .elastic , unaccounted .elastic =
633
+ t .mu .counters [elastic ].adjustTokensLocked (
634
+ ctx , delta , now , false /* isReset */ , flag )
635
+ }
636
+ if expensiveLog {
637
+ regularTokens = t .tokensLocked (regular )
638
+ elasticTokens = t .tokensLocked (elastic )
639
+ }
640
+ }()
635
641
636
642
// Adjust metrics if any tokens were actually adjusted or unaccounted for
637
643
// tokens were detected.
@@ -643,7 +649,7 @@ func (t *tokenCounter) adjustLocked(
643
649
}
644
650
if expensiveLog {
645
651
log .Infof (ctx , "adjusted %v flow tokens (wc=%v stream=%v delta=%v flag=%v): regular=%v elastic=%v" ,
646
- t .tokenType , class , t .stream , delta , flag , t . tokensLocked ( regular ), t . tokensLocked ( elastic ) )
652
+ t .tokenType , class , t .stream , delta , flag , regularTokens , elasticTokens )
647
653
}
648
654
}
649
655
0 commit comments