Skip to content

Commit 8e90610

Browse files
committed
Using the SDK ThreadManager for DB websocket threads
1 parent 7a74047 commit 8e90610

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

src/main/java/com/google/firebase/database/connection/NettyWebSocketClient.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44
import static com.google.common.base.Preconditions.checkState;
55

6-
import com.google.common.util.concurrent.ThreadFactoryBuilder;
6+
import com.google.firebase.internal.GaeThreadFactory;
7+
import com.google.firebase.internal.RevivingScheduledExecutor;
78
import io.netty.bootstrap.Bootstrap;
89
import io.netty.channel.Channel;
910
import io.netty.channel.ChannelHandler;
@@ -32,6 +33,7 @@
3233
import io.netty.util.CharsetUtil;
3334

3435
import java.net.URI;
36+
import java.util.concurrent.ExecutorService;
3537
import java.util.concurrent.ThreadFactory;
3638
import javax.net.ssl.SSLException;
3739

@@ -40,12 +42,13 @@ class NettyWebSocketClient implements WebsocketConnection.WSClient {
4042
private final URI uri;
4143
private final SslContext sslContext;
4244
private final ChannelHandler channelHandler;
45+
private final ExecutorService executorService;
4346
private final EventLoopGroup group;
4447

4548
private Channel channel;
4649

4750
NettyWebSocketClient(
48-
URI uri, String userAgent,
51+
URI uri, String userAgent, ThreadFactory threadFactory,
4952
WebsocketConnection.WSClientEventHandler eventHandler) throws SSLException {
5053
this.uri = checkNotNull(uri);
5154
this.sslContext = SslContextBuilder.forClient()
@@ -56,11 +59,9 @@ class NettyWebSocketClient implements WebsocketConnection.WSClient {
5659
new DefaultHttpHeaders().add("User-Agent", userAgent));
5760
this.channelHandler = new WebSocketClientHandler(eventHandler, handshaker);
5861

59-
ThreadFactory factory = new ThreadFactoryBuilder()
60-
.setNameFormat("hkj-websocket-%d")
61-
.setDaemon(true)
62-
.build();
63-
this.group = new NioEventLoopGroup(1, factory);
62+
this.executorService = new RevivingScheduledExecutor(
63+
threadFactory, "firebase-websocket-worker", GaeThreadFactory.isAvailable());
64+
this.group = new NioEventLoopGroup(1, this.executorService);
6465
}
6566

6667
@Override
@@ -91,6 +92,7 @@ public void close() {
9192
channel.close();
9293
} finally {
9394
group.shutdownGracefully();
95+
executorService.shutdown();
9496
// TODO(hkj): https://github.com/netty/netty/issues/7310
9597
}
9698
}

src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,18 @@ public void merge(List<String> path, Map<String, Object> data, RequestResultCall
227227

228228
@Override
229229
public void purgeOutstandingWrites() {
230+
purgeOutstandingWrites("write_cancelled");
231+
}
232+
233+
private void purgeOutstandingWrites(String reason) {
230234
for (OutstandingPut put : this.outstandingPuts.values()) {
231235
if (put.onComplete != null) {
232-
put.onComplete.onRequestResult("write_canceled", null);
236+
put.onComplete.onRequestResult(reason, null);
233237
}
234238
}
235239
for (OutstandingDisconnect onDisconnect : this.onDisconnectRequestQueue) {
236240
if (onDisconnect.onComplete != null) {
237-
onDisconnect.onComplete.onRequestResult("write_canceled", null);
241+
onDisconnect.onComplete.onRequestResult(reason, null);
238242
}
239243
}
240244
this.outstandingPuts.clear();
@@ -311,13 +315,14 @@ public void onDisconnect(Connection.DisconnectReason reason) {
311315

312316
@Override
313317
public void onKill(String reason) {
314-
if (logger.logsDebug()) {
315-
logger.debug(
316-
"Firebase Database connection was forcefully killed by the server. Will not attempt "
317-
+ "reconnect. Reason: "
318-
+ reason);
319-
}
318+
// This usually represents a configuration error by the user (e.g. incorrect database URL).
319+
// Log the details as a warning, and purge outstanding writes so the error details bubble
320+
// up as exceptions.
321+
logger.warn(
322+
"Firebase Database connection was forcefully killed by the server. Will not attempt "
323+
+ "reconnect. Reason: " + reason);
320324
interrupt(SERVER_KILL_INTERRUPT_REASON);
325+
purgeOutstandingWrites("disconnected");
321326
}
322327

323328
@Override

src/main/java/com/google/firebase/database/connection/WebsocketConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private WSClient createConnection(
7272
host, hostInfo.isSecure(), hostInfo.getNamespace(), optLastSessionId);
7373
try {
7474
return new NettyWebSocketClient(
75-
uri, connectionContext.getUserAgent(), new WSClientHandlerImpl());
75+
uri, connectionContext.getUserAgent(), connectionContext.getThreadFactory(),
76+
new WSClientHandlerImpl());
7677
} catch (Exception e) {
7778
String msg = "Error while initializing websocket client";
7879
logger.error(msg, e);

src/main/java/com/google/firebase/database/core/RepoManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,22 @@ public void run() {
143143
}
144144

145145
private void destroyInternal(final Context ctx) {
146-
synchronized (repos) {
147-
if (repos.containsKey(ctx)) {
148-
for (Repo repo : repos.get(ctx).values()) {
149-
repo.interrupt();
146+
// RunLoop gets initialized before any Repo is created. Therefore we can assume that when
147+
// the RunLoop is not present, there's nothing to clean up.
148+
RunLoop runLoop = ctx.getRunLoop();
149+
if (runLoop != null) {
150+
runLoop.scheduleNow(new Runnable() {
151+
@Override
152+
public void run() {
153+
synchronized (repos) {
154+
if (repos.containsKey(ctx)) {
155+
for (Repo repo : repos.get(ctx).values()) {
156+
repo.interrupt();
157+
}
158+
}
159+
}
150160
}
151-
}
161+
});
152162
}
153163
}
154164

0 commit comments

Comments
 (0)