forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentPool.java
More file actions
226 lines (196 loc) · 6.46 KB
/
Copy pathConcurrentPool.java
File metadata and controls
226 lines (196 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* 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
*
* http://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 com.mongodb;
import java.util.Iterator;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
class ConcurrentPool<T> {
private final int maxSize;
private final ItemFactory<T> itemFactory;
private final Deque<T> available = new ConcurrentLinkedDeque<T>();
private final Semaphore permits;
private volatile boolean closed;
/**
* Factory for creating and closing pooled items.
*
* @param <T>
*/
interface ItemFactory<T> {
T create();
void close(T t);
boolean shouldPrune(T t);
}
/**
* Initializes a new pool of objects.
*
* @param maxSize max to hold to at any given time. if < 0 then no limit
* @param itemFactory factory used to create and close items in the pool
*/
public ConcurrentPool(final int maxSize, final ItemFactory<T> itemFactory) {
this.maxSize = maxSize;
this.itemFactory = itemFactory;
permits = new Semaphore(maxSize, true);
}
/**
* Return an instance of T to the pool. This method simply calls {@code release(t, false)}
*
* @param t item to return to the pool
*/
public void release(final T t) {
release(t, false);
}
/**
* call done when you are done with an object from the pool if there is room and the object is ok will get added
*
* @param t item to return to the pool
* @param prune true if the item should be closed, false if it should be put back in the pool
*/
public void release(final T t, final boolean prune) {
if (t == null) {
throw new IllegalArgumentException("Can not return a null item to the pool");
}
if (closed) {
close(t);
return;
}
if (prune) {
close(t);
} else {
available.addLast(t);
}
releasePermit();
}
/**
* Gets an object from the pool. This method will block until a permit is available.
*
* @return An object from the pool.
*/
public T get() {
return get(-1, TimeUnit.MILLISECONDS);
}
/**
* Gets an object from the pool - will block if none are available
*
* @param timeout negative - forever 0 - return immediately no matter what positive ms to wait
* @param timeUnit the time unit of the timeout
* @return An object from the pool, or null if can't get one in the given waitTime
* @throws MongoTimeoutException if the timeout has been exceeded
*/
public T get(final long timeout, final TimeUnit timeUnit) {
if (closed) {
throw new IllegalStateException("The pool is closed");
}
if (!acquirePermit(timeout, timeUnit)) {
throw new MongoTimeoutException(String.format("Timeout waiting for a pooled item after %d %s", timeout, timeUnit));
}
T t = available.pollLast();
if (t == null) {
t = createNewAndReleasePermitIfFailure();
}
return t;
}
public void prune() {
int currentAvailableCount = getAvailableCount();
for (int numAttempts = 0; numAttempts < currentAvailableCount; numAttempts++) {
if (!acquirePermit(10, TimeUnit.MILLISECONDS)) {
break;
}
T cur = available.pollFirst();
if (cur == null) {
releasePermit();
break;
}
release(cur, itemFactory.shouldPrune(cur));
}
}
public void ensureMinSize(final int minSize) {
while (getCount() < minSize) {
if (!acquirePermit(10, TimeUnit.MILLISECONDS)) {
break;
}
release(createNewAndReleasePermitIfFailure());
}
}
private T createNewAndReleasePermitIfFailure() {
try {
T newMember = itemFactory.create();
if (newMember == null) {
throw new MongoInternalException("The factory for the pool created a null item");
}
return newMember;
} catch (RuntimeException e) {
permits.release();
throw e;
}
}
protected boolean acquirePermit(final long timeout, final TimeUnit timeUnit) {
try {
if (closed) {
return false;
} else if (timeout >= 0) {
return permits.tryAcquire(timeout, timeUnit);
} else {
permits.acquire();
return true;
}
} catch (InterruptedException e) {
throw new MongoInterruptedException("Interrupted acquiring a permit to retrieve an item from the pool ", e);
}
}
protected void releasePermit() {
permits.release();
}
/**
* Clears the pool of all objects.
*/
public void close() {
closed = true;
Iterator<T> iter = available.iterator();
while (iter.hasNext()) {
T t = iter.next();
close(t);
iter.remove();
}
}
public int getMaxSize() {
return maxSize;
}
public int getInUseCount() {
return maxSize - permits.availablePermits();
}
public int getAvailableCount() {
return available.size();
}
public int getCount() {
return getInUseCount() + getAvailableCount();
}
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("pool: ")
.append(" maxSize: ").append(maxSize)
.append(" availableCount ").append(getAvailableCount())
.append(" inUseCount ").append(getInUseCount());
return buf.toString();
}
// swallow exceptions from ItemFactory.close()
private void close(final T t) {
try {
itemFactory.close(t);
} catch (RuntimeException e) {
// ItemFactory.close() really should not throw
}
}
}