Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpCache] Do not call terminate() on cache hit #46763

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

Merged
merged 1 commit into from
Jun 27, 2022

Conversation

Toflar
Copy link
Contributor

@Toflar Toflar commented Jun 24, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? yes
Tickets -
License MIT
Doc PR -

Currently, HttpCache always calls the kernel.terminate events, even if the response is coming from cache.
Why is this a problem?

kernel.terminate events are used to do things after the response has been sent to the visitor. At least that happens if you have support for fastcgi_finish_request().
For Contao for example, we use this to dispatch a message to update the search index but you can imagine a lot of stuff being done there, like sending e-mails etc. According to the docs, it says "perform some heavy action".

This means that currently, the system is basically always booted even when the response is coming from HttpCache because dispatching the TerminateEvent causes the container to be booted etc. which makes the system slower than it needs to be.

We don't need to update the search index when the response is coming from the cache because it already is up to date. And there are no e-mails or other "heavy actions" to perform because by definition, nothing could've happened as the system was not booted (or should not have been).

Also, imagine if you used a "real" reverse proxy like Varnish. There's no call to the back end either when there's a cache hit so it's actually an inconsistency. You cannot "perform some heavy action" there either. If you wanted to, it would have to be implemented in the proxy itself. So Varnish would need to trigger that heavy action. HttpCache should behave just the same.
You could e.g. use the EventDispatchingHttpCache from https://github.com/FriendsOfSymfony/FOSHttpCache, if you need something like that.

@carsonbot carsonbot added this to the 4.4 milestone Jun 24, 2022
@Toflar Toflar force-pushed the fix/no-terminate-call-on-cache-hit branch 2 times, most recently from 90348c9 to c7b7593 Compare June 24, 2022 09:19
@Toflar
Copy link
Contributor Author

Toflar commented Jun 24, 2022

Failing tests unrelated, imho.

@Toflar
Copy link
Contributor Author

Toflar commented Jun 24, 2022

Mentioning some users of HttpCache for their OSS projects: @dbu @alexander-schranz @chirimoya @andrerom

Copy link
Contributor

@alexander-schranz alexander-schranz left a comment

Choose a reason for hiding this comment

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

Make sense from my point of view!

@andrerom
Copy link
Contributor

I think it makes (a lot of) sense for eZ=>Ibexa as well, but as I'm not there anymore I'll ping @bdunogier who might know who is involved in this now

@bdunogier
Copy link

Thank you @andrerom ! I'll ping @adamwojs about it :)

@GromNaN
Copy link
Member

GromNaN commented Jun 24, 2022

That seems reasonable.
Regarding our policy, this is a performance improvement that may break someone's code if they need the terminate listeners to be called on cache hit. Your PR should target 6.2.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

this seems reasonable to me. the other kernel events are also not triggered on a cache hit (as the cache kernel does not forward the request to the wrapped kernel). having only the terminate event seems odd. and indeed, when a separate caching proxy is used, no terminate is triggered.

the code was added over a decade ago: 7efe4bc

i can see the fear of breaking some existing systems. i think most delayed tasks should not be needed after a cache hit, but if some sort of reporting is done in the framework it might care about cache hits too.

another option could be to use a flag whether to forward the termination event for cache hits that defaults to true for now, and maybe do a deprecation warning if it is not set to false, to make people aware that the behaviour will change.

without a flag, the behaviour can be changed by extending the HttpCache class and overwriting the terminate method to unconditionally forward the event.

@Toflar
Copy link
Contributor Author

Toflar commented Jun 24, 2022

To me, this is definitely a bug so I don't see why this would not need to be fixed in 4.4.
However, I understand that we cannot simply change the current behavior in a bugfix release so I've introduced a new configuration option. That allows everybody to fix it whenever they want.

Not sure if I should deprecate this as I did now. And if so, how the heck did we expect deprecations in 4.4? With the annotation still - so I would need to have two test methods and cannot use if - else in the same test as I do now?

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

regarding bug vs change, i think we are here in really a gray area.

there is nothing technically wrong with calling terminate. no security risk, no exceptions happening. you can't be sure that nobody would want the event to happen, so i don't think its correct to consider it a bug.

it is a significant waste of server resources even if it should not affect response times directly. the option seems justified to me, and given how unlikely it is to need the event, i think it makes sense to change the default in the next major version. (e.g. logging of requests should happen at webserver level, not in PHP)

given how long it has been like this, i would vote for stability in this case and only change the default in the next major.

@Toflar
Copy link
Contributor Author

Toflar commented Jun 24, 2022

I agree, that's why the default remains untouched now. However, I think we should be given the ability to fix it easily in all actively supported versions without having to override terminate() in every single project.

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

As explained in the comments, this must be done in 6.2.
In addition, performance optimizations are only for 6.2 as well.
Also, as it might break BC anyway, it cannot be part of 4.4 where stability is paramount.

@fabpot
Copy link
Member

fabpot commented Jun 25, 2022

Forgot to mention that I think this is indeed a good idea to have that in 6.2.

@Toflar
Copy link
Contributor Author

Toflar commented Jun 25, 2022

With the option there is no BC break. It just offers the users the ability to adjust their setups in 4.4 already.
I'm unsure what I need to do now :)
If you want this in 6.2 only, would you want me to still keep the option when I rebase or not? :)

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

With the option there is no BC break. It just offers the users the ability to adjust their setups in 4.4 already. I'm unsure what I need to do now :) If you want this in 6.2 only, would you want me to still keep the option when I rebase or not? :)

We really appreciate the investigations. The proposed solution is good for the next minor version (6.2). @fabpot listed the reasons it cannot be merged in an old branch:

  • it is not a security issue, not a functional bug
  • it is a new feature (performance optimization are feature)
  • it adds a new option
  • it adds a depreciation message (which is good for changing the default behavior in 7.0)

@Toflar Toflar changed the base branch from 4.4 to 6.2 June 25, 2022 20:21
@Toflar Toflar force-pushed the fix/no-terminate-call-on-cache-hit branch from fa6aeb5 to c730730 Compare June 25, 2022 20:21
@Toflar Toflar force-pushed the fix/no-terminate-call-on-cache-hit branch from c730730 to 662eb17 Compare June 25, 2022 20:28
@Toflar
Copy link
Contributor Author

Toflar commented Jun 25, 2022

My question was just if you want to still keep the option now that we've decided it should be 6.2 but I guess we should do that. PR is updated. Failing tests seem unrelated, the current 6.2 branch does not pass apparently :)

@nicolas-grekas nicolas-grekas removed this from the 4.4 milestone Jun 26, 2022
@nicolas-grekas nicolas-grekas added this to the 6.2 milestone Jun 26, 2022
@fabpot
Copy link
Member

fabpot commented Jun 27, 2022

Thank you @Toflar.

@fabpot fabpot merged commit 47c2da7 into symfony:6.2 Jun 27, 2022
@Toflar Toflar deleted the fix/no-terminate-call-on-cache-hit branch June 27, 2022 08:57
fabpot added a commit that referenced this pull request Sep 20, 2022
…e_on_cache_hit HttpCache option (wouterj)

This PR was merged into the 6.2 branch.

Discussion
----------

[FrameworkBundle] Add semantic config for new terminate_on_cache_hit HttpCache option

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | symfony/symfony-docs#16999

Adds the semantic configuration for the option introduced in Symfony 6.2 by #46763

Commits
-------

bb387e9 [FrameworkBundle] Add semantic config for new terminate_on_cache_hit HttpCache option
@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants