-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fetched child folder information in parallel
Closes #26
- Loading branch information
1 parent
cb07105
commit c489fec
Showing
10 changed files
with
266 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/io/github/devatherock/artifactory/config/AppProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.devatherock.artifactory.config; | ||
|
||
import io.micronaut.context.annotation.ConfigurationProperties; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties("artifactory.badge") | ||
public class AppProperties { | ||
/** | ||
* Amount of parallelism to use when fetching details about versions of an image | ||
*/ | ||
private int parallelism = 5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/io/github/devatherock/artifactory/util/ParallelProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.github.devatherock.artifactory.util; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.function.Supplier; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class ParallelProcessor { | ||
private final Executor executor; | ||
private final Semaphore semaphore; // To limit parallelism in case of outbound API calls | ||
|
||
public <T> List<T> parallelProcess(List<Supplier<T>> suppliers) { | ||
Map<Integer, Optional<T>> result = new ConcurrentHashMap<>(); | ||
CountDownLatch countDownLatch = new CountDownLatch(suppliers.size()); | ||
|
||
try { | ||
for (int index = 0; index < suppliers.size(); index++) { | ||
int currentIndex = index; | ||
|
||
semaphore.acquire(); | ||
executor.execute(() -> { | ||
try { | ||
LOGGER.trace("In parallelProcess. Index: {}", currentIndex); | ||
T currentResult = suppliers.get(currentIndex).get(); | ||
result.put(currentIndex, currentResult != null ? Optional.of(currentResult) : Optional.empty()); | ||
} catch (Exception exception) { | ||
LOGGER.warn("Exception while processing index {}", currentIndex, exception); | ||
result.put(currentIndex, Optional.empty()); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
semaphore.release(); | ||
} | ||
|
||
countDownLatch.await(); | ||
} catch (InterruptedException exception) { | ||
LOGGER.warn("Exception during parallel processing", exception); | ||
} | ||
|
||
return result.entrySet() | ||
.stream() | ||
.sorted(Entry.comparingByKey(Comparator.naturalOrder())) | ||
.map(entry -> entry.getValue().orElse(null)) | ||
.toList(); | ||
} | ||
} |
Oops, something went wrong.