File tree Expand file tree Collapse file tree
java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1 Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -681,33 +681,25 @@ public ApiFuture<PublishResponse> call() {
681681
682682 void refillTokenBucket () {
683683 if (hedgeSettings != null ) {
684- while (true ) {
685- int current = hedgeTokenBucket .get ();
686- if (current >= scaledMaxHedgeTokens ) {
687- return ;
688- }
689- int next = Math .min (scaledMaxHedgeTokens , current + scaledHedgeRefillAmount );
690- if (hedgeTokenBucket .compareAndSet (current , next )) {
691- return ;
692- }
693- }
684+ hedgeTokenBucket .accumulateAndGet (
685+ scaledHedgeRefillAmount ,
686+ (current , refill ) -> Math .min (scaledMaxHedgeTokens , current + refill ));
694687 }
695688 }
696689
697690 boolean tryAcquireHedgeToken () {
698691 if (hedgeSettings == null ) {
699692 return false ;
700693 }
701- while (true ) {
702- int current = hedgeTokenBucket .get ();
703- if (current < HEDGE_TOKEN_SCALE ) {
704- return false ;
705- }
706- int next = current - HEDGE_TOKEN_SCALE ;
707- if (hedgeTokenBucket .compareAndSet (current , next )) {
708- return true ;
709- }
710- }
694+ int previous =
695+ hedgeTokenBucket .getAndUpdate (
696+ current -> {
697+ if (current < HEDGE_TOKEN_SCALE ) {
698+ return current ;
699+ }
700+ return current - HEDGE_TOKEN_SCALE ;
701+ });
702+ return previous >= HEDGE_TOKEN_SCALE ;
711703 }
712704
713705 private ApiFuture <PublishResponse > startHedgedCall (final OutstandingBatch outstandingBatch ) {
You can’t perform that action at this time.
0 commit comments