diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java index 4f2d36a130a..5ff95b347a7 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java @@ -2,6 +2,7 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.vertx.core.file.FileSystem; +import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpClientRequest; import io.vertx.core.http.HttpClientResponse; import com.maxmind.db.Reader; @@ -37,8 +38,7 @@ public class DatabaseReaderFactory implements Initializable { private final FileSystem fileSystem; - public DatabaseReaderFactory( - GreenbidsRealTimeDataProperties properties, Vertx vertx) { + public DatabaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) { this.properties = properties; this.vertx = vertx; this.fileSystem = vertx.fileSystem(); @@ -46,7 +46,8 @@ public DatabaseReaderFactory( @Override public void initialize(Promise initializePromise) { - vertx.executeBlocking(() -> downloadAndExtract().onSuccess(databaseReaderRef::set)) + downloadAndExtract() + .onSuccess(databaseReaderRef::set) .mapEmpty() .onComplete(initializePromise); } @@ -55,7 +56,7 @@ private Future downloadAndExtract() { final String downloadUrl = properties.getGeoLiteCountryPath(); final String tmpPath = properties.getTmpPath(); return downloadFile(downloadUrl, tmpPath) - .map(unused -> extractMMDB(tmpPath)) + .compose(ignored -> vertx.executeBlocking(() -> extractMMDB(tmpPath))) .onComplete(ar -> removeFile(tmpPath)); } @@ -74,7 +75,11 @@ private Future sendHttpRequest(String url) { .setTimeout(properties.getTimeoutMs()) .setAbsoluteURI(url); - return vertx.createHttpClient().request(options) + final HttpClientOptions httpClientOptions = new HttpClientOptions() + .setConnectTimeout(properties.getTimeoutMs().intValue()) + .setMaxRedirects(properties.getMaxRedirects()); + + return vertx.createHttpClient(httpClientOptions).request(options) .compose(HttpClientRequest::send) .map(this::validateResponse); } @@ -113,8 +118,12 @@ private DatabaseReader extractMMDB(String tarGzPath) { } private void removeFile(String filePath) { - fileSystem.delete(filePath) - .onFailure(err -> logger.error("Failed to remove file {}", filePath, err)); + fileSystem.exists(filePath).onSuccess(exists -> { + if (exists) { + fileSystem.delete(filePath) + .onFailure(err -> logger.error("Failed to remove file {}", filePath, err)); + } + }); } public DatabaseReader getDatabaseReader() { diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java index 596d41bc939..a6b47f7d6ad 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java @@ -32,15 +32,14 @@ public class GreenbidsRealTimeDataConfiguration { @Bean - DatabaseReaderFactory databaseReaderFactory( - GreenbidsRealTimeDataProperties properties, Vertx vertx) { + DatabaseReaderFactory databaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) { return new DatabaseReaderFactory(properties, vertx); } @Bean - GreenbidsInferenceDataService greenbidsInferenceDataService( - DatabaseReaderFactory databaseReaderFactory, - CountryCodeMapper countryCodeMapper) { + GreenbidsInferenceDataService greenbidsInferenceDataService(DatabaseReaderFactory databaseReaderFactory, + CountryCodeMapper countryCodeMapper) { + return new GreenbidsInferenceDataService( databaseReaderFactory, ObjectMapperProvider.mapper(), countryCodeMapper); } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java index e86776d155e..4752a2e8840 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java @@ -22,4 +22,6 @@ public class GreenbidsRealTimeDataProperties { String thresholdsCacheKeyPrefix; Long timeoutMs; + + Integer maxRedirects; } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/GreenbidsInferenceDataService.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/GreenbidsInferenceDataService.java index 6bc599a0b85..c16a8de917f 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/GreenbidsInferenceDataService.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/GreenbidsInferenceDataService.java @@ -40,10 +40,9 @@ public class GreenbidsInferenceDataService { private final CountryCodeMapper countryCodeMapper; - public GreenbidsInferenceDataService( - DatabaseReaderFactory dbReaderFactory, - ObjectMapper mapper, - CountryCodeMapper countryCodeMapper) { + public GreenbidsInferenceDataService(DatabaseReaderFactory dbReaderFactory, + ObjectMapper mapper, + CountryCodeMapper countryCodeMapper) { this.databaseReaderFactory = Objects.requireNonNull(dbReaderFactory); this.mapper = Objects.requireNonNull(mapper); this.countryCodeMapper = Objects.requireNonNull(countryCodeMapper); @@ -100,6 +99,7 @@ private List extractMessagesForImp( .map(Geo::getCountry) .map(countryCodeMapper::mapToAlpha2) .map(GreenbidsInferenceDataService::getCountryNameFromAlpha2) + .filter(c -> !c.isEmpty()) .orElseGet(() -> getCountry(ip)); return createThrottlingMessages( @@ -119,13 +119,10 @@ private static String getCountryNameFromAlpha2(String isoCode) { } private String getCountry(String ip) { - if (ip == null) { - return null; - } - - return Optional.ofNullable(databaseReaderFactory.getDatabaseReader()) - .map(dbReader -> getCountryFromIpUsingDatabase(dbReader, ip)) - .orElse(null); + final DatabaseReader databaseReader = databaseReaderFactory.getDatabaseReader(); + return ip != null && databaseReader != null + ? getCountryFromIpUsingDatabase(databaseReader, ip) + : null; } private String getCountryFromIpUsingDatabase(DatabaseReader databaseReader, String ip) {