Skip to content

Commit

Permalink
fix review: dbreader + config + inferencedata
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Jan 16, 2025
1 parent 806e48c commit 9794643
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,16 +38,16 @@ 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();
}

@Override
public void initialize(Promise<Void> initializePromise) {
vertx.executeBlocking(() -> downloadAndExtract().onSuccess(databaseReaderRef::set))
downloadAndExtract()
.onSuccess(databaseReaderRef::set)
.<Void>mapEmpty()
.onComplete(initializePromise);
}
Expand All @@ -55,7 +56,7 @@ private Future<DatabaseReader> 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));
}

Expand All @@ -74,7 +75,11 @@ private Future<HttpClientResponse> 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);
}
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class GreenbidsRealTimeDataProperties {
String thresholdsCacheKeyPrefix;

Long timeoutMs;

Integer maxRedirects;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -100,6 +99,7 @@ private List<ThrottlingMessage> extractMessagesForImp(
.map(Geo::getCountry)
.map(countryCodeMapper::mapToAlpha2)
.map(GreenbidsInferenceDataService::getCountryNameFromAlpha2)
.filter(c -> !c.isEmpty())
.orElseGet(() -> getCountry(ip));

return createThrottlingMessages(
Expand All @@ -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) {
Expand Down

0 comments on commit 9794643

Please sign in to comment.