Skip to content

Commit 9794643

Browse files
committed
fix review: dbreader + config + inferencedata
1 parent 806e48c commit 9794643

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.netty.handler.codec.http.HttpResponseStatus;
44
import io.vertx.core.file.FileSystem;
5+
import io.vertx.core.http.HttpClientOptions;
56
import io.vertx.core.http.HttpClientRequest;
67
import io.vertx.core.http.HttpClientResponse;
78
import com.maxmind.db.Reader;
@@ -37,16 +38,16 @@ public class DatabaseReaderFactory implements Initializable {
3738

3839
private final FileSystem fileSystem;
3940

40-
public DatabaseReaderFactory(
41-
GreenbidsRealTimeDataProperties properties, Vertx vertx) {
41+
public DatabaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) {
4242
this.properties = properties;
4343
this.vertx = vertx;
4444
this.fileSystem = vertx.fileSystem();
4545
}
4646

4747
@Override
4848
public void initialize(Promise<Void> initializePromise) {
49-
vertx.executeBlocking(() -> downloadAndExtract().onSuccess(databaseReaderRef::set))
49+
downloadAndExtract()
50+
.onSuccess(databaseReaderRef::set)
5051
.<Void>mapEmpty()
5152
.onComplete(initializePromise);
5253
}
@@ -55,7 +56,7 @@ private Future<DatabaseReader> downloadAndExtract() {
5556
final String downloadUrl = properties.getGeoLiteCountryPath();
5657
final String tmpPath = properties.getTmpPath();
5758
return downloadFile(downloadUrl, tmpPath)
58-
.map(unused -> extractMMDB(tmpPath))
59+
.compose(ignored -> vertx.executeBlocking(() -> extractMMDB(tmpPath)))
5960
.onComplete(ar -> removeFile(tmpPath));
6061
}
6162

@@ -74,7 +75,11 @@ private Future<HttpClientResponse> sendHttpRequest(String url) {
7475
.setTimeout(properties.getTimeoutMs())
7576
.setAbsoluteURI(url);
7677

77-
return vertx.createHttpClient().request(options)
78+
final HttpClientOptions httpClientOptions = new HttpClientOptions()
79+
.setConnectTimeout(properties.getTimeoutMs().intValue())
80+
.setMaxRedirects(properties.getMaxRedirects());
81+
82+
return vertx.createHttpClient(httpClientOptions).request(options)
7883
.compose(HttpClientRequest::send)
7984
.map(this::validateResponse);
8085
}
@@ -113,8 +118,12 @@ private DatabaseReader extractMMDB(String tarGzPath) {
113118
}
114119

115120
private void removeFile(String filePath) {
116-
fileSystem.delete(filePath)
117-
.onFailure(err -> logger.error("Failed to remove file {}", filePath, err));
121+
fileSystem.exists(filePath).onSuccess(exists -> {
122+
if (exists) {
123+
fileSystem.delete(filePath)
124+
.onFailure(err -> logger.error("Failed to remove file {}", filePath, err));
125+
}
126+
});
118127
}
119128

120129
public DatabaseReader getDatabaseReader() {

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
public class GreenbidsRealTimeDataConfiguration {
3333

3434
@Bean
35-
DatabaseReaderFactory databaseReaderFactory(
36-
GreenbidsRealTimeDataProperties properties, Vertx vertx) {
35+
DatabaseReaderFactory databaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) {
3736
return new DatabaseReaderFactory(properties, vertx);
3837
}
3938

4039
@Bean
41-
GreenbidsInferenceDataService greenbidsInferenceDataService(
42-
DatabaseReaderFactory databaseReaderFactory,
43-
CountryCodeMapper countryCodeMapper) {
40+
GreenbidsInferenceDataService greenbidsInferenceDataService(DatabaseReaderFactory databaseReaderFactory,
41+
CountryCodeMapper countryCodeMapper) {
42+
4443
return new GreenbidsInferenceDataService(
4544
databaseReaderFactory, ObjectMapperProvider.mapper(), countryCodeMapper);
4645
}

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public class GreenbidsRealTimeDataProperties {
2222
String thresholdsCacheKeyPrefix;
2323

2424
Long timeoutMs;
25+
26+
Integer maxRedirects;
2527
}

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/GreenbidsInferenceDataService.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ public class GreenbidsInferenceDataService {
4040

4141
private final CountryCodeMapper countryCodeMapper;
4242

43-
public GreenbidsInferenceDataService(
44-
DatabaseReaderFactory dbReaderFactory,
45-
ObjectMapper mapper,
46-
CountryCodeMapper countryCodeMapper) {
43+
public GreenbidsInferenceDataService(DatabaseReaderFactory dbReaderFactory,
44+
ObjectMapper mapper,
45+
CountryCodeMapper countryCodeMapper) {
4746
this.databaseReaderFactory = Objects.requireNonNull(dbReaderFactory);
4847
this.mapper = Objects.requireNonNull(mapper);
4948
this.countryCodeMapper = Objects.requireNonNull(countryCodeMapper);
@@ -100,6 +99,7 @@ private List<ThrottlingMessage> extractMessagesForImp(
10099
.map(Geo::getCountry)
101100
.map(countryCodeMapper::mapToAlpha2)
102101
.map(GreenbidsInferenceDataService::getCountryNameFromAlpha2)
102+
.filter(c -> !c.isEmpty())
103103
.orElseGet(() -> getCountry(ip));
104104

105105
return createThrottlingMessages(
@@ -119,13 +119,10 @@ private static String getCountryNameFromAlpha2(String isoCode) {
119119
}
120120

121121
private String getCountry(String ip) {
122-
if (ip == null) {
123-
return null;
124-
}
125-
126-
return Optional.ofNullable(databaseReaderFactory.getDatabaseReader())
127-
.map(dbReader -> getCountryFromIpUsingDatabase(dbReader, ip))
128-
.orElse(null);
122+
final DatabaseReader databaseReader = databaseReaderFactory.getDatabaseReader();
123+
return ip != null && databaseReader != null
124+
? getCountryFromIpUsingDatabase(databaseReader, ip)
125+
: null;
129126
}
130127

131128
private String getCountryFromIpUsingDatabase(DatabaseReader databaseReader, String ip) {

0 commit comments

Comments
 (0)