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

Infinite loop calling .all, .first, .find and .query on a Contentful model #111

Closed
svpersteve opened this issue Jun 11, 2018 · 4 comments
Closed
Assignees
Labels

Comments

@svpersteve
Copy link

svpersteve commented Jun 11, 2018

Hi 😄

I'm trying to render articles from Contentful as a blog and both Rails console and Rails server are hanging when they try to call Article.all, Article.all.load or Article.first.

I've made a new Rails app to isolate what I'm doing and show you what I've done: https://github.com/svpersteve/contentful_articles

I'm not sure if this is a bug or if it's something I've done wrong.

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 11, 2018

Hi @svpersteve,

By any chance do you have a circular reference between your articles?

This may be the cause of your issue, if you do have a circular reference, there's a few different configuration options you can tweak depending on your needs.

If using caching, then on the initializer add:

config.options = {
  max_include_resolution_depth: THE_DEPTH_YOU_DESIRE # this is most likely 2 if you're linking articles with other articles
}

This will limit the default behaviour of generating a tree 20 levels deep (if reached, in case of circularity, this is reached extremely easy and is extremely slow) of all the relationships present in your content. By limiting this number to the specific value you need, the hydration process becomes extremely fast. This method allows for proper caching, at the cost of a bit slower hydration time and larger memory footprint.

If not using caching (this would generate a stack level too deep error if caching), instead add the following:

config.options = {
  reuse_entries: true
}

This will allow for an unlimited amount of nesting via reference reusing, but will prevent you from being able to cache properly. This method does use less memory and has a faster hydration time.

Hope this helps,

Cheers

@dlitvakb dlitvakb self-assigned this Jun 11, 2018
@svpersteve
Copy link
Author

Ah brilliant, thanks @dlitvakb! It works when I add max_include_resolution_depth.

Just while you're here, do you have an example of what a circular reference between articles on Contentful might look like? As far as I can see they're just articles with fields, I don't see how they're linking to each other.

@dlitvakb
Copy link
Contributor

Hey @svpersteve,

A circular reference looks something like this:

Say you have Entry A and Entry B, each of them have a reference field.
A has a link to B, B has a link to A.
Therefore, you can traverse the relationship tree like A -> B -> A -> B -> ... -> A indefinitely.

This can be happening on the 1st level of the relationship, or maybe you have A -> B -> C -> A or even a more complex relationship. But in any case, this is generating a circular structure, which may become extremely complex as when hydrating it it can exponentially grow.

Hope this explains the issue a bit better,

Cheers

@svpersteve
Copy link
Author

Ah, I think it must be the Twitter cards and/or article authors. Thanks for taking the time to explain @dlitvakb 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants