Skip to content

Commit 5378b39

Browse files
committed
pr comment
1 parent baf2d43 commit 5378b39

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public class LoggingHandler extends Handler {
125125
// https://github.com/GoogleCloudPlatform/google-cloud-java/issues/1740 .
126126
private final Level baseLevel;
127127

128-
private final Lock writeLock = new ReentrantLock();
128+
private final Object writeLock = new Object();
129129
private final Set<ApiFuture<Void>> pendingWrites =
130130
Collections.newSetFromMap(new IdentityHashMap<ApiFuture<Void>, Boolean>());
131131

@@ -473,25 +473,23 @@ void write(LogEntry entry, WriteOption... options) {
473473
case ASYNC:
474474
default:
475475
final ApiFuture<Void> writeFuture = getLogging().writeAsync(entryList, options);
476-
writeLock.lock();
477-
try {
476+
synchronized(writeLock) {
478477
pendingWrites.add(writeFuture);
479-
} finally {
480-
writeLock.unlock();
481478
}
482479
ApiFutures.addCallback(
483480
writeFuture,
484481
new ApiFutureCallback<Void>() {
485-
@Override
486-
public void onSuccess(Void v) {
487-
writeLock.lock();
488-
try {
482+
private void removeFromPending() {
483+
synchronized(writeLock) {
489484
pendingWrites.remove(writeFuture);
490-
} finally {
491-
writeLock.unlock();
492485
}
493486
}
494487

488+
@Override
489+
public void onSuccess(Void v) {
490+
removeFromPending();
491+
}
492+
495493
@Override
496494
public void onFailure(Throwable t) {
497495
try {
@@ -501,7 +499,7 @@ public void onFailure(Throwable t) {
501499
reportError(null, new Exception(t), ErrorManager.FLUSH_FAILURE);
502500
}
503501
} finally {
504-
onSuccess(null);
502+
removeFromPending();
505503
}
506504
}
507505
});
@@ -511,16 +509,14 @@ public void onFailure(Throwable t) {
511509

512510
@Override
513511
public void flush() {
514-
// BUG(1795): flush is broken, need support from batching implementation.
512+
// BUG(1795): We should force batcher to issue RPC call for buffered messages,
513+
// so the code below doesn't wait uselessly.
515514

516-
ArrayList<ApiFuture<Void>> writes = new ArrayList<>();
517-
writeLock.lock();
518-
try {
519-
writes.addAll(pendingWrites);
520-
} finally {
521-
writeLock.unlock();
515+
ArrayList<ApiFuture<Void>> writesToFlush = new ArrayList<>();
516+
synchronized(writeLock) {
517+
writesToFlush.addAll(pendingWrites);
522518
}
523-
for (ApiFuture<Void> write : writes) {
519+
for (ApiFuture<Void> write : writesToFlush) {
524520
try {
525521
Uninterruptibles.getUninterruptibly(write);
526522
} catch (Exception e) {

0 commit comments

Comments
 (0)