8000 Netty related code has been isolated and moved to tests as well as th… · LarsG/postgres-async-driver@2463b93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2463b93

Browse files
Netty related code has been isolated and moved to tests as well as the dependency on Netty library
1 parent 9f12f6b commit 2463b93

13 files changed

+85
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Postgres-async-driver is a non-blocking Java driver for PostgreSQL. The driver s
66

77
## Download
88

9-
Postgres-async-driver is available on [Maven Central](http://search.maven.org/#search|ga|1|g%3A%22com.github.alaisi.pgasync%22).
9+
Postgres-async-driver is available on [Maven Central](http://search.maven.org/#search|ga|1|g%3A%22com.github.alaisi.pgasync).
1010

1111
```xml
1212
<dependency>

pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@
6363
</build>
6464

6565
<dependencies>
66-
<dependency>
67-
<groupId>io.netty</groupId>
68-
<artifactId>netty-handler</artifactId>
69-
<version>4.1.33.Final</version>
70-
</dependency>
7166
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
7267
<dependency>
7368
<groupId>javax.xml.bind</groupId>
@@ -82,6 +77,12 @@
8277
<scope>provided</scope>
8378
</dependency>
8479

80+
<dependency>
81+
<groupId>io.netty</groupId>
82+
<artifactId>netty-handler</artifactId>
83+
<version>4.1.33.Final</version>
84+
<scope>test</scope>
85+
</dependency>
8586
<dependency>
8687
<groupId>junit</groupId>
8788
<artifactId>junit</artifactId>

src/main/java/com/github/pgasync/PgConnectible.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.github.pgasync;
22

33
import com.github.pgasync.conversion.DataConverter;
4+
import com.pgasync.ConnectibleBuilder;
45
import com.pgasync.Connection;
5-
import com.pgasync.NettyConnectibleBuilder;
66
import com.pgasync.Connectible;
77
import com.pgasync.Row;
88
import com.pgasync.Transaction;
@@ -27,7 +27,7 @@ public abstract class PgConnectible implements Connectible {
2727
protected final String database;
2828
protected final Charset encoding;
2929

30-
PgConnectible(NettyConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> toStream, Executor futuresExecutor) {
30+
PgConnectible(ConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> toStream, Executor futuresExecutor) {
3131
this.username = properties.getUsername();
3232
this.password = properties.getPassword();
3333
this.database = properties.getDatabase();

src/main/java/com/github/pgasync/PgConnectionPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
package com.github.pgasync;
1616

17+
import com.pgasync.ConnectibleBuilder;
1718
import com.pgasync.Connection;
1819
import com.pgasync.Listening;
1920
import com.pgasync.PreparedStatement;
2021
import com.pgasync.Row;
2122
import com.pgasync.SqlException;
22-
import com.pgasync.NettyConnectibleBuilder;
2323
import com.pgasync.ResultSet;
2424
import com.pgasync.Transaction;
2525

@@ -254,7 +254,7 @@ public CompletableFuture<Integer> fetch(BiConsumer<Map<String, PgColumn>, PgColu
254254
@GuardedBy("lock")
255255
private final Queue<PooledPgConnection> connections = new LinkedList<>();
256256

257-
public PgConnectionPool(NettyConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> addressToStream, Executor futuresExecutor) {
257+
public PgConnectionPool(ConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> addressToStream, Executor futuresExecutor) {
258258
super(properties, addressToStream, futuresExecutor);
259259
this.maxConnections = properties.getMaxConnections();
260260
this.maxStatements = properties.getMaxStatements();

src/main/java/com/github/pgasync/PgDatabase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.github.pgasync;
22

3+
import com.pgasync.ConnectibleBuilder;
34
import com.pgasync.Connection;
4-
import com.pgasync.NettyConnectibleBuilder;
55

66
import java.util.concurrent.CompletableFuture;
77
import java.util.concurrent.Executor;
88
import java.util.function.Function;
99

1010
public class PgDatabase extends PgConnectible {
1111

12-
public PgDatabase(NettyConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> toStream, Executor futuresExecutor) {
12+
public PgDatabase(ConnectibleBuilder.ConnectibleProperties properties, Function<Executor, ProtocolStream> toStream, Executor futuresExecutor) {
1313
super(properties, toStream, futuresExecutor);
1414
}
1515

src/main/java/com/github/pgasync/PgProtocolStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import java.util.logging.Logger;
4747

4848
/**
49-
* Netty messages stream to Postgres backend.
49+
* Messages stream to Postgres backend.
5050
*
5151
* @author Marat Gainullin
5252
*/

src/main/java/com/pgasync/NettyConnectibleBuilder.java renamed to src/main/java/com/pgasync/ConnectibleBuilder.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
import com.github.pgasync.PgDatabase;
1919
import com.github.pgasync.ProtocolStream;
2020
import com.github.pgasync.conversion.DataConverter;
21-
import com.github.pgasync.netty.NettyProtocolStream;
22-
import io.netty.channel.EventLoopGroup;
23-
import io.netty.channel.nio.NioEventLoopGroup;
2421

25-
import java.net.InetSocketAddress;
2622
import java.nio.charset.Charset;
2723
import java.util.ArrayList;
2824
import java.util.Collections;
@@ -36,22 +32,11 @@
3632
* @author Antti Laisi
3733
* @author Marat Gainullin
3834
*/
39-
public class NettyConnectibleBuilder {
35+
public abstract class ConnectibleBuilder {
4036

41-
private final ConnectibleProperties properties = new ConnectibleProperties();
42-
// TODO: refactor when Netty will support more advanced threading model
43-
//new NioEventLoopGroup(0/*Netty defaults will be used*/, futuresExecutor),
44-
private static final EventLoopGroup group = new NioEventLoopGroup();
37+
protected final ConnectibleProperties properties = new ConnectibleProperties();
4538

46-
private ProtocolStream newProtocolStream(Executor futuresExecutor) {
47-
return new NettyProtocolStream(
48-
group,
49-
new InetSocketAddress(properties.getHostname(), properties.getPort()),
50-
properties.getUseSsl(),
51-
Charset.forName(properties.getEncoding()),
52-
futuresExecutor
53-
);
54-
}
39+
F42D protected abstract ProtocolStream newProtocolStream(Executor futuresExecutor);
5540

5641
/**
5742
* @return Pool ready for use
@@ -76,62 +61,62 @@ public Connectible plain() {
7661
return plain(ForkJoinPool.commonPool());
7762
}
7863

79-
public NettyConnectibleBuilder hostname(String hostname) {
64+
public ConnectibleBuilder hostname(String hostname) {
8065
properties.hostname = hostname;
8166
return this;
8267
}
8368

84-
public NettyConnectibleBuilder port(int port) {
69+
public ConnectibleBuilder port(int port) {
8570
properties.port = port;
8671
return this;
8772
}
8873

89-
public NettyConnectibleBuilder username(String username) {
74+
public ConnectibleBuilder username(String username) {
9075
properties.username = username;
9176
return this;
9277
}
9378

94-
public NettyConnectibleBuilder password(String password) {
79+
public ConnectibleBuilder password(String password) {
9580
properties.password = password;
9681
return this;
9782
}
9883

99-
public NettyConnectibleBuilder database(String database) {
84+
public ConnectibleBuilder database(String database) {
10085
properties.database = database;
10186
return this;
10287
}
10388

104-
public NettyConnectibleBuilder maxConnections(int maxConnections) {
89+
public ConnectibleBuilder maxConnections(int maxConnections) {
10590
properties.maxConnections = maxConnections;
10691
return this;
10792
}
10893

109-
public NettyConnectibleBuilder maxStatements(int maxStatements) {
94+
public ConnectibleBuilder maxStatements 179B (int maxStatements) {
11095
properties.maxStatements = maxStatements;
11196
return this;
11297
}
11398

114-
public NettyConnectibleBuilder converters(Converter<?>... converters) {
99+
public ConnectibleBuilder converters(Converter<?>... converters) {
115100
Collections.addAll(properties.converters, converters);
116101
return this;
117102
}
118103

119-
public NettyConnectibleBuilder dataConverter(DataConverter dataConverter) {
104+
public ConnectibleBuilder dataConverter(DataConverter dataConverter) {
120105
properties.dataConverter = dataConverter;
121106
return this;
122107
}
123108

124-
public NettyConnectibleBuilder ssl(boolean ssl) {
109+
public ConnectibleBuilder ssl(boolean ssl) {
125110
properties.useSsl = ssl;
126111
return this;
127112
}
128113

129-
public NettyConnectibleBuilder validationQuery(String validationQuery) {
114+
public ConnectibleBuilder validationQuery(String validationQuery) {
130115
properties.validationQuery = validationQuery;
131116
return this;
132117
}
133118

134-
public NettyConnectibleBuilder encoding(String value) {
119+
public ConnectibleBuilder encoding(String value) {
135120
properties.encoding = value;
136121
return this;
137122
}

src/test/java/com/github/pgasync/DatabaseRule.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import static java.lang.System.getenv;
44

5-
import com.pgasync.NettyConnectibleBuilder;
5+
import com.github.pgasync.netty.NettyConnectibleBuilder;
66
import com.pgasync.Connectible;
7+
import com.pgasync.ConnectibleBuilder;
78
import com.pgasync.ResultSet;
89
import org.junit.rules.ExternalResource;
910

@@ -27,20 +28,20 @@ class DatabaseRule extends ExternalResource {
2728

2829
private static PostgresProcess process;
2930

30-
final NettyConnectibleBuilder builder;
31+
final ConnectibleBuilder builder;
3132
Connectible pool;
3233

3334
DatabaseRule() {
3435
this(createPoolBuilder(1));
3536
}
3637

37-
DatabaseRule(NettyConnectibleBuilder builder) {
38+
DatabaseRule(ConnectibleBuilder builder) {
3839
this.builder = builder;
3940
if (builder instanceof EmbeddedConnectionPoolBuilder) {
4041
String port = System.getProperty("asyncpg.test.postgres.port");
4142
if (port != null && !port.isBlank()) {
4243
builder.hostname("localhost");
43-
builder.port(Integer.valueOf(port));
44+
builder.port(Integer.parseInt(port));
4445
} else {
4546
if (process == null) {
4647
try {
@@ -123,7 +124,7 @@ static Connectible createPool(int size) {
123124
return createPoolBuilder(size).pool();
124125
}
125126

126-
static NettyConnectibleBuilder createPoolBuilder(int size) {
127+
static ConnectibleBuilder createPoolBuilder(int size) {
127128
String db = getenv("PG_DATABASE");
128129
String user = getenv("PG_USERNAME");
129130
String pass = getenv("PG_PASSWORD");

src/test/java/com/github/pgasync/PipelineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private Connection getConnection() throws InterruptedException {
9898
public void messageStreamEnsuresSequentialAccess() throws Exception {
9999
Connection connection = getConnection();
100100
try {
101-
CompletableFuture.allOf((CompletableFuture<?>[]) IntStream.range(0, 10).mapToObj(i -> connection.completeQuery("select " + i + ", pg_sleep(" + 10 + ")")
101+
CompletableFuture.allOf(IntStream.range(0, 10).mapToObj(i -> connection.completeQuery("select " + i + ", pg_sleep(" + 10 + ")")
102102
.exceptionally(th -> {
103103
throw new IllegalStateException(new SqlException(th));
104104
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.github.pgasync.netty;
16+
17+
import com.github.pgasync.ProtocolStream;
18+
import com.pgasync.Connectible;
19+
import com.pgasync.ConnectibleBuilder;
20+
import io.netty.channel.EventLoopGroup;
21+
import io.netty.channel.nio.NioEventLoopGroup;
22+
23+
import java.net.InetSocketAddress;
24+
import java.nio.charset.Charset;
25+
import java.util.concurrent.Executor;
26+
27+
/**
28+
* Builder for creating {@link Connectible} instances.
29+
*
30+
* @author Antti Laisi
31+
* @author Marat Gainullin
32+
*/
33+
public class NettyConnectibleBuilder extends ConnectibleBuilder {
34+
35+
// TODO: refactor when Netty will support more advanced threading model
36+
//new NioEventLoopGroup(0/*Netty defaults will be used*/, futuresExecutor),
37+
private static final EventLoopGroup group = new NioEventLoopGroup();
38+
39+
protected ProtocolStream newProtocolStream(Executor futuresExecutor) {
40+
return new NettyProtocolStream(
41+
group,
42+
new InetSocketAddress(properties.getHostname(), properties.getPort()),
43+
properties.getUseSsl(),
44+
Charset.forName(properties.getEncoding()),
45+
futuresExecutor
46+
);
47+
}
48+
}

0 commit comments

Comments
 (0)
0