9d59e918e6df6dbf29dc9671b3d1736cbff7e04b
java/com.sap.sse.landscape/src/com/sap/sse/landscape/impl/GithubReleasesRepository.java
| ... | ... | @@ -19,6 +19,7 @@ import org.json.simple.JSONObject; |
| 19 | 19 | import org.json.simple.parser.JSONParser; |
| 20 | 20 | import org.json.simple.parser.ParseException; |
| 21 | 21 | |
| 22 | +import com.sap.sse.common.Duration; |
|
| 22 | 23 | import com.sap.sse.common.TimePoint; |
| 23 | 24 | import com.sap.sse.common.Util.Pair; |
| 24 | 25 | import com.sap.sse.landscape.Release; |
| ... | ... | @@ -34,8 +35,6 @@ import com.sap.sse.util.HttpUrlConnectionHelper; |
| 34 | 35 | * |
| 35 | 36 | * TODO Concurrency Control! What, if multiple requests or iterations are run on this repository object concurrently?<p> |
| 36 | 37 | * |
| 37 | - * TODO implement a cool-down period, e.g., one minute, during which the first releases page is loaded only once<p> |
|
| 38 | - * |
|
| 39 | 38 | * @author Axel Uhl (d043530) |
| 40 | 39 | */ |
| 41 | 40 | public class GithubReleasesRepository extends AbstractReleaseRepository implements ReleaseRepository { |
| ... | ... | @@ -68,12 +67,17 @@ public class GithubReleasesRepository extends AbstractReleaseRepository implemen |
| 68 | 67 | |
| 69 | 68 | private boolean cacheContainsOldestRelease; |
| 70 | 69 | |
| 70 | + private TimePoint lastFetchOfNewestReleases; |
|
| 71 | + |
|
| 72 | + private final static Duration RELOAD_NEWEST_RELEASES_AFTER_DURATION = Duration.ONE_MINUTE; |
|
| 73 | + |
|
| 71 | 74 | public GithubReleasesRepository(String owner, String repositoryName, String defaultReleaseNamePrefix) { |
| 72 | 75 | super(defaultReleaseNamePrefix); |
| 73 | 76 | this.owner = owner; |
| 74 | 77 | this.repositoryName = repositoryName; |
| 75 | 78 | this.releasesByPublishingTimePoint = new TreeMap<>(); |
| 76 | 79 | this.cacheContainsOldestRelease = false; |
| 80 | + this.lastFetchOfNewestReleases = null; |
|
| 77 | 81 | } |
| 78 | 82 | |
| 79 | 83 | private String getRepositoryPath() { |
| ... | ... | @@ -91,6 +95,13 @@ public class GithubReleasesRepository extends AbstractReleaseRepository implemen |
| 91 | 95 | } |
| 92 | 96 | |
| 93 | 97 | /** |
| 98 | + * If {@link GithubReleasesRepository#lastFetchOfNewestReleases} is {@code null} or older than the |
|
| 99 | + * {@link GithubReleasesRepository#RELOAD_NEWEST_RELEASES_AFTER_DURATION}, the page with newest releases is actually |
|
| 100 | + * loaded. Otherwise, we assume that within the |
|
| 101 | + * {@link GithubReleasesRepository#RELOAD_NEWEST_RELEASES_AFTER_DURATION} interval changes are sufficiently |
|
| 102 | + * unlikely, so we will set the {@link #cachedReleasesIterator} to directly serve the current contents of the cache. |
|
| 103 | + * <p> |
|
| 104 | + * |
|
| 94 | 105 | * Always fetches the first page from the {@code /releases} end point and starts constructing and |
| 95 | 106 | * {@link GithubReleasesRepository#releasesByPublishingTimePoint caching} releases, until a publishing time point |
| 96 | 107 | * overlap with {@link GithubReleasesRepository#releasesByPublishingTimePoint} is found. Iteration then starts from |
| ... | ... | @@ -128,9 +139,16 @@ public class GithubReleasesRepository extends AbstractReleaseRepository implemen |
| 128 | 139 | |
| 129 | 140 | private ReleaseIterator() throws MalformedURLException, IOException, ParseException { |
| 130 | 141 | nextPageURL = getReleasesURL(); |
| 131 | - cachedReleasesIterator = null; |
|
| 132 | - while (nextPageURL != null && cachedReleasesIterator == null) { |
|
| 133 | - loadNextPage(/* olderThan */ null); |
|
| 142 | + final TimePoint now = TimePoint.now(); |
|
| 143 | + if (lastFetchOfNewestReleases != null && lastFetchOfNewestReleases.until(now) |
|
| 144 | + .compareTo(RELOAD_NEWEST_RELEASES_AFTER_DURATION) < 0) { |
|
| 145 | + cachedReleasesIterator = releasesByPublishingTimePoint.descendingMap().values().iterator(); |
|
| 146 | + } else { |
|
| 147 | + cachedReleasesIterator = null; |
|
| 148 | + while (nextPageURL != null && cachedReleasesIterator == null) { |
|
| 149 | + lastFetchOfNewestReleases = now; |
|
| 150 | + loadNextPage(/* olderThan */ null); |
|
| 151 | + } |
|
| 134 | 152 | } |
| 135 | 153 | } |
| 136 | 154 |