Skip to content

Commit

Permalink
#45 Cache for the version
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed May 15, 2018
1 parent 3f91c89 commit b8b0e2e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ OntrackConfiguration getOntrackConfiguration() {
private String ontrackPassword;
private int ontrackMaxTries = 1;
private int ontrackRetryDelaySeconds = 10000;
private int ontrackVersionCacheExpirationSeconds = 3600;
private OntrackSecurityMode securityMode = OntrackSecurityMode.DEFAULT;
private final AtomicReference<Version> version = new AtomicReference<>();

private final AtomicReference<VersionCache> version = new AtomicReference<>();

public OntrackConfiguration() {
load();
Expand All @@ -48,6 +50,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
ontrackPassword = json.getString("ontrackPassword");
ontrackMaxTries = json.getInt("ontrackMaxTries");
ontrackRetryDelaySeconds = json.getInt("ontrackRetryDelaySeconds");
ontrackVersionCacheExpirationSeconds = json.getInt("ontrackVersionCacheExpirationSeconds");
securityMode = OntrackSecurityMode.valueOf(json.getString("securityMode"));
save();
boolean ok = super.configure(req, json);
Expand All @@ -57,13 +60,32 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
return ok;
}

private void loadVersion() {
version.updateAndGet(
ignored -> computeVersion()
);
private VersionCache loadVersion() {
return version.updateAndGet(current -> {
if (current != null) {
long expiredTimeMs = System.currentTimeMillis() - current.getTimestamp();
long expiredTimeSeconds = expiredTimeMs / 1000;
if (expiredTimeSeconds >= ontrackVersionCacheExpirationSeconds) {
return computeVersionCache();
} else {
return current;
}
} else {
return computeVersionCache();
}
});
}

private VersionCache computeVersionCache() {
Version remoteVersion = getRemoteVersion();
if (remoteVersion != null) {
return new VersionCache(remoteVersion, System.currentTimeMillis());
} else {
return null;
}
}

private Version computeVersion() {
private Version getRemoteVersion() {
try {
OntrackConnection connection = OntrackConnection.create(ontrackUrl);
String user = ontrackUser;
Expand All @@ -82,12 +104,11 @@ private Version computeVersion() {

public @Nullable
Version getVersion() {
return version.updateAndGet(
currentVersion -> currentVersion != null ? currentVersion : computeVersion()
);
VersionCache versionCache = loadVersion();
return versionCache != null ? versionCache.getValue() : null;
}

public String getOntrackConfigurationName() {
String getOntrackConfigurationName() {
return ontrackConfigurationName;
}

Expand Down Expand Up @@ -150,6 +171,16 @@ public void setOntrackRetryDelaySeconds(int ontrackRetryDelaySeconds) {
this.ontrackRetryDelaySeconds = ontrackRetryDelaySeconds;
}

@SuppressWarnings("unused")
public int getOntrackVersionCacheExpirationSeconds() {
return ontrackVersionCacheExpirationSeconds;
}

@SuppressWarnings("unused")
public void setOntrackVersionCacheExpirationSeconds(int ontrackVersionCacheExpirationSeconds) {
this.ontrackVersionCacheExpirationSeconds = ontrackVersionCacheExpirationSeconds;
}

@SuppressWarnings("unused")
public ListBoxModel doFillSecurityModeItems() {
ListBoxModel items = new ListBoxModel();
Expand All @@ -158,4 +189,22 @@ public ListBoxModel doFillSecurityModeItems() {
}
return items;
}

private static class VersionCache {
private final Version value;
private final long timestamp;

private VersionCache(Version value, long timestamp) {
this.value = value;
this.timestamp = timestamp;
}

public Version getValue() {
return value;
}

long getTimestamp() {
return timestamp;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<f:entry title="Retry delay (s)">
<f:textbox name="ontrack.ontrackRetryDelaySeconds" value="${descriptor.getOntrackRetryDelaySeconds()}"/>
</f:entry>
<f:entry title="Remote Ontrack version cache (s)">
<f:textbox name="ontrack.ontrackVersionCacheExpirationSeconds" value="${descriptor.getOntrackVersionCacheExpirationSeconds()}"/>
</f:entry>
<f:entry field="securityMode" title="Security mode">
<f:select/>
</f:entry>
Expand Down

0 comments on commit b8b0e2e

Please sign in to comment.