|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-2019 Pivotal Software Inc, 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://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 | + |
| 17 | +package reactor.rabbitmq; |
| 18 | + |
| 19 | +import com.rabbitmq.client.Channel; |
| 20 | +import com.rabbitmq.client.Connection; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.core.publisher.SignalType; |
| 23 | +import reactor.core.scheduler.Scheduler; |
| 24 | +import reactor.core.scheduler.Schedulers; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.BlockingQueue; |
| 30 | +import java.util.concurrent.LinkedBlockingQueue; |
| 31 | +import java.util.function.BiConsumer; |
| 32 | + |
| 33 | +import static reactor.rabbitmq.ChannelCloseHandlers.SENDER_CHANNEL_CLOSE_HANDLER_INSTANCE; |
| 34 | + |
| 35 | +/** |
| 36 | + * This channel pool is lazy initialized. It might even not reach its maximum size {@link ChannelPoolOptions#getMaxCacheSize()} in low-concurrency environments. |
| 37 | + * It always tries to obtain channel from the pool. However, in case of high-concurrency environments, number of channels might exceeds channel pool maximum size. |
| 38 | + * |
| 39 | + * Channels are added to the pool after their use {@link ChannelPool#getChannelCloseHandler()} and obtained from the pool when channel is requested {@link ChannelPool#getChannelMono()}. |
| 40 | + * |
| 41 | + * If pool is empty, new channel is created. |
| 42 | + * If channel is no longer needed and the channel pool is full, then channel is being closed. |
| 43 | + * If channel is no longer needed and the channel pool has not reached its capacity, then channel is added to the pool. |
| 44 | + * |
| 45 | + * It uses {@link BlockingQueue} internally in a non-blocking way. |
| 46 | + * |
| 47 | + */ |
| 48 | +class LazyChannelPool implements ChannelPool { |
| 49 | + |
| 50 | + private static final int DEFAULT_CHANNEL_POOL_SIZE = 5; |
| 51 | + |
| 52 | + private final Mono<? extends Connection> connectionMono; |
| 53 | + private final BlockingQueue<Channel> channelsQueue; |
| 54 | + private final Scheduler subscriptionScheduler; |
| 55 | + |
| 56 | + LazyChannelPool(Mono<? extends Connection> connectionMono, ChannelPoolOptions channelPoolOptions) { |
| 57 | + int channelsQueueCapacity = channelPoolOptions.getMaxCacheSize() == null ? |
| 58 | + DEFAULT_CHANNEL_POOL_SIZE : channelPoolOptions.getMaxCacheSize(); |
| 59 | + this.channelsQueue = new LinkedBlockingQueue<>(channelsQueueCapacity); |
| 60 | + this.connectionMono = connectionMono; |
| 61 | + this.subscriptionScheduler = channelPoolOptions.getSubscriptionScheduler() == null ? |
| 62 | + Schedulers.newElastic("sender-channel-pool") : channelPoolOptions.getSubscriptionScheduler(); |
| 63 | + } |
| 64 | + |
| 65 | + public Mono<? extends Channel> getChannelMono() { |
| 66 | + return connectionMono.map(connection -> { |
| 67 | + Channel channel = channelsQueue.poll(); |
| 68 | + if (channel == null) { |
| 69 | + channel = createChannel(connection); |
| 70 | + } |
| 71 | + return channel; |
| 72 | + }) |
| 73 | + .subscribeOn(subscriptionScheduler); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public BiConsumer<SignalType, Channel> getChannelCloseHandler() { |
| 78 | + return (signalType, channel) -> { |
| 79 | + if (!channel.isOpen()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + boolean offer = signalType == SignalType.ON_COMPLETE && channelsQueue.offer(channel); |
| 83 | + if (!offer) { |
| 84 | + SENDER_CHANNEL_CLOSE_HANDLER_INSTANCE.accept(signalType, channel); |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void close() { |
| 91 | + List<Channel> channels = new ArrayList<>(); |
| 92 | + channelsQueue.drainTo(channels); |
| 93 | + channels.forEach(channel -> { |
| 94 | + SENDER_CHANNEL_CLOSE_HANDLER_INSTANCE.accept(SignalType.ON_COMPLETE, channel); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private Channel createChannel(Connection connection) { |
| 99 | + try { |
| 100 | + return connection.createChannel(); |
| 101 | + } catch (IOException e) { |
| 102 | + throw new RabbitFluxException("Error while creating channel", e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments