|
1 | 1 |
|
2 | 2 | package io.helidon.benchmark.nima.models;
|
3 | 3 |
|
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
4 | 6 | import java.util.concurrent.locks.ReentrantLock;
|
5 | 7 |
|
6 | 8 | import io.vertx.core.Vertx;
|
|
12 | 14 |
|
13 | 15 | class PgClientConnectionPool implements AutoCloseable {
|
14 | 16 |
|
15 |
| - private final int size; |
16 | 17 | private final Vertx vertx;
|
17 | 18 | private final PgConnectOptions options;
|
18 |
| - private final PgClientConnection[] connections; |
19 | 19 | private final ReentrantLock lock = new ReentrantLock();
|
| 20 | + private final Map<String, PgClientConnection> connectionMap = new HashMap<>(); |
20 | 21 |
|
21 |
| - public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) { |
22 |
| - this.size = size; |
| 22 | + public PgClientConnectionPool(Vertx vertx, PgConnectOptions options) { |
23 | 23 | this.vertx = vertx;
|
24 | 24 | this.options = options;
|
25 |
| - this.connections = new PgClientConnection[size]; |
26 | 25 | }
|
27 | 26 |
|
28 | 27 | public PgClientConnection clientConnection() {
|
29 |
| - int bucket = Thread.currentThread().hashCode() % size; |
30 |
| - if (connections[bucket] == null) { |
| 28 | + String carrierThread = carrierThread(); |
| 29 | + PgClientConnection connection = connectionMap.get(carrierThread); |
| 30 | + if (connection == null) { |
31 | 31 | try {
|
32 | 32 | lock.lock();
|
33 |
| - if (connections[bucket] == null) { |
34 |
| - connect(bucket); |
| 33 | + connection = connectionMap.get(carrierThread); |
| 34 | + if (connection == null) { |
| 35 | + connection = newConnection(); |
| 36 | + connectionMap.put(carrierThread, connection); |
35 | 37 | }
|
36 | 38 | } finally {
|
37 | 39 | lock.unlock();
|
38 | 40 | }
|
39 | 41 | }
|
40 |
| - return connections[bucket]; |
| 42 | + return connection; |
41 | 43 | }
|
42 | 44 |
|
43 | 45 | @Override
|
44 | 46 | public void close() {
|
45 | 47 | try {
|
46 |
| - for (PgClientConnection connection : connections) { |
| 48 | + for (PgClientConnection connection : connectionMap.values()) { |
47 | 49 | connection.close();
|
48 | 50 | }
|
49 | 51 | } catch (Exception e) {
|
50 | 52 | throw new RuntimeException(e);
|
51 | 53 | }
|
52 | 54 | }
|
53 | 55 |
|
54 |
| - private void connect(int bucket) { |
| 56 | + private PgClientConnection newConnection() { |
55 | 57 | try {
|
56 | 58 | PgConnection conn = PgConnection.connect(vertx, options)
|
57 | 59 | .toCompletionStage().toCompletableFuture().get();
|
58 | 60 | PgClientConnection clientConn = new PgClientConnection(conn);
|
59 | 61 | clientConn.prepare();
|
60 |
| - connections[bucket] = clientConn; |
| 62 | + return clientConn; |
61 | 63 | } catch (Exception e) {
|
62 | 64 | throw new RuntimeException(e);
|
63 | 65 | }
|
64 | 66 | }
|
65 | 67 |
|
| 68 | + static String carrierThread() { |
| 69 | + String threadName = Thread.currentThread().toString(); |
| 70 | + return threadName.substring(threadName.indexOf('@') + 1); |
| 71 | + } |
| 72 | + |
66 | 73 | public static class PgClientConnection implements AutoCloseable {
|
67 |
| - private static final int UPDATE_QUERIES = 500; |
| 74 | + static final int UPDATE_QUERIES = 500; |
68 | 75 | private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
|
69 | 76 | private static String SELECT_FORTUNE = "SELECT id, message from FORTUNE";
|
70 | 77 |
|
|
0 commit comments