Skip to content

Commit 7328b32

Browse files
authored
---
yaml --- r: 34659 b: refs/heads/autosynth-texttospeech c: b1ebd2d h: refs/heads/master i: 34657: 4798212 34655: 6f9597e
1 parent 53c1325 commit 7328b32

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

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

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ refs/heads/autosynth-scheduler: a3de6480746d1cd586ca8b9d75c55a636f371539
140140
refs/heads/autosynth-spanner: d963fe4368e79cf6abae5d511785e8ced8ac57f4
141141
refs/heads/autosynth-speech: c563dcd420cce0a37c39b1b9c24be1b9ba604dc7
142142
refs/heads/autosynth-tasks: 25d1eafe8cb66b00e3dad765dac74a5b45b83e63
143-
refs/heads/autosynth-texttospeech: 79b64df4705b7a47895ec02aae4e84e5b8107884
143+
refs/heads/autosynth-texttospeech: b1ebd2d84384315f5788d1fa9f48c0a60265de20
144144
refs/heads/autosynth-trace: c94eef6e4d9c6fd24888216e28ca7271959c1cf0
145145
refs/heads/autosynth-websecurityscanner: fa561b356aabcd92d415ae8dc88fd8d87dbc5b23
146146
refs/heads/bigquerystorage: 06db74d123d7f8a3ef48755c2fcabed09faf8e64

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

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public class Publisher {
8686
private final BatchingSettings batchingSettings;
8787

8888
private final Lock messagesBatchLock;
89-
private List<OutstandingPublish> messagesBatch;
90-
private int batchedBytes;
89+
private MessagesBatch messagesBatch;
9190

9291
private final AtomicBoolean activeAlarm;
9392

@@ -116,7 +115,7 @@ private Publisher(Builder builder) throws IOException {
116115
this.batchingSettings = builder.batchingSettings;
117116
this.messageTransform = builder.messageTransform;
118117

119-
messagesBatch = new LinkedList<>();
118+
messagesBatch = new MessagesBatch();
120119
messagesBatchLock = new ReentrantLock();
121120
activeAlarm = new AtomicBoolean(false);
122121
executor = builder.executorProvider.getExecutor();
@@ -207,24 +206,19 @@ public ApiFuture<String> publish(PubsubMessage message) {
207206
// Check if the next message makes the current batch exceed the max batch byte size.
208207
if (!messagesBatch.isEmpty()
209208
&& hasBatchingBytes()
210-
&& batchedBytes + messageSize >= getMaxBatchBytes()) {
211-
batchToSend = new OutstandingBatch(messagesBatch, batchedBytes);
212-
messagesBatch = new LinkedList<>();
213-
batchedBytes = 0;
209+
&& messagesBatch.getBatchedBytes() + messageSize >= getMaxBatchBytes()) {
210+
batchToSend = messagesBatch.popOutstandingBatch();
214211
}
215212

216213
// Border case if the message to send is greater or equals to the max batch size then can't
217214
// be included in the current batch and instead sent immediately.
218215
if (!hasBatchingBytes() || messageSize < getMaxBatchBytes()) {
219-
batchedBytes += messageSize;
220-
messagesBatch.add(outstandingPublish);
216+
messagesBatch.addMessage(outstandingPublish, messageSize);
221217

222218
// If after adding the message we have reached the batch max messages then we have a batch
223219
// to send.
224-
if (messagesBatch.size() == getBatchingSettings().getElementCountThreshold()) {
225-
batchToSend = new OutstandingBatch(messagesBatch, batchedBytes);
226-
messagesBatch = new LinkedList<>();
227-
batchedBytes = 0;
220+
if (messagesBatch.getMessagesCount() == getBatchingSettings().getElementCountThreshold()) {
221+
batchToSend = messagesBatch.popOutstandingBatch();
228222
}
229223
}
230224
// Setup the next duration based delivery alarm if there are messages batched.
@@ -303,9 +297,7 @@ public void publishAllOutstanding() {
303297
if (messagesBatch.isEmpty()) {
304298
return;
305299
}
306-
batchToSend = new OutstandingBatch(messagesBatch, batchedBytes);
307-
messagesBatch = new LinkedList<>();
308-
batchedBytes = 0;
300+
batchToSend = messagesBatch.popOutstandingBatch();
309301
} finally {
310302
messagesBatchLock.unlock();
311303
}
@@ -640,4 +632,37 @@ public Publisher build() throws IOException {
640632
return new Publisher(this);
641633
}
642634
}
635+
636+
private static class MessagesBatch {
637+
private List<OutstandingPublish> messages = new LinkedList<>();
638+
private int batchedBytes;
639+
640+
private OutstandingBatch popOutstandingBatch() {
641+
OutstandingBatch batch = new OutstandingBatch(messages, batchedBytes);
642+
reset();
643+
return batch;
644+
}
645+
646+
private void reset() {
647+
messages = new LinkedList<>();
648+
batchedBytes = 0;
649+
}
650+
651+
private boolean isEmpty() {
652+
return messages.isEmpty();
653+
}
654+
655+
private int getBatchedBytes() {
656+
return batchedBytes;
657+
}
658+
659+
private void addMessage(OutstandingPublish message, int messageSize) {
660+
messages.add(message);
661+
batchedBytes += messageSize;
662+
}
663+
664+
private int getMessagesCount() {
665+
return messages.size();
666+
}
667+
}
643668
}

0 commit comments

Comments
 (0)