[wp-trac] [WordPress Trac] #41358: Shutdown hooks can significantly slow down REST API responses
WordPress Trac
noreply at wordpress.org
Tue Jul 18 16:36:59 UTC 2017
#41358: Shutdown hooks can significantly slow down REST API responses
-----------------------------------+-----------------------------
Reporter: mikejolley | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version:
Severity: normal | Keywords:
Focuses: rest-api, performance |
-----------------------------------+-----------------------------
If you have a site with some slow, maybe deferred actions hooked into
`shutdown`, these actions will slow the response from the REST API
unnecessarily.
To test, simply add the following then do a request to the WP Rest API:
{{{#!php
<?php
add_action( 'shutdown', 'delay_shutdown' );
function delay_shutdown() { sleep( 3); }
}}}
The response will not be sent out until all of these shutdown hooks are
completed. In our case, we found that some heavy sync functions ran on
shutdown would cause our API calls to WooCommerce API timed out, when
really the response could have been sent much earlier so these calls could
run afterwards.
This is semi-related to https://core.trac.wordpress.org/ticket/27122 in
that the fix could be along those lines.
This isn't a solution we feel should be merged into WooCommerce itself
because it would be more consistent to have it in WP itself, but see
https://github.com/woocommerce/woocommerce/pull/16158/files
So on shutdown, it ends any sessions, uses `fastcgi_finish_request` if
available, and falls back to flushing. The response is sent and received,
and shutdown continues on in the background. The response is sent before
the `sleep(3`) in this test case.
This could be applied to just REST API requests, or everywhere, depending
on everyones feedback. If REST API only, it may make sense to fire an
action after the response is sent to avoid `shutdown` hook altogether.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41358>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list