Skip to content

Commit 7a74047

Browse files
committed
More code clean up and removing tubesock package
1 parent 7e7f694 commit 7a74047

22 files changed

+71
-1686
lines changed

src/main/java/com/google/firebase/database/FirebaseDatabase.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.google.firebase.internal.FirebaseService;
3434

3535
import com.google.firebase.internal.SdkUtils;
36+
import io.netty.util.concurrent.FastThreadLocal;
37+
3638
import java.util.Collections;
3739
import java.util.HashMap;
3840
import java.util.Map;
@@ -358,12 +360,8 @@ void destroy() {
358360
if (destroyed.get()) {
359361
return;
360362
}
361-
362-
if (repo != null) {
363-
RepoManager.interrupt(repo);
364-
repo = null;
365-
}
366-
RepoManager.interrupt(getConfig());
363+
RepoManager.destroy(getConfig());
364+
FastThreadLocal.removeAll();
367365
destroyed.compareAndSet(false, true);
368366
}
369367
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package com.google.firebase.database.connection;
1818

1919
import com.google.firebase.database.logging.Logger;
20-
import com.google.firebase.database.tubesock.ThreadConfig;
2120

2221
import java.util.concurrent.ScheduledExecutorService;
22+
import java.util.concurrent.ThreadFactory;
2323

