Skip to content

Commit fd07e9e

Browse files
committed
Pub/Sub: publishAll defers sending batches.
1 parent 42697e2 commit fd07e9e

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

  • google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,22 @@ public void run() {
313313
*/
314314
public void publishAllOutstanding() {
315315
messagesBatchLock.lock();
316+
List<MessagesBatch> toSend;
316317
try {
317-
for (MessagesBatch batch : messagesBatches.values()) {
318-
if (!batch.isEmpty()) {
319-
// TODO(kimkyung-goog): Do not release `messagesBatchLock` when publishing a batch. If
320-
// it's released, the order of publishing cannot be guaranteed if `publish()` is called
321-
// while this function is running. This locking mechanism needs to be improved if it
322-
// causes any performance degradation.
323-
publishOutstandingBatch(batch.popOutstandingBatch());
324-
}
325-
}
318+
toSend = new ArrayList<>(messagesBatches.values());
326319
messagesBatches.clear();
327320
} finally {
328321
messagesBatchLock.unlock();
329322
}
323+
for (MessagesBatch batch : toSend) {
324+
if (!batch.isEmpty()) {
325+
// TODO(kimkyung-goog): Do not release `messagesBatchLock` when publishing a batch. If
326+
// it's released, the order of publishing cannot be guaranteed if `publish()` is called
327+
// while this function is running. This locking mechanism needs to be improved if it
328+
// causes any performance degradation.
329+
publishOutstandingBatch(batch.popOutstandingBatch());
330+
}
331+
}
330332
}
331333

332334
private ApiFuture<PublishResponse> publishCall(OutstandingBatch outstandingBatch) {

0 commit comments

Comments
 (0)