Skip to content

Commit

Permalink
Merge pull request #219 from entur/fix-minimum-max-age
Browse files Browse the repository at this point in the history
fix: max-age should not be less than 0
  • Loading branch information
testower authored Aug 24, 2023
2 parents b5055f3 + 049f916 commit 044d979
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
67 changes: 10 additions & 57 deletions src/main/java/org/entur/lamassu/controller/GBFSFeedController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.entur.lamassu.controller;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.List;
import java.util.NoSuchElementException;
Expand All @@ -12,7 +11,6 @@
import org.entur.gbfs.v2_3.gbfs.GBFSFeed;
import org.entur.gbfs.v2_3.gbfs.GBFSFeedName;
import org.entur.gbfs.v2_3.gbfs.GBFSFeeds;
import org.entur.gbfs.v2_3.gbfs.GBFSGbfs;
import org.entur.gbfs.v2_3.geofencing_zones.GBFSGeofencingZones;
import org.entur.gbfs.v2_3.station_information.GBFSStationInformation;
import org.entur.gbfs.v2_3.station_status.GBFSStationStatus;
Expand All @@ -31,6 +29,7 @@
import org.entur.lamassu.model.provider.FeedProvider;
import org.entur.lamassu.service.FeedProviderService;
import org.entur.lamassu.service.SystemDiscoveryService;
import org.entur.lamassu.util.CacheUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -115,7 +114,9 @@ public ResponseEntity<Object> getGbfsFeedForProvider(
return ResponseEntity
.ok()
.cacheControl(
CacheControl.maxAge(getMaxAge(feedName, data), TimeUnit.SECONDS).cachePublic()
CacheControl
.maxAge(CacheUtil.getMaxAge(feedName, data), TimeUnit.SECONDS)
.cachePublic()
)
.body(data);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -145,7 +146,9 @@ public Object getInternalGbfsFeedForProvider(
return ResponseEntity
.ok()
.cacheControl(
CacheControl.maxAge(getMaxAge(feedName, data), TimeUnit.SECONDS).cachePublic()
CacheControl
.maxAge(CacheUtil.getMaxAge(feedName, data), TimeUnit.SECONDS)
.cachePublic()
)
.body(data);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -266,7 +269,9 @@ public ResponseEntity<Object> getV3Feed(
return ResponseEntity
.ok()
.cacheControl(
CacheControl.maxAge(getMaxAge(feedName, mapped), TimeUnit.SECONDS).cachePublic()
CacheControl
.maxAge(CacheUtil.getMaxAge(feedName, mapped), TimeUnit.SECONDS)
.cachePublic()
)
.body(mapped);
} catch (IllegalArgumentException e) {
Expand All @@ -276,58 +281,6 @@ public ResponseEntity<Object> getV3Feed(
}
}

private int getMaxAge(GBFSFeedName feedName, Object data) {
int maxAge = 60;
try {
Integer lastUpdated = (Integer) feedName
.implementingClass()
.getMethod("getLastUpdated")
.invoke(data);
Integer ttl = (Integer) feedName
.implementingClass()
.getMethod("getTtl")
.invoke(data);

if (lastUpdated != null && ttl != null) {
maxAge = getCurrentTimeSeconds() - lastUpdated + ttl;
}
} catch (
IllegalAccessException | InvocationTargetException | NoSuchMethodException e
) {
// log something?
}

return maxAge;
}

private int getMaxAge(org.entur.gbfs.v3_0_RC.gbfs.GBFSFeed.Name feedName, Object data) {
int maxAge = 60;
try {
Integer lastUpdated = (Integer) org.entur.gbfs.v3_0_RC.gbfs.GBFSFeedName
.implementingClass(feedName)
.getMethod("getLastUpdated")
.invoke(data);
Integer ttl = (Integer) org.entur.gbfs.v3_0_RC.gbfs.GBFSFeedName
.implementingClass(feedName)
.getMethod("getTtl")
.invoke(data);

if (lastUpdated != null && ttl != null) {
maxAge = getCurrentTimeSeconds() - lastUpdated + ttl;
}
} catch (
IllegalAccessException | InvocationTargetException | NoSuchMethodException e
) {
// log something?
}

return maxAge;
}

private int getCurrentTimeSeconds() {
return (int) (java.lang.System.currentTimeMillis() / 1000L);
}

@NotNull
private Object getFeed(String systemId, String feed) {
var feedName = GBFSFeedName.fromValue(feed);
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/entur/lamassu/util/CacheUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.entur.lamassu.util;

import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
import org.entur.gbfs.v2_3.gbfs.GBFSFeedName;

public class CacheUtil {

Expand All @@ -32,4 +34,51 @@ public static int getTtl(int lastUpdated, int ttl, int minimumTtl) {
public static int getTtl(int lastUpdated, int ttl, int minimumTtl, int maximumTtl) {
return Math.min(getTtl(lastUpdated, ttl, minimumTtl), maximumTtl);
}

public static int getMaxAge(GBFSFeedName feedName, Object data) {
int maxAge = 60;
try {
Integer lastUpdated = (Integer) feedName
.implementingClass()
.getMethod("getLastUpdated")
.invoke(data);
Integer ttl = (Integer) feedName
.implementingClass()
.getMethod("getTtl")
.invoke(data);

maxAge = getTtl(lastUpdated, ttl, 0);
} catch (
IllegalAccessException | InvocationTargetException | NoSuchMethodException e
) {
// log something?
}

return maxAge;
}

public static int getMaxAge(
org.entur.gbfs.v3_0_RC.gbfs.GBFSFeed.Name feedName,
Object data
) {
int maxAge = 60;
try {
Integer lastUpdated = (Integer) org.entur.gbfs.v3_0_RC.gbfs.GBFSFeedName
.implementingClass(feedName)
.getMethod("getLastUpdated")
.invoke(data);
Integer ttl = (Integer) org.entur.gbfs.v3_0_RC.gbfs.GBFSFeedName
.implementingClass(feedName)
.getMethod("getTtl")
.invoke(data);

maxAge = getTtl(lastUpdated, ttl, 0);
} catch (
IllegalAccessException | InvocationTargetException | NoSuchMethodException e
) {
// log something?
}

return maxAge;
}
}
24 changes: 24 additions & 0 deletions src/test/java/org/entur/lamassu/util/CacheUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.mockito.Mockito.mockStatic;

import java.time.Instant;
import org.entur.gbfs.v2_3.gbfs.GBFS;
import org.entur.gbfs.v2_3.gbfs.GBFSFeedName;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -66,4 +68,26 @@ void getTtlReturnsMinimumTtlWhenLessThanMinimumTtl() {
void getTtlReturnsCalculatedTtlWhenLargerThanMinimumTtl() {
Assertions.assertEquals(20, CacheUtil.getTtl(now - 10, 30, 10));
}

@Test
void getMaxAgeWorks() {
Assertions.assertEquals(
60,
CacheUtil.getMaxAge(
GBFSFeedName.GBFS,
new GBFS().withLastUpdated(1640000000 - 60).withTtl(120)
)
);
}

@Test
void getMaxAgeReturnsZeroWhenCalculatedTtlIsInThePast() {
Assertions.assertEquals(
0,
CacheUtil.getMaxAge(
GBFSFeedName.GBFS,
new GBFS().withLastUpdated(1540000000).withTtl(60)
)
);
}
}

0 comments on commit 044d979

Please sign in to comment.