Skip to content

Commit 11c8259

Browse files
committed
Implemented logic to update cloud asn cache
1 parent 7d91a85 commit 11c8259

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/main/groovy/io/ipgeolocation/databaseReader/databases/cloudprovider/CloudProviderIndexer.groovy

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import static com.google.common.base.Preconditions.checkNotNull
88
class CloudProviderIndexer {
99
private Set<String> cloudProviders
1010
private Set<String> cloudAsnSet
11-
11+
private Set<String> backupCloudAsnSet
12+
public static volatile boolean updatingMainCache = false
1213
CloudProviderIndexer() {
1314
cloudProviders = new HashSet<String>()
1415
cloudAsnSet = new HashSet<String>()
16+
backupCloudAsnSet = new HashSet<String>()
1517
}
1618

1719
void index(String cloudProvider) {
@@ -44,13 +46,21 @@ class CloudProviderIndexer {
4446
boolean isCloudAsn(String asn) {
4547
checkNotNull(asn, "Pre-condition violated: asn must not be null.")
4648
asn = normalizeAsn(asn)
47-
cloudAsnSet.contains(asn)
49+
if (updatingMainCache) {
50+
return backupCloudAsnSet.contains(asn)
51+
} else {
52+
return cloudAsnSet.contains(asn)
53+
}
4854
}
4955

5056
private static String normalizeAsn(String asn) {
5157
asn.toLowerCase().replaceAll("as|[\\s.,\\-_\\\"]", "")
5258
}
5359

60+
void copyMainToBackupCache() {
61+
backupCloudAsnSet.addAll(cloudAsnSet)
62+
}
63+
5464
Integer size() {
5565
cloudProviders.size()
5666
}
@@ -62,4 +72,8 @@ class CloudProviderIndexer {
6272
void clearCloudASNSet () {
6373
cloudAsnSet.clear()
6474
}
75+
76+
void clearBackupCloudASNSet () {
77+
backupCloudAsnSet.clear()
78+
}
6579
}

src/main/groovy/io/ipgeolocation/databaseReader/services/database/CsvDatabaseService.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ class CsvDatabaseService implements DatabaseService {
8585

8686
@Scheduled(cron = "0 0 0 ? * WED", zone = "UTC")
8787
void updateCloudAsnCache() {
88-
if (databaseUpdateService.getDatabaseVersion() in DatabaseVersion.DATABASES_WITH_PROXY) {
88+
if (databaseUpdateService.getDatabaseVersion() in DatabaseVersion.DATABASES_WITH_PROXY
89+
&& databaseUpdateService.databaseType == "csv") {
90+
cloudProviderIndexer.copyMainToBackupCache()
91+
CloudProviderIndexer.updatingMainCache = true
8992
cloudProviderIndexer.clearCloudASNSet()
9093
cloudProviderLoader.downloadAndCacheBadASN(cloudProviderIndexer, cloudAsnUrl)
94+
CloudProviderIndexer.updatingMainCache = false
95+
cloudProviderIndexer.clearBackupCloudASNSet()
9196
}
9297
}
9398

src/main/groovy/io/ipgeolocation/databaseReader/services/database/MMDBDatabaseService.groovy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ class MMDBDatabaseService implements DatabaseService {
8282
}
8383
}
8484

85-
@Scheduled(cron = "0 0 0 ? * WED")
85+
@Scheduled(cron = "0 0 0 ? * WED", zone = "UTC")
8686
void updateCloudAsnCache() {
87-
if (databaseUpdateService.getDatabaseVersion() in DatabaseVersion.DATABASES_WITH_PROXY) {
87+
if (databaseUpdateService.getDatabaseVersion() in DatabaseVersion.DATABASES_WITH_PROXY
88+
&& databaseUpdateService.databaseType == "mmdb") {
89+
cloudProviderIndexer.copyMainToBackupCache()
90+
CloudProviderIndexer.updatingMainCache = true
8891
cloudProviderIndexer.clearCloudASNSet()
8992
cloudProviderLoader.downloadAndCacheBadASN(cloudProviderIndexer, cloudAsnUrl)
93+
CloudProviderIndexer.updatingMainCache = false
94+
cloudProviderIndexer.clearBackupCloudASNSet()
9095
}
9196
}
9297

0 commit comments

Comments
 (0)