-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFixedChannelPool.java
More file actions
348 lines (348 loc) · 12.4 KB
/
Copy pathFixedChannelPool.java
File metadata and controls
348 lines (348 loc) · 12.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by
///FernFlower decompiler)//package io.netty.channel.pool;
io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.ObjectUtil;
import java.nio.channels.ClosedChannelException;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
public class FixedChannelPool extends SimpleChannelPool {
private final EventExecutor executor;
private final long acquireTimeoutNanos;
private final Runnable timeoutTask;
private final Queue<AcquireTask> pendingAcquireQueue;
private final int maxConnections;
private final int maxPendingAcquires;
private final AtomicInteger acquiredChannelCount;
private int pendingAcquireCount;
private boolean closed;
public FixedChannelPool(
Bootstrap bootstrap, ChannelPoolHandler handler, int maxConnections) {
this(bootstrap, handler, maxConnections, Integer.MAX_VALUE);
}
public FixedChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler,
int maxConnections, int maxPendingAcquires) {
this(bootstrap, handler, ChannelHealthChecker.ACTIVE,
(AcquireTimeoutAction) null, -1L, maxConnections, maxPendingAcquires);
}
public FixedChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler,
ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires) {
this(bootstrap, handler, healthCheck, action, acquireTimeoutMillis,
maxConnections, maxPendingAcquires, true);
}
public FixedChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler,
ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires,
boolean releaseHealthCheck) {
this(bootstrap, handler, healthCheck, action, acquireTimeoutMillis,
maxConnections, maxPendingAcquires, releaseHealthCheck, true);
}
public FixedChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler,
ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires,
boolean releaseHealthCheck, boolean lastRecentUsed) {
super(bootstrap, handler, healthCheck, releaseHealthCheck, lastRecentUsed);
this.pendingAcquireQueue = new ArrayDeque();
this.acquiredChannelCount = new AtomicInteger();
ObjectUtil.checkPositive(maxConnections, "maxConnections");
ObjectUtil.checkPositive(maxPendingAcquires, "maxPendingAcquires");
if (action == null && acquireTimeoutMillis == -1L) {
this.timeoutTask = null;
this.acquireTimeoutNanos = -1L;
} else {
if (action == null && acquireTimeoutMillis != -1L) {
throw new NullPointerException("action");
}
if (action != null && acquireTimeoutMillis < 0L) {
throw new IllegalArgumentException("acquireTimeoutMillis: "
+ acquireTimeoutMillis + " (expected: >= 0)");
}
this.acquireTimeoutNanos =
TimeUnit.MILLISECONDS.toNanos(acquireTimeoutMillis);
switch (action) {
case FAIL:
this.timeoutTask = new TimeoutTask() {
public void onTimeout(AcquireTask task) {
task.promise.setFailure(new AcquireTimeoutException());
}
};
break;
case NEW:
this.timeoutTask = new TimeoutTask() {
public void onTimeout(AcquireTask task) {
task.acquired();
FixedChannelPool.super.acquire(task.promise);
}
};
break;
default:
throw new Error();
}
}
this.executor = bootstrap.config().group().next();
this.maxConnections = maxConnections;
this.maxPendingAcquires = maxPendingAcquires;
}
public int acquiredChannelCount() {
return this.acquiredChannelCount.get();
}
public Future<Channel> acquire(final Promise<Channel> promise) {
try {
if (this.executor.inEventLoop()) {
this.acquire0(promise);
} else {
this.executor.execute(new Runnable() {
public void run() {
FixedChannelPool.this.acquire0(promise);
}
});
}
} catch (Throwable cause) {
promise.tryFailure(cause);
}
return promise;
}
private void acquire0(Promise<Channel> promise) {
try {
assert this.executor.inEventLoop();
if (this.closed) {
promise.setFailure(
new IllegalStateException("FixedChannelPool was closed"));
return;
}
if (this.acquiredChannelCount.get() < this.maxConnections) {
assert this.acquiredChannelCount.get() >= 0;
Promise<Channel> p = this.executor.newPromise();
AcquireListener l = new AcquireListener(promise);
l.acquired();
p.addListener(l);
super.acquire(p);
} else {
if (this.pendingAcquireCount >= this.maxPendingAcquires) {
this.tooManyOutstanding(promise);
} else {
AcquireTask task = new AcquireTask(promise);
if (this.pendingAcquireQueue.offer(task)) {
++this.pendingAcquireCount;
if (this.timeoutTask != null) {
task.timeoutFuture = this.executor.schedule(this.timeoutTask,
this.acquireTimeoutNanos, TimeUnit.NANOSECONDS);
}
} else {
this.tooManyOutstanding(promise);
}
}
assert this.pendingAcquireCount > 0;
}
} catch (Throwable cause) {
promise.tryFailure(cause);
}
}
private void tooManyOutstanding(Promise<?> promise) {
promise.setFailure(
new IllegalStateException("Too many outstanding acquire operations"));
}
public Future<Void> release(
final Channel channel, final Promise<Void> promise) {
ObjectUtil.checkNotNull(promise, "promise");
Promise<Void> p = this.executor.newPromise();
super.release(channel, p.addListener(new FutureListener<Void>() {
public void operationComplete(Future<Void> future) {
try {
assert FixedChannelPool.this.executor.inEventLoop();
if (FixedChannelPool.this.closed) {
channel.close();
promise.setFailure(
new IllegalStateException("FixedChannelPool was closed"));
return;
}
if (future.isSuccess()) {
FixedChannelPool.this.decrementAndRunTaskQueue();
promise.setSuccess((Object) null);
} else {
Throwable cause = future.cause();
if (!(cause instanceof IllegalArgumentException)) {
FixedChannelPool.this.decrementAndRunTaskQueue();
}
promise.setFailure(future.cause());
}
} catch (Throwable cause) {
promise.tryFailure(cause);
}
}
}));
return promise;
}
private void decrementAndRunTaskQueue() {
int currentCount = this.acquiredChannelCount.decrementAndGet();
assert currentCount >= 0;
this.runTaskQueue();
}
private void runTaskQueue() {
while (true) {
if (this.acquiredChannelCount.get() < this.maxConnections) {
AcquireTask task = (AcquireTask) this.pendingAcquireQueue.poll();
if (task != null) {
ScheduledFuture<?> timeoutFuture = task.timeoutFuture;
if (timeoutFuture != null) {
timeoutFuture.cancel(false);
}
--this.pendingAcquireCount;
task.acquired();
super.acquire(task.promise);
continue;
}
}
assert this.pendingAcquireCount >= 0;
assert this.acquiredChannelCount.get() >= 0;
return;
}
}
public void close() {
try {
this.closeAsync().await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
public Future<Void> closeAsync() {
if (this.executor.inEventLoop()) {
return this.close0();
} else {
final Promise<Void> closeComplete = this.executor.newPromise();
this.executor.execute(new Runnable() {
public void run() {
FixedChannelPool.this.close0().addListener(
new FutureListener<Void>() {
public void operationComplete(Future<Void> f) throws Exception {
if (f.isSuccess()) {
closeComplete.setSuccess((Object) null);
} else {
closeComplete.setFailure(f.cause());
}
}
});
}
});
return closeComplete;
}
}
private Future<Void> close0() {
assert this.executor.inEventLoop();
if (!this.closed) {
this.closed = true;
while (true) {
AcquireTask task = (AcquireTask) this.pendingAcquireQueue.poll();
if (task == null) {
this.acquiredChannelCount.set(0);
this.pendingAcquireCount = 0;
return GlobalEventExecutor.INSTANCE.submit(new Callable<Void>() {
public Void call() throws Exception {
FixedChannelPool.super.close();
return null;
}
});
}
ScheduledFuture<?> f = task.timeoutFuture;
if (f != null) {
f.cancel(false);
}
task.promise.setFailure(new ClosedChannelException());
}
} else {
return GlobalEventExecutor.INSTANCE.newSucceededFuture((Object) null);
}
}
public static enum AcquireTimeoutAction {
NEW,
FAIL;
}
private final class AcquireTask extends AcquireListener {
final Promise<Channel> promise;
final long expireNanoTime;
ScheduledFuture<?> timeoutFuture;
AcquireTask(Promise<Channel> promise) {
super(promise);
this.expireNanoTime =
System.nanoTime() + FixedChannelPool.this.acquireTimeoutNanos;
this.promise =
FixedChannelPool.this.executor.newPromise().addListener(this);
}
}
private abstract class TimeoutTask implements Runnable {
private TimeoutTask() {}
public final void run() {
assert FixedChannelPool.this.executor.inEventLoop();
long nanoTime = System.nanoTime();
while (true) {
AcquireTask task =
(AcquireTask) FixedChannelPool.this.pendingAcquireQueue.peek();
if (task == null || nanoTime - task.expireNanoTime < 0L) {
return;
}
FixedChannelPool.this.pendingAcquireQueue.remove();
--FixedChannelPool.this.pendingAcquireCount;
this.onTimeout(task);
}
}
public abstract void onTimeout(AcquireTask var1);
}
private class AcquireListener implements FutureListener<Channel> {
private final Promise<Channel> originalPromise;
protected boolean acquired;
AcquireListener(Promise<Channel> originalPromise) {
this.originalPromise = originalPromise;
}
public void operationComplete(Future<Channel> future) throws Exception {
try {
assert FixedChannelPool.this.executor.inEventLoop();
if (FixedChannelPool.this.closed) {
if (future.isSuccess()) {
((Channel) future.getNow()).close();
}
this.originalPromise.setFailure(
new IllegalStateException("FixedChannelPool was closed"));
return;
}
if (future.isSuccess()) {
this.originalPromise.setSuccess(future.getNow());
} else {
if (this.acquired) {
FixedChannelPool.this.decrementAndRunTaskQueue();
} else {
FixedChannelPool.this.runTaskQueue();
}
this.originalPromise.setFailure(future.cause());
}
} catch (Throwable cause) {
this.originalPromise.tryFailure(cause);
}
}
public void acquired() {
if (!this.acquired) {
FixedChannelPool.this.acquiredChannelCount.incrementAndGet();
this.acquired = true;
}
}
}
private static final class AcquireTimeoutException extends TimeoutException {
private AcquireTimeoutException() {
super("Acquire operation took longer then configured maximum time");
}
public Throwable fillInStackTrace() {
return this;
}
}
}