forked from awspring/spring-cloud-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a CompositeBackPressureHandler allowing for composition of …
…BackPressureHandlers (awspring#1251)
- Loading branch information
1 parent
93cb447
commit 432d490
Showing
7 changed files
with
201 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ud-aws-sqs/src/main/java/io/awspring/cloud/sqs/listener/CompositeBackPressureHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2013-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.awspring.cloud.sqs.listener; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
public class CompositeBackPressureHandler implements BatchAwareBackPressureHandler, IdentifiableContainerComponent { | ||
|
||
private final List<BackPressureHandler> backPressureHandlers; | ||
|
||
private final int batchSize; | ||
|
||
private String id; | ||
|
||
public CompositeBackPressureHandler(List<BackPressureHandler> backPressureHandlers, int batchSize) { | ||
this.backPressureHandlers = backPressureHandlers; | ||
this.batchSize = batchSize; | ||
} | ||
|
||
@Override | ||
public void setId(String id) { | ||
this.id = id; | ||
backPressureHandlers.stream().filter(IdentifiableContainerComponent.class::isInstance) | ||
.map(IdentifiableContainerComponent.class::cast) | ||
.forEach(bph -> bph.setId(bph.getClass().getSimpleName() + "-" + id)); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public int requestBatch() throws InterruptedException { | ||
return request(batchSize); | ||
} | ||
|
||
@Override | ||
public int request(int amount) throws InterruptedException { | ||
int obtained = amount; | ||
int[] obtainedPerBph = new int[backPressureHandlers.size()]; | ||
for (int i = 0; i < backPressureHandlers.size() && obtained > 0; i++) { | ||
obtainedPerBph[i] = backPressureHandlers.get(i).request(obtained); | ||
obtained = Math.min(obtained, obtainedPerBph[i]); | ||
} | ||
for (int i = 0; i < backPressureHandlers.size(); i++) { | ||
int obtainedForBph = obtainedPerBph[i]; | ||
if (obtainedForBph > obtained) { | ||
backPressureHandlers.get(i).release(obtainedForBph - obtained, ReleaseReason.LIMITED); | ||
} | ||
} | ||
return obtained; | ||
} | ||
|
||
@Override | ||
public void release(int amount, ReleaseReason reason) { | ||
for (BackPressureHandler handler : backPressureHandlers) { | ||
handler.release(amount, reason); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean drain(Duration timeout) { | ||
boolean result = true; | ||
for (BackPressureHandler handler : backPressureHandlers) { | ||
result &= !handler.drain(timeout); | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.