2424
public class ConnectionContext {
2525

@@ -29,7 +29,7 @@ public class ConnectionContext {
2929
private final boolean persistenceEnabled;
3030
private final String clientSdkVersion;
3131
private final String userAgent;
32-
private final ThreadConfig threadConfig;
32+
private final ThreadFactory threadFactory;
3333

3434
public ConnectionContext(
3535
Logger logger,
@@ -38,14 +38,14 @@ public ConnectionContext(
3838
boolean persistenceEnabled,
3939
String clientSdkVersion,
4040
String userAgent,
41-
ThreadConfig threadConfig) {
41+
ThreadFactory threadFactory) {
4242
this.logger = logger;
4343
this.authTokenProvider = authTokenProvider;
4444
this.executorService = executorService;
4545
this.persistenceEnabled = persistenceEnabled;
4646
this.clientSdkVersion = clientSdkVersion;
4747
this.userAgent = userAgent;
48-
this.threadConfig = threadConfig;
48+
this.threadFactory = threadFactory;
4949
}
5050

5151
public Logger getLogger() {
@@ -72,7 +72,7 @@ public String getUserAgent() {
7272
return this.userAgent;
7373
}
7474

75-
public ThreadConfig getThreadConfig() {
76-
return threadConfig;
75+
public ThreadFactory getThreadFactory() {
76+
return threadFactory;
7777
}
7878
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ public void onDisconnect(Connection.DisconnectReason reason) {
284284
this.realtime = null;
285285
this.hasOnDisconnects = false;
286286
requestCBHash.clear();
287+
if (inactivityTimer != null) {
288+
logger.debug("cancelling idle time checker");
289+
inactivityTimer.cancel(false);
290+
inactivityTimer = null;
291+
}
287292
cancelSentTransactions();
288293
if (shouldReconnect()) {
289294
long timeSinceLastConnectSucceeded =

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void start() {
9898
// No-op in java
9999
}
100100

101-
public void close() {
101+
void close() {
102102
if (logger.logsDebug()) {
103103
logger.debug("websocket is being closed");
104104
}
@@ -111,6 +111,7 @@ public void close() {
111111
}
112112
if (keepAlive != null) {
113113
keepAlive.cancel(true);
114+
keepAlive = null;
114115
}
115116
}
116117

@@ -304,13 +305,15 @@ public void onClose() {
304305
if (logger.logsDebug()) {
305306
logger.debug("closed");
306307
}
307-
executorService.execute(
308-
new Runnable() {
309-
@Override
310-
public void run() {
311-
onClosed();
312-
}
313-
});
308+
if (!isClosed) {
309+
executorService.execute(
310+
new Runnable() {
311+
@Override
312+
public void run() {
313+
onClosed();
314+
}
315+
});
316+
}
314317
}
315318

316319
@Override

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

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.firebase.database.core.persistence.PersistenceManager;
2929
import com.google.firebase.database.logging.LogWrapper;
3030
import com.google.firebase.database.logging.Logger;
31-
import com.google.firebase.database.tubesock.ThreadConfig;
3231
import com.google.firebase.database.utilities.DefaultRunLoop;
3332

3433
import java.util.List;
@@ -39,17 +38,18 @@ public class Context {
3938
private static final long DEFAULT_CACHE_SIZE = 10 * 1024 * 1024;
4039

4140
protected Logger logger;
42-
protected EventTarget eventTarget;
43-
protected AuthTokenProvider authTokenProvider;
44-
protected RunLoop runLoop;
45-
protected String persistenceKey;
46-
protected List<String> loggedComponents;
47-
protected String userAgent;
48-
protected Logger.Level logLevel = Logger.Level.INFO;
49-
protected boolean persistenceEnabled;
50-
protected long cacheSize = DEFAULT_CACHE_SIZE;
51-
protected FirebaseApp firebaseApp;
52-
private PersistenceManager forcedPersistenceManager;
41+
42+
EventTarget eventTarget;
43+
AuthTokenProvider authTokenProvider;
44+
RunLoop runLoop;
45+
String persistenceKey;
46+
List<String> loggedComponents;
47+
Logger.Level logLevel = Logger.Level.INFO;
48+
boolean persistenceEnabled;
49+
long cacheSize = DEFAULT_CACHE_SIZE;
50+
FirebaseApp firebaseApp;
51+
52+
private String userAgent;
5353
private boolean frozen = false;
5454
private boolean stopped = false;
5555

@@ -88,10 +88,6 @@ private Platform getPlatform() {
8888
return platform;
8989
}
9090

91-
public boolean isFrozen() {
92-
return frozen;
93-
}
94-
9591
public boolean isStopped() {
9692
return stopped;
9793
}
@@ -103,7 +99,7 @@ synchronized void freeze() {
10399
}
104100
}
105101

106-
public void requireStarted() {
102+
void requireStarted() {
107103
if (stopped) {
108104
restartServices();
109105
stopped = false;
@@ -130,12 +126,16 @@ private void restartServices() {
130126

131127
void stop() {
132128
stopped = true;
133-
eventTarget.shutdown();
134-
runLoop.shutdown();
129+
if (eventTarget != null) {
130+
eventTarget.shutdown();
131+
}
132+
if (runLoop != null) {
133+
runLoop.shutdown();
134+
}
135135
}
136136

137-
protected void assertUnfrozen() {
138-
if (isFrozen()) {
137+
void assertUnfrozen() {
138+
if (frozen) {
139139
throw new DatabaseException(
140140
"Modifications to DatabaseConfig objects must occur before they are in use");
141141
}
@@ -153,27 +153,19 @@ public LogWrapper getLogger(Class component, String prefix) {
153153
return new LogWrapper(logger, component, prefix);
154154
}
155155

156-
private ThreadConfig getThreadConfig() {
157-
return new ThreadConfig(ImplFirebaseTrampolines.getThreadFactory(firebaseApp),
158-
getPlatform().getThreadInitializer());
159-
}
160-
161-
public ConnectionContext getConnectionContext() {
156+
ConnectionContext getConnectionContext() {
162157
return new ConnectionContext(
163158
this.logger,
164159
wrapAuthTokenProvider(this.getAuthTokenProvider()),
165160
this.getExecutorService(),
166161
this.isPersistenceEnabled(),
167162
FirebaseDatabase.getSdkVersion(),
168163
this.getUserAgent(),
169-
this.getThreadConfig());
164+
ImplFirebaseTrampolines.getThreadFactory(firebaseApp));
170165
}
171166

172167
PersistenceManager getPersistenceManager(String firebaseId) {
173168
// TODO[persistence]: Create this once and store it.
174-
if (forcedPersistenceManager != null) {
175-
return forcedPersistenceManager;
176-
}
177169
if (this.persistenceEnabled) {
178170
PersistenceManager cache = platform.createPersistenceManager(this, firebaseId);
179171
if (cache == null) {
@@ -187,19 +179,14 @@ PersistenceManager getPersistenceManager(String firebaseId) {
187179
}
188180
}
189181

190-
public boolean isPersistenceEnabled() {
182+
boolean isPersistenceEnabled() {
191183
return this.persistenceEnabled;
192184
}
193185

194186
public long getPersistenceCacheSizeBytes() {
195187
return this.cacheSize;
196188
}
197189

198-
// For testing
199-
void forcePersistenceManager(PersistenceManager persistenceManager) {
200-
this.forcedPersistenceManager = persistenceManager;
201-
}
202-
203190
public EventTarget getEventTarget() {
204191
return eventTarget;
205192
}
@@ -208,23 +195,15 @@ public RunLoop getRunLoop() {
208195
return runLoop;
209196
}
210197

211-
public String getUserAgent() {
198+
String getUserAgent() {
212199
return userAgent;
213200
}
214201

215-
public String getPlatformVersion() {
216-
return getPlatform().getPlatformVersion();
217-
}
218-
219-
public String getSessionPersistenceKey() {
220-
return this.persistenceKey;
221-
}
222-
223202
public AuthTokenProvider getAuthTokenProvider() {
224203
return this.authTokenProvider;
225204
}
226205

227-
public PersistentConnection newPersistentConnection(
206+
PersistentConnection newPersistentConnection(
228207
HostInfo info, PersistentConnection.Delegate delegate) {
229208
return getPlatform().newPersistentConnection(this, this.getConnectionContext(), info, delegate);
230209
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public static Repo createRepo(Context ctx, RepoInfo info, FirebaseDatabase datab
4949
return instance.createLocalRepo(ctx, info, database);
5050
}
5151

52+
public static void destroy(Context ctx) {
53+
try {
54+
instance.destroyInternal(ctx);
55+
} finally {
56+
ctx.stop();
57+
}
58+
}
59+
5260
public static void interrupt(Context ctx) {
5361
instance.interruptInternal(ctx);
5462
}
@@ -134,6 +142,16 @@ public void run() {
134142
}
135143
}
136144

145+
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();
150+
}
151+
}
152+
}
153+
}
154+
137155
private void resumeInternal(final Context ctx) {
138156
RunLoop runLoop = ctx.getRunLoop();
139157
if (runLoop != null) {

0 commit comments

Comments
 (0)