forked from TechEmpower/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jooby: update reactive db tests (TechEmpower#5854)
- Upgrade reactive driver - Use a thread-local pgpool - jooby 2.8.9
- Loading branch information
Showing
4 changed files
with
83 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 30 additions & 20 deletions
50
frameworks/Java/jooby/src/main/java/com/techempower/PgClients.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,42 @@ | ||
package com.techempower; | ||
|
||
import io.reactiverse.pgclient.PgClient; | ||
import io.reactiverse.pgclient.PgPool; | ||
import io.reactiverse.pgclient.PgPoolOptions; | ||
import com.typesafe.config.Config; | ||
import io.vertx.core.Vertx; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import io.vertx.core.VertxOptions; | ||
import io.vertx.pgclient.PgConnectOptions; | ||
import io.vertx.pgclient.PgPool; | ||
import io.vertx.sqlclient.PoolOptions; | ||
|
||
public class PgClients { | ||
private final Iterator<PgPool> iterator; | ||
private static final PoolOptions SINGLE = new PoolOptions().setMaxSize(1); | ||
|
||
private final PgConnectOptions connectOptions; | ||
|
||
private final Vertx vertx; | ||
|
||
private ThreadLocal<PgPool> sqlClient = ThreadLocal.withInitial(this::sqlClientPool); | ||
|
||
public PgClients(Config config) { | ||
this.vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true).setWorkerPoolSize(4)); | ||
this.connectOptions = pgPoolOptions(config); | ||
} | ||
|
||
private PgClients(Collection<PgPool> clients) { | ||
iterator = Stream.generate(() -> clients).flatMap(Collection::stream).iterator(); | ||
public PgPool next() { | ||
return sqlClient.get(); | ||
} | ||
|
||
public synchronized PgPool next() { | ||
return iterator.next(); | ||
private PgConnectOptions pgPoolOptions(Config config) { | ||
PgConnectOptions options = new PgConnectOptions(); | ||
options.setDatabase(config.getString("databaseName")); | ||
options.setHost(config.getString("serverName")); | ||
options.setPort(config.getInt("portNumber")); | ||
options.setUser(config.getString("user")); | ||
options.setPassword(config.getString("password")); | ||
options.setCachePreparedStatements(true); | ||
return options; | ||
} | ||
|
||
public static PgClients create(Vertx vertx, PgPoolOptions options) { | ||
List<PgPool> clients = new ArrayList<>(); | ||
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) { | ||
clients.add(PgClient.pool(vertx, options)); | ||
} | ||
return new PgClients(clients); | ||
private PgPool sqlClientPool() { | ||
return PgPool.pool(vertx, connectOptions, SINGLE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters