Skip to content

Commit cef6cd0

Browse files
sduskischingor13
authored andcommitted
PubSub: refactor alarm setup into a single method (googleapis#5008)
Moving some alarm setup logic from `publish()` and some logic from `setupDurationBasedPublishAlarm` into a new method called `setupAlarm`
1 parent 7f889b2 commit cef6cd0

File tree

1 file changed

+24
-24
lines changed
  • google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1

1 file changed

+24
-24
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,7 @@ && hasBatchingBytes()
222222
}
223223
}
224224
// Setup the next duration based delivery alarm if there are messages batched.
225-
if (!messagesBatch.isEmpty()) {
226-
setupDurationBasedPublishAlarm();
227-
} else if (currentAlarmFuture != null) {
228-
logger.log(Level.FINER, "Cancelling alarm, no more messages");
229-
if (activeAlarm.getAndSet(false)) {
230-
currentAlarmFuture.cancel(false);
231-
}
232-
}
225+
setupAlarm();
233226
} finally {
234227
messagesBatchLock.unlock();
235228
}
@@ -266,22 +259,29 @@ public void run() {
266259
return publishResult;
267260
}
268261

269-
private void setupDurationBasedPublishAlarm() {
270-
if (!activeAlarm.getAndSet(true)) {
271-
long delayThresholdMs = getBatchingSettings().getDelayThreshold().toMillis();
272-
logger.log(Level.FINER, "Setting up alarm for the next {0} ms.", delayThresholdMs);
273-
currentAlarmFuture =
274-
executor.schedule(
275-
new Runnable() {
276-
@Override
277-
public void run() {
278-
logger.log(Level.FINER, "Sending messages based on schedule.");
279-
activeAlarm.getAndSet(false);
280-
publishAllOutstanding();
281-
}
282-
},
283-
delayThresholdMs,
284-
TimeUnit.MILLISECONDS);
262+
private void setupAlarm() {
263+
if (!messagesBatch.isEmpty()) {
264+
if (!activeAlarm.getAndSet(true)) {
265+
long delayThresholdMs = getBatchingSettings().getDelayThreshold().toMillis();
266+
logger.log(Level.FINER, "Setting up alarm for the next {0} ms.", delayThresholdMs);
267+
currentAlarmFuture =
268+
executor.schedule(
269+
new Runnable() {
270+
@Override
271+
public void run() {
272+
logger.log(Level.FINER, "Sending messages based on schedule.");
273+
activeAlarm.getAndSet(false);
274+
publishAllOutstanding();
275+
}
276+
},
277+
delayThresholdMs,
278+
TimeUnit.MILLISECONDS);
279+
}
280+
} else if (currentAlarmFuture != null) {
281+
logger.log(Level.FINER, "Cancelling alarm, no more messages");
282+
if (activeAlarm.getAndSet(false)) {
283+
currentAlarmFuture.cancel(false);
284+
}
285285
}
286286
}
287287

0 commit comments

Comments
 (0)