Skip to content

[geoip] refactor maxmind provider lookup functions#31490

Closed
hwyuan wants to merge 1 commit intoenvoyproxy:mainfrom
hwyuan:lookup-refactor
Closed

[geoip] refactor maxmind provider lookup functions#31490
hwyuan wants to merge 1 commit intoenvoyproxy:mainfrom
hwyuan:lookup-refactor

Conversation

@hwyuan
Copy link
Copy Markdown
Contributor

@hwyuan hwyuan commented Dec 22, 2023

Commit Message: refactor maxmind provider lookup functions
Additional Description:
Extracted common maxmind db lookup functions into lookupInMaxmindDb
Introduced isLookupEnabledForDbType and populateGeoLookupResultForDbType to handle db-specific logics
Risk Level: low
Testing: TODO
Docs Changes:
Release Notes:
Platform Specific Features:

@repokitteh-read-only
Copy link
Copy Markdown

Hi @hwyuan, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

🐱

Caused by: #31490 was opened by hwyuan.

see: more, trace.

@repokitteh-read-only
Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #31490 was opened by hwyuan.

see: more, trace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RFC: MaxmindDbPtr is a unique_ptr, so we are assuming the pointer is always available in this function call, would be something to pay attention to when db pointer is reloaded (such as in hot reload)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point in taking a const reference to a smart pointer - if you're going to require that it exists for the duration of the call, the function should just take a reference to a MaxmindDb.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now stat_prefix doesn't add much value since it's always the same as db_type, if we are ok with db_type using a string (vs. enum), then we could probably combine these two fields

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think you need to pass/use stat_prefix here, as all maxmind stats is created in stat scope that already has stat_prefix as part of its name: https://github.com/envoyproxy/envoy/pull/31490/files#diff-d21c11080878cdf65727845da9fa3846d138efb810faeb12e8d16006713446deL30. Tagging @jmarantz as stats expert to confirm

Signed-off-by: Haowei Yuan <hyuan@wustl.edu>
@hwyuan hwyuan marked this pull request as ready for review December 24, 2023 03:45
}

void GeoipProvider::lookupInCityDb(
bool GeoipProvider::isLookupEnabledForDbType(const std::string& db_type) const {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use absl::string_view for read-only string parameters, it makes the function more flexible. (In that if the caller has a string_view or a constant and the function takes a std::string&, the caller has to create a string object.)

(Throughout - this actually already makes a difference since the caller is passing a bare string constant, which means it is unnecessarily allocating a temporary string object.)

Or since it's an internal implementation detail, make it an enum, and avoid all the string compares.

Or, since it's actually only ever used as a constant and branched like a switch, it would probably make the most sense to redo the whole thing as an enum template function, something like

class enum DbType {CITY, ISP, ANON};

template <DbType T> bool isLookupEnabledForDbType() const;
template <DbType T> void populateGeoLookupResult(
    MMDB_lookup_result_s& mmdb_lookup_result,
    absl::flat_hash_map<std::string, std::string>& lookup_result) const;
template <DbType T> void lookupInMaxMindDb(...);

Implementing specializations for the first two (in lieu of the branches) and using the type to explicitly call with the appropriate type at the callsites.

(I would suggest actually making the specialized template functions non-member functions that take the required members as parameters, so that they don't have to be templated in the header, at least as far as that is possible. It would work for isLookupEnabled but I haven't looked closely to see if it's possible for populateGeoLookupResult)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point in taking a const reference to a smart pointer - if you're going to require that it exists for the duration of the call, the function should just take a reference to a MaxmindDb.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think you need to pass/use stat_prefix here, as all maxmind stats is created in stat scope that already has stat_prefix as part of its name: https://github.com/envoyproxy/envoy/pull/31490/files#diff-d21c11080878cdf65727845da9fa3846d138efb810faeb12e8d16006713446deL30. Tagging @jmarantz as stats expert to confirm

void lookupInAnonDb(const Network::Address::InstanceConstSharedPtr& remote_address,
absl::flat_hash_map<std::string, std::string>& lookup_result) const;

void populateGeoLookupResultForCityDb(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since all populateGeoLookupResultFor*Db methods have very similar structure, would it be possible to refactor them into single method?

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 3, 2024

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Feb 3, 2024
@github-actions
Copy link
Copy Markdown

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot closed this Feb 10, 2024
@hwyuan
Copy link
Copy Markdown
Contributor Author

hwyuan commented Feb 18, 2024

seems PR is closed, possible to re-open or in this case sending a new PR is preferred?

@phlax phlax reopened this Feb 19, 2024
@phlax phlax removed the stale stalebot believes this issue/PR has not been touched recently label Feb 19, 2024
@phlax
Copy link
Copy Markdown
Member

phlax commented Feb 19, 2024

@hwyuan reopened

@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Mar 20, 2024
@github-actions
Copy link
Copy Markdown

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot closed this Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants