|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2016 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.net.pooling; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.concurrent.BlockingQueue; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | +import java.util.concurrent.LinkedBlockingQueue; |
| 28 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 29 | + |
| 30 | +import org.neo4j.driver.internal.util.Supplier; |
| 31 | + |
| 32 | +/** |
| 33 | + * A blocking queue that also keeps track of connections that are acquired in order |
| 34 | + * to facilitate termination of all connections. |
| 35 | + */ |
| 36 | +public class BlockingPooledConnectionQueue |
| 37 | +{ |
| 38 | + /** The backing queue, keeps track of connections currently in queue */ |
| 39 | + private final BlockingQueue<PooledConnection> queue; |
| 40 | + |
| 41 | + private final AtomicBoolean isTerminating = new AtomicBoolean( false ); |
| 42 | + |
| 43 | + /** Keeps track of acquired connections */ |
| 44 | + private final Set<PooledConnection> acquiredConnections = |
| 45 | + Collections.newSetFromMap(new ConcurrentHashMap<PooledConnection, Boolean>()); |
| 46 | + |
| 47 | + public BlockingPooledConnectionQueue( int capacity ) |
| 48 | + { |
| 49 | + this.queue = new LinkedBlockingQueue<>( capacity ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Offer a connections back to the queue |
| 54 | + * |
| 55 | + * @param pooledConnection the connection to put back to the queue |
| 56 | + * @return <code>true</code> if connections was accepted otherwise <code>false</code> |
| 57 | + */ |
| 58 | + public boolean offer( PooledConnection pooledConnection ) |
| 59 | + { |
| 60 | + acquiredConnections.remove( pooledConnection ); |
| 61 | + boolean offer = queue.offer( pooledConnection ); |
| 62 | + // not added back to the queue, dispose of the connection |
| 63 | + if (!offer) { |
| 64 | + pooledConnection.dispose(); |
| 65 | + } |
| 66 | + if (isTerminating.get()) { |
| 67 | + PooledConnection poll = queue.poll(); |
| 68 | + if (poll != null) |
| 69 | + { |
| 70 | + poll.dispose(); |
| 71 | + } |
| 72 | + } |
| 73 | + return offer; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Acquire connection or create a new one if the queue is empty |
| 78 | + * @param supplier used to create a new connection if queue is empty |
| 79 | + * @return a PooledConnection instance |
| 80 | + */ |
| 81 | + public PooledConnection acquire( Supplier<PooledConnection> supplier ) |
| 82 | + { |
| 83 | + |
| 84 | + PooledConnection poll = queue.poll(); |
| 85 | + if ( poll == null ) |
| 86 | + { |
| 87 | + poll = supplier.get(); |
| 88 | + } |
| 89 | + acquiredConnections.add( poll ); |
| 90 | + |
| 91 | + if (isTerminating.get()) { |
| 92 | + acquiredConnections.remove( poll ); |
| 93 | + poll.dispose(); |
| 94 | + throw new IllegalStateException( "Pool has been closed, cannot acquire new values." ); |
| 95 | + } |
| 96 | + return poll; |
| 97 | + } |
| 98 | + |
| 99 | + public List<PooledConnection> toList() |
| 100 | + { |
| 101 | + return new ArrayList<>( queue ); |
| 102 | + } |
| 103 | + |
| 104 | + public boolean isEmpty() |
| 105 | + { |
| 106 | + return queue.isEmpty(); |
| 107 | + } |
| 108 | + |
| 109 | + public int size() |
| 110 | + { |
| 111 | + return queue.size(); |
| 112 | + } |
| 113 | + |
| 114 | + public boolean contains( PooledConnection pooledConnection ) |
| 115 | + { |
| 116 | + return queue.contains( pooledConnection ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Terminates all connections, both those that are currently in the queue as well |
| 121 | + * as those that have been acquired. |
| 122 | + */ |
| 123 | + public void terminate() |
| 124 | + { |
| 125 | + if (isTerminating.compareAndSet( false, true )) |
| 126 | + { |
| 127 | + while ( !queue.isEmpty() ) |
| 128 | + { |
| 129 | + PooledConnection conn = queue.poll(); |
| 130 | + if ( conn != null ) |
| 131 | + { |
| 132 | + //close the underlying connection without adding it back to the queue |
| 133 | + conn.dispose(); |
| 134 | + } |
| 135 | + } |
| 136 | + for ( PooledConnection pooledConnection : acquiredConnections ) |
| 137 | + { |
| 138 | + pooledConnection.dispose(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments