|
13 | 13 |
|
14 | 14 | package org.asynchttpclient.providers.grizzly;
|
15 | 15 |
|
| 16 | +import org.glassfish.grizzly.CompletionHandler; |
| 17 | +import org.glassfish.grizzly.Connection; |
| 18 | +import org.glassfish.grizzly.EmptyCompletionHandler; |
| 19 | +import org.glassfish.grizzly.GrizzlyFuture; |
16 | 20 | import org.glassfish.grizzly.connectionpool.EndpointKey;
|
17 | 21 | import org.glassfish.grizzly.connectionpool.MultiEndpointPool;
|
18 | 22 | import org.glassfish.grizzly.connectionpool.SingleEndpointPool;
|
|
29 | 33 | */
|
30 | 34 | public class ConnectionPool extends MultiEndpointPool<SocketAddress>{
|
31 | 35 |
|
| 36 | + private final Object lock = new Object(); |
32 | 37 |
|
33 | 38 | // ------------------------------------------------------------ Constructors
|
34 | 39 |
|
@@ -69,6 +74,62 @@ protected SingleEndpointPool<SocketAddress> obtainSingleEndpointPool(
|
69 | 74 | return sePool;
|
70 | 75 | }
|
71 | 76 |
|
| 77 | + @Override |
| 78 | + public GrizzlyFuture<Connection> take(final EndpointKey<SocketAddress> endpointKey) { |
| 79 | + synchronized (lock) { |
| 80 | + final GrizzlyFuture<Connection> f = super.take(endpointKey); |
| 81 | + f.addCompletionHandler(new EmptyCompletionHandler<Connection>() { |
| 82 | + @Override |
| 83 | + public void completed(Connection result) { |
| 84 | + if (Utils.isSpdyConnection(result)) { |
| 85 | + release(result); |
| 86 | + } |
| 87 | + super.completed(result); |
| 88 | + } |
| 89 | + }); |
| 90 | + return f; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void take(final EndpointKey<SocketAddress> endpointKey, |
| 96 | + final CompletionHandler<Connection> completionHandler) { |
| 97 | + synchronized (lock) { |
| 98 | + if (completionHandler == null) { |
| 99 | + throw new IllegalStateException("CompletionHandler argument cannot be null."); |
| 100 | + } |
| 101 | + |
| 102 | + super.take(endpointKey, new CompletionHandler<Connection>() { |
| 103 | + @Override |
| 104 | + public void cancelled() { |
| 105 | + completionHandler.cancelled(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void failed(Throwable throwable) { |
| 110 | + completionHandler.failed(throwable); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void completed(Connection result) { |
| 115 | + release(result); |
| 116 | + completionHandler.completed(result); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void updated(Connection result) { |
| 121 | + completionHandler.updated(result); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean release(Connection connection) { |
| 129 | + synchronized (lock) { |
| 130 | + return super.release(connection); |
| 131 | + } |
| 132 | + } |
72 | 133 |
|
73 | 134 | // ---------------------------------------------------------- Nested Classes
|
74 | 135 |
|
|
0 commit comments