|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.sqs.listener; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * Spring Cloud AWS provides the following {@link BackPressureHandler} implementations: |
| 24 | + * <ul> |
| 25 | + * <li>{@link ConcurrencyLimiterBlockingBackPressureHandler}: Limits the maximum number of messages that can be * |
| 26 | + * processed concurrently by the application.</li> * |
| 27 | + * <li>{@link ThroughputBackPressureHandler}: Adapts the throughput dynamically between high and low modes in order to * |
| 28 | + * reduce SQS pull costs when few messages are coming in.</li> * |
| 29 | + * <li>{@link CompositeBackPressureHandler}: Allows combining multiple {@link BackPressureHandler} together and ensures |
| 30 | + * * they cooperate.</li> * |
| 31 | + * </ul> |
| 32 | + * <p> |
| 33 | + * Below are a few examples of how common use cases can be achieved. Keep in mind you can always create your own * |
| 34 | + * {@link BackPressureHandler} implementation and if needed combine it with the provided ones thanks to the * |
| 35 | + * {@link CompositeBackPressureHandler}. * * |
| 36 | + * <h3>A BackPressureHandler limiting the max concurrency with high throughput</h3> * * |
| 37 | + * |
| 38 | + * <pre>{@code |
| 39 | + * containerOptionsBuilder.backPressureHandlerFactory(containerOptions -> { |
| 40 | + * return ConcurrencyLimiterBlockingBackPressureHandler.builder() |
| 41 | + * .batchSize(containerOptions.getMaxMessagesPerPoll()) |
| 42 | + * .totalPermits(containerOptions.getMaxConcurrentMessages()) |
| 43 | + * .acquireTimeout(containerOptions.getMaxDelayBetweenPolls()) |
| 44 | + * .throughputConfiguration(BackPressureMode.FIXED_HIGH_THROUGHPUT) |
| 45 | + * .build() |
| 46 | + * }}</pre> |
| 47 | + * <p> |
| 48 | + * * * |
| 49 | + * <h3>A BackPressureHandler limiting the max concurrency with dynamic throughput</h3> * * |
| 50 | + * |
| 51 | + * <pre>{@code |
| 52 | + * containerOptionsBuilder.backPressureHandlerFactory(containerOptions -> { |
| 53 | + * int batchSize = containerOptions.getMaxMessagesPerPoll(); |
| 54 | + * var concurrencyLimiterBlockingBackPressureHandler = ConcurrencyLimiterBlockingBackPressureHandler.builder() |
| 55 | + * .batchSize(batchSize) |
| 56 | + * .totalPermits(containerOptions.getMaxConcurrentMessages()) |
| 57 | + * .acquireTimeout(containerOptions.getMaxDelayBetweenPolls()) |
| 58 | + * .throughputConfiguration(BackPressureMode.AUTO) |
| 59 | + * .build() |
| 60 | + * var throughputBackPressureHandler = ThroughputBackPressureHandler.builder() |
| 61 | + * .batchSize(batchSize) |
| 62 | + * .build(); |
| 63 | + * return new CompositeBackPressureHandler(List.of( |
| 64 | + * concurrencyLimiterBlockingBackPressureHandler, |
| 65 | + * throughputBackPressureHandler |
| 66 | + * ), |
| 67 | + * batchSize, |
| 68 | + * standbyLimitPollingInterval |
| 69 | + * ); |
| 70 | + * }}</pre> |
| 71 | + */ |
| 72 | +public class BackPressureHandlerFactories { |
| 73 | + |
| 74 | + private BackPressureHandlerFactories() { |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new {@link SemaphoreBackPressureHandler} instance based on the provided {@link ContainerOptions}. |
| 79 | + * |
| 80 | + * @param options the container options. |
| 81 | + * @return the created SemaphoreBackPressureHandler. |
| 82 | + */ |
| 83 | + public static BatchAwareBackPressureHandler semaphoreBackPressureHandler(ContainerOptions<?, ?> options) { |
| 84 | + return SemaphoreBackPressureHandler.builder().batchSize(options.getMaxMessagesPerPoll()) |
| 85 | + .totalPermits(options.getMaxConcurrentMessages()).acquireTimeout(options.getMaxDelayBetweenPolls()) |
| 86 | + .throughputConfiguration(options.getBackPressureMode()).build(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates a new {@link BackPressureHandler} instance based on the provided {@link ContainerOptions} combining a |
| 91 | + * {@link ConcurrencyLimiterBlockingBackPressureHandler}, a {@link ThroughputBackPressureHandler} and a |
| 92 | + * {@link FullBatchBackPressureHandler}. The exact combination of depends on the given {@link ContainerOptions}. |
| 93 | + * |
| 94 | + * @param options the container options. |
| 95 | + * @param maxIdleWaitTime the maximum amount of time to wait for a permit to be released in case no permits were |
| 96 | + * obtained. |
| 97 | + * @return the created SemaphoreBackPressureHandler. |
| 98 | + */ |
| 99 | + public static BatchAwareBackPressureHandler adaptativeThroughputBackPressureHandler(ContainerOptions<?, ?> options, |
| 100 | + Duration maxIdleWaitTime) { |
| 101 | + BackPressureMode backPressureMode = options.getBackPressureMode(); |
| 102 | + |
| 103 | + var concurrencyLimiterBlockingBackPressureHandler = concurrencyLimiterBackPressureHandler(options); |
| 104 | + if (backPressureMode == BackPressureMode.FIXED_HIGH_THROUGHPUT) { |
| 105 | + return concurrencyLimiterBlockingBackPressureHandler; |
| 106 | + } |
| 107 | + var backPressureHandlers = new ArrayList<BackPressureHandler>(); |
| 108 | + backPressureHandlers.add(concurrencyLimiterBlockingBackPressureHandler); |
| 109 | + |
| 110 | + // The ThroughputBackPressureHandler should run second in the chain as it is non-blocking. |
| 111 | + // Running it first would result in more polls as it would potentially limit the |
| 112 | + // ConcurrencyLimiterBlockingBackPressureHandler to a lower amount of requested permits |
| 113 | + // which means the ConcurrencyLimiterBlockingBackPressureHandler blocking behavior would |
| 114 | + // not be optimally leveraged. |
| 115 | + if (backPressureMode == BackPressureMode.AUTO |
| 116 | + || backPressureMode == BackPressureMode.ALWAYS_POLL_MAX_MESSAGES) { |
| 117 | + backPressureHandlers.add(throughputBackPressureHandler(options)); |
| 118 | + } |
| 119 | + |
| 120 | + // The FullBatchBackPressureHandler should run last in the chain to ensure that a full batch is requested or not |
| 121 | + if (backPressureMode == BackPressureMode.ALWAYS_POLL_MAX_MESSAGES) { |
| 122 | + backPressureHandlers.add(fullBatchBackPressureHandler(options)); |
| 123 | + } |
| 124 | + return compositeBackPressureHandler(options, maxIdleWaitTime, backPressureHandlers); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creates a new {@link ConcurrencyLimiterBlockingBackPressureHandler} instance based on the provided |
| 129 | + * {@link ContainerOptions}. |
| 130 | + * |
| 131 | + * @param options the container options. |
| 132 | + * @return the created ConcurrencyLimiterBlockingBackPressureHandler. |
| 133 | + */ |
| 134 | + public static CompositeBackPressureHandler compositeBackPressureHandler(ContainerOptions<?, ?> options, |
| 135 | + Duration maxIdleWaitTime, List<BackPressureHandler> backPressureHandlers) { |
| 136 | + return new CompositeBackPressureHandler(List.copyOf(backPressureHandlers), options.getMaxMessagesPerPoll(), |
| 137 | + maxIdleWaitTime); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Creates a new {@link ConcurrencyLimiterBlockingBackPressureHandler} instance based on the provided |
| 142 | + * {@link ContainerOptions}. |
| 143 | + * |
| 144 | + * @param options the container options. |
| 145 | + * @return the created ConcurrencyLimiterBlockingBackPressureHandler. |
| 146 | + */ |
| 147 | + public static ConcurrencyLimiterBlockingBackPressureHandler concurrencyLimiterBackPressureHandler( |
| 148 | + ContainerOptions<?, ?> options) { |
| 149 | + return ConcurrencyLimiterBlockingBackPressureHandler.builder().batchSize(options.getMaxMessagesPerPoll()) |
| 150 | + .totalPermits(options.getMaxConcurrentMessages()).throughputConfiguration(options.getBackPressureMode()) |
| 151 | + .acquireTimeout(options.getMaxDelayBetweenPolls()).build(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Creates a new {@link ThroughputBackPressureHandler} instance based on the provided {@link ContainerOptions}. |
| 156 | + * |
| 157 | + * @param options the container options. |
| 158 | + * @return the created ThroughputBackPressureHandler. |
| 159 | + */ |
| 160 | + public static ThroughputBackPressureHandler throughputBackPressureHandler(ContainerOptions<?, ?> options) { |
| 161 | + return ThroughputBackPressureHandler.builder().build(); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Creates a new {@link FullBatchBackPressureHandler} instance based on the provided {@link ContainerOptions}. |
| 166 | + * |
| 167 | + * @param options the container options. |
| 168 | + * @return the created FullBatchBackPressureHandler. |
| 169 | + */ |
| 170 | + public static FullBatchBackPressureHandler fullBatchBackPressureHandler(ContainerOptions<?, ?> options) { |
| 171 | + return FullBatchBackPressureHandler.builder().batchSize(options.getMaxMessagesPerPoll()).build(); |
| 172 | + } |
| 173 | +} |
0 commit comments