Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to DomainContraint to prevent database call per request #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonsgreen
Copy link
Contributor

Reason for Change

Recently while watching this GoRails video on Domain and Subdomain Constraints I noticed that it was recommended to not do database requests in your routes constraints since it means that every request will need to make the request.

It occurred to me that we could avoid the requests entirely by instead matching against the hosts for the events side of cfp-app. It means the environment variable needs to be set for most of cfp app to be accessible but that should not change much once it is done.

Changes

  • Switches logic to match on non event host domain set in ENV variable
    rather than website domains stored in DB
  • Moves config/initializers/domain_constraint.rb to app/constraints folder
  • Set Rails configuration events_hosts value from EVENTS_HOSTS env
    variable with fallback defaults for non production environments
  • Uses lvh.me as domain to more accurately test domain constraint logic
    in feature specs

Comment on lines +1 to +11
module Features
module DomainHelpers
def with_domain(host)
original_host = Capybara.app_host
Capybara.app_host = "http://#{host}"
yield
ensure
Capybara.app_host = original_host
end
end
end
Copy link
Contributor

@codyjamesbrooks codyjamesbrooks Jul 9, 2022

Choose a reason for hiding this comment

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

This was a good idea.

- Switches logic to match on non event host domain set in ENV variable
  rather than website domains stored in DB
- Moves config/initializers/domain_constraint.rb to app/constraints folder
- Set Rails configuration events_hosts value from EVENTS_HOSTS env
  variable with fallback defaults for non production environments
- Uses lvh.me as domain to more accurately test domain constraint logic
  in feature specs
@jonsgreen jonsgreen force-pushed the flagrant/non-db-domain-match branch from 10e789b to 0cda55c Compare July 14, 2022 21:38
@adarsh adarsh temporarily deployed to cfp-app-flagrant-non-db-6gd4et July 14, 2022 21:45 Inactive
@adarsh
Copy link
Contributor

adarsh commented Jul 15, 2022

This seems like a fine solution although it moves the pattern-matching out of postgres and into memory, right? This might not be a huge deal if caching is utilized properly and given the low level of traffic. Consider not-fixing this (in spite of my previous concern).

@jonsgreen
Copy link
Contributor Author

@adarsh Yes, this would change the constraint so that the matching does not depend on a database query on every page request. It is true that caching would eliminate any performance concern for requests to the websites. The main issue would be for the speakers and organizers interacting with the rest of CFP app since that part cfp-app will not be cached. On the other hand, the performance penalty is small and maybe not as critical for those use cases.

I will say that another benefit from this PR is that that I think the integration specs for domains would be better implemented using 'lvh.me' since it better reflects what would happen in production.

I guess I felt responsible to at least try a different approach since the current one is considered to be a bad practice. I will leave it to you to decide whether to merge this or put it aside for now.

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.

3 participants