Skip to content

Avoid queries for ServiceProvider with blank issuer#11798

Merged
mitchellhenke merged 3 commits intomainfrom
mitchellhenke/reduce-service-provider-queries
Jan 24, 2025
Merged

Avoid queries for ServiceProvider with blank issuer#11798
mitchellhenke merged 3 commits intomainfrom
mitchellhenke/reduce-service-provider-queries

Conversation

@mitchellhenke
Copy link
Copy Markdown
Contributor

@mitchellhenke mitchellhenke commented Jan 24, 2025

🛠 Summary of changes

Small performance improvement to avoid queries for ServiceProvider where issuer is not present. We see this sometimes around SP requests based on looking into NewRelic metrics and local testing to confirm. Also moves a frequently used Regexp into a constant to avoid having to re-create it every time.

@mitchellhenke mitchellhenke requested a review from a team January 24, 2025 17:33
Comment on lines 240 to 245
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.

Technically we could make this a single if without the else and have this assign as nil with working memoization, though I think maybe this a good instance of being overly explicit due to how confusing the conditional assignment can be in Ruby.

    @saml_request_service_provider =
      if current_issuer.present?
        ServiceProvider.find_by(issuer: current_issuer)
      end

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.

Ah, you're right, that hadn't occurred to me. I kind of prefer the explicitness though.

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.

i have a slight preference for the explicitness as well!

Copy link
Copy Markdown
Contributor

@Sgtpluck Sgtpluck left a comment

Choose a reason for hiding this comment

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

we could add a test that checks whether the find_by method is called, but as the value of the assignment doesn't change, that doesn't feel blocking to me

Mitchell Henke added 3 commits January 24, 2025 12:29
changelog: Internal, Performance, Avoid queries for ServiceProvider with blank issuer
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/reduce-service-provider-queries branch from 877c069 to 83e88d5 Compare January 24, 2025 18:30
@mitchellhenke
Copy link
Copy Markdown
Contributor Author

we could add a test that checks whether the find_by method is called, but as the value of the assignment doesn't change, that doesn't feel blocking to me

I had assumed we didn't have many tests around not querying, but we do have this one. I've added a test for the OIDC case, but the SAML concern doesn't have its own test file currently, so I opted against it.

@mitchellhenke mitchellhenke merged commit c311274 into main Jan 24, 2025
@mitchellhenke mitchellhenke deleted the mitchellhenke/reduce-service-provider-queries branch January 24, 2025 18:44
@zachmargolis
Copy link
Copy Markdown
Contributor

we could add a test that checks whether the find_by method is called, but as the value of the assignment doesn't change, that doesn't feel blocking to me

I had assumed we didn't have many tests around not querying, but we do have this one. I've added a test for the OIDC case, but the SAML concern doesn't have its own test file currently, so I opted against it.

We also have a QueryTracker class that can track queries by table (so say, if we switched methods to use like .where instead of .find_by it would still detect)

it 'does not make too many queries' do
queries = QueryTracker.track do
visit_idp_from_ial1_oidc_sp
end
expect(queries[:service_providers].count).to be <= 6
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants