You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/08/30 11:12:35 UTC

[camel] 02/02: CAMEL-18439: camel-github - Consumer that polls commits crashed when repository has more than 100 commits

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit b90991eb8f16df156ebcb9cd322b2411b69b048c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 30 13:10:58 2022 +0200

    CAMEL-18439: camel-github - Consumer that polls commits crashed when repository has more than 100 commits
---
 .../camel/component/github/consumer/CommitConsumer.java      | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
index d8afce2bbb5..3bf44be6bc7 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
@@ -19,7 +19,7 @@ package org.apache.camel.component.github.consumer;
 import java.util.List;
 import java.util.Queue;
 import java.util.Stack;
-import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -34,12 +34,14 @@ import org.slf4j.LoggerFactory;
 public class CommitConsumer extends AbstractGitHubConsumer {
     private static final transient Logger LOG = LoggerFactory.getLogger(CommitConsumer.class);
 
+    private static final int CAPACITY = 100;
+
     private CommitService commitService;
     private final String branchName;
     private final String startingSha;
 
-    // keep a chunk of the last hashes so we can filter out duplicates
-    private final Queue<String> commitHashes = new ArrayBlockingQueue<>(100);
+    // keep a chunk of the last 100 hashes, so we can filter out duplicates
+    private final Queue<String> commitHashes = new LinkedBlockingQueue<>(CAPACITY);
     private volatile String lastSha;
 
     public CommitConsumer(GitHubEndpoint endpoint, Processor processor, String branchName,
@@ -77,6 +79,10 @@ public class CommitConsumer extends AbstractGitHubConsumer {
             List<RepositoryCommit> commits = commitService.getCommits(getRepository(), branchName, null);
             for (RepositoryCommit commit : commits) {
                 String sha = commit.getSha();
+                // make room when adding new elements
+                while (commitHashes.size() > CAPACITY - 1) {
+                    commitHashes.remove();
+                }
                 commitHashes.add(sha);
                 if (lastSha == null) {
                     lastSha = sha;