Skip to content

Commit 80e304f

Browse files
Adding .close() to Firestore Service (googleapis#2807)
1 parent 5475888 commit 80e304f

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Firestore.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.annotation.Nonnull;
2323

2424
/** Represents a Firestore Database and is the entry point for all Firestore operations */
25-
public interface Firestore extends Service<FirestoreOptions> {
25+
public interface Firestore extends Service<FirestoreOptions>, AutoCloseable {
2626

2727
/**
2828
* Gets a {@link CollectionReference} that refers to the collection at the specified path.
@@ -91,4 +91,12 @@ <T> ApiFuture<T> runTransaction(
9191
*/
9292
@Nonnull
9393
WriteBatch batch();
94+
95+
/**
96+
* Closes the gRPC channels associated with this instance and frees up their resources. This
97+
* method blocks until all channels are closed. Once this method is called, this Firestore client
98+
* is no longer usable.
99+
*/
100+
@Override
101+
void close() throws Exception;
94102
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,12 @@ class FirestoreImpl implements Firestore {
5252
private static final String AUTO_ID_ALPHABET =
5353
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
5454

55-
/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
56-
static String autoId() {
57-
StringBuilder builder = new StringBuilder();
58-
int maxRandom = AUTO_ID_ALPHABET.length();
59-
for (int i = 0; i < AUTO_ID_LENGTH; i++) {
60-
builder.append(AUTO_ID_ALPHABET.charAt(RANDOM.nextInt(maxRandom)));
61-
}
62-
return builder.toString();
63-
}
64-
6555
private final FirestoreRpc firestoreClient;
6656
private final FirestoreOptions firestoreOptions;
6757
private final ResourcePath databasePath;
6858

59+
private boolean closed;
60+
6961
FirestoreImpl(FirestoreOptions options) {
7062
this(options, options.getFirestoreRpc());
7163
}
@@ -81,6 +73,16 @@ static String autoId() {
8173
ResourcePath.create(DatabaseRootName.of(options.getProjectId(), options.getDatabaseId()));
8274
}
8375

76+
/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
77+
static String autoId() {
78+
StringBuilder builder = new StringBuilder();
79+
int maxRandom = AUTO_ID_ALPHABET.length();
80+
for (int i = 0; i < AUTO_ID_LENGTH; i++) {
81+
builder.append(AUTO_ID_ALPHABET.charAt(RANDOM.nextInt(maxRandom)));
82+
}
83+
return builder.toString();
84+
}
85+
8486
@Nonnull
8587
@Override
8688
public WriteBatch batch() {
@@ -324,6 +326,7 @@ FirestoreRpc getClient() {
324326
/** Request funnel for all read/write requests. */
325327
<RequestT, ResponseT> ApiFuture<ResponseT> sendRequest(
326328
RequestT requestT, UnaryCallable<RequestT, ResponseT> callable) {
329+
Preconditions.checkState(!closed, "Firestore client has already been closed");
327330
return callable.futureCall(requestT);
328331
}
329332

@@ -332,18 +335,26 @@ <RequestT, ResponseT> void streamRequest(
332335
RequestT requestT,
333336
ApiStreamObserver<ResponseT> responseObserverT,
334337
ServerStreamingCallable<RequestT, ResponseT> callable) {
338+
Preconditions.checkState(!closed, "Firestore client has already been closed");
335339
callable.serverStreamingCall(requestT, responseObserverT);
336340
}
337341

338342
/** Request funnel for all bidirectional streaming requests. */
339343
<RequestT, ResponseT> ApiStreamObserver<RequestT> streamRequest(
340344
ApiStreamObserver<ResponseT> responseObserverT,
341345
BidiStreamingCallable<RequestT, ResponseT> callable) {
346+
Preconditions.checkState(!closed, "Firestore client has already been closed");
342347
return callable.bidiStreamingCall(responseObserverT);
343348
}
344349

345350
@Override
346351
public FirestoreOptions getOptions() {
347352
return firestoreOptions;
348353
}
354+
355+
@Override
356+
public void close() throws Exception {
357+
firestoreClient.close();
358+
closed = true;
359+
}
349360
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.concurrent.Semaphore;
6363
import java.util.concurrent.atomic.AtomicInteger;
6464
import javax.annotation.Nullable;
65+
import org.junit.After;
6566
import org.junit.Before;
6667
import org.junit.Rule;
6768
import org.junit.Test;
@@ -92,6 +93,11 @@ public void before() {
9293
randomDoc = randomColl.document();
9394
}
9495

96+
@After
97+
public void after() throws Exception {
98+
firestore.close();
99+
}
100+
95101
private DocumentReference addDocument(String key, Object value, Object... fields)
96102
throws Exception {
97103
DocumentReference documentReference = randomColl.document();

0 commit comments

Comments
 (0)