|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty.internal; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils.consumeOrPropagate; |
| 19 | + |
| 20 | +import io.netty.channel.Channel; |
| 21 | +import io.netty.channel.EventLoop; |
| 22 | +import io.netty.channel.EventLoopGroup; |
| 23 | +import io.netty.channel.pool.ChannelPoolHandler; |
| 24 | +import io.netty.util.concurrent.Future; |
| 25 | +import io.netty.util.concurrent.Promise; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.function.Supplier; |
| 29 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 30 | +import software.amazon.awssdk.http.nio.netty.internal.http2.HttpOrHttp2ChannelPool; |
| 31 | +import software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils; |
| 32 | +import software.amazon.awssdk.metrics.MetricCollector; |
| 33 | + |
| 34 | +/** |
| 35 | + * A {@link SdkChannelPool} that wraps and delegates to another {@link SdkChannelPool} while invoking {@link ChannelPoolListener}s |
| 36 | + * for important events that occur. |
| 37 | + * <p> |
| 38 | + * {@link ChannelPoolListener} is similar to Netty's {@link ChannelPoolHandler} interface, but by invoking them as part of a |
| 39 | + * {@link SdkChannelPool} wrapper, we are given more control over when they are invoked. For example, {@link |
| 40 | + * HttpOrHttp2ChannelPool} may choose not to release HTTP/2 stream channels to the lowest-level pool (and instead store the |
| 41 | + * channels in its own pool), but by instrumenting listeners that sit on top of this layer, we are still given visibility into |
| 42 | + * these events occurring. |
| 43 | + */ |
| 44 | +@SdkInternalApi |
| 45 | +public final class ListenerInvokingChannelPool implements SdkChannelPool { |
| 46 | + private final SdkChannelPool delegatePool; |
| 47 | + private final Supplier<Promise<Channel>> promiseFactory; |
| 48 | + private final List<ChannelPoolListener> listeners; |
| 49 | + |
| 50 | + /** |
| 51 | + * Listener which is called for various actions performed on a {@link SdkChannelPool}. All listener events are guaranteed to |
| 52 | + * be invoked as part of the {@link Channel}'s {@link EventLoop}. |
| 53 | + */ |
| 54 | + @SdkInternalApi |
| 55 | + public interface ChannelPoolListener { |
| 56 | + |
| 57 | + /** |
| 58 | + * Called <b>after</b> a {@link Channel} was acquired by calling {@link SdkChannelPool#acquire()} or {@link |
| 59 | + * SdkChannelPool#acquire(Promise)}. |
| 60 | + * <p> |
| 61 | + * This method will be called by the {@link EventLoop} of the {@link Channel}. |
| 62 | + */ |
| 63 | + default void channelAcquired(Channel channel) { |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Called <b>before</b> a {@link Channel} is released by calling {@link SdkChannelPool#release(Channel)} or {@link |
| 68 | + * SdkChannelPool#release(Channel, Promise)}. |
| 69 | + * <p> |
| 70 | + * This method will be called by the {@link EventLoop} of the {@link Channel}. |
| 71 | + */ |
| 72 | + default void channelReleased(Channel channel) { |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public ListenerInvokingChannelPool(EventLoopGroup eventLoopGroup, |
| 77 | + SdkChannelPool delegatePool, |
| 78 | + List<ChannelPoolListener> listeners) { |
| 79 | + this(() -> eventLoopGroup.next().newPromise(), delegatePool, listeners); |
| 80 | + } |
| 81 | + |
| 82 | + public ListenerInvokingChannelPool(Supplier<Promise<Channel>> promiseFactory, |
| 83 | + SdkChannelPool delegatePool, |
| 84 | + List<ChannelPoolListener> listeners) { |
| 85 | + this.delegatePool = delegatePool; |
| 86 | + this.promiseFactory = promiseFactory; |
| 87 | + this.listeners = listeners; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Future<Channel> acquire() { |
| 92 | + return acquire(promiseFactory.get()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Future<Channel> acquire(Promise<Channel> returnFuture) { |
| 97 | + delegatePool.acquire(promiseFactory.get()) |
| 98 | + .addListener(consumeOrPropagate(returnFuture, channel -> { |
| 99 | + NettyUtils.doInEventLoop(channel.eventLoop(), () -> { |
| 100 | + invokeChannelAcquired(channel); |
| 101 | + returnFuture.trySuccess(channel); |
| 102 | + }, returnFuture); |
| 103 | + })); |
| 104 | + return returnFuture; |
| 105 | + } |
| 106 | + |
| 107 | + private void invokeChannelAcquired(Channel channel) { |
| 108 | + listeners.forEach(listener -> listener.channelAcquired(channel)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Future<Void> release(Channel channel) { |
| 113 | + return release(channel, channel.eventLoop().newPromise()); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Future<Void> release(Channel channel, Promise<Void> returnFuture) { |
| 118 | + NettyUtils.doInEventLoop(channel.eventLoop(), () -> { |
| 119 | + invokeChannelReleased(channel); |
| 120 | + delegatePool.release(channel, returnFuture); |
| 121 | + }, returnFuture); |
| 122 | + return returnFuture; |
| 123 | + } |
| 124 | + |
| 125 | + private void invokeChannelReleased(Channel channel) { |
| 126 | + listeners.forEach(listener -> listener.channelReleased(channel)); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + @Override |
| 131 | + public void close() { |
| 132 | + delegatePool.close(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public CompletableFuture<Void> collectChannelPoolMetrics(MetricCollector metrics) { |
| 137 | + return delegatePool.collectChannelPoolMetrics(metrics); |
| 138 | + } |
| 139 | +} |
0 commit comments