Throttle reports site load on a sliding scale of 0-10. Zero represents a site currently under no load, while 10 represents the maximum load the site can manage. If set up as default, this top of the scale actually represents the maximum allowed load without the site exceeding bandwidth.

Rather than check the current throttle level & compare that against internal options settings, plugins can instead set throttle triggers using the Throttle API. This allows all throttle checking and configuration to be kept out of the plugin, reducing the changes required to make a plugin throttle enabled.

In practise it should be possible to make a plugin Throttle compatible with only a few extra lines of code. This section covers setting up your plugins to work with WordPress Throttle and for ensuring backward compatibility for those who do not have the plugin installed.

These instructions assume you have the Throttle already installed. For installation following the instructions on the main Throttle pages.

Throttle-Enabling Your Plugins

There are two steps to adding Throttle capability to your plugins: Registering the Throttle, then checking it’s status.

Registering A Throttle

Throttle uses a system of named throttles to indicate when or when not to perform events. A plugin can define as many named throttles and check them as frequently as required.

add_throttle('Most Recent Posts',5);

In the example above ‘Most Recenty Posts’ is the name for this throttle. The second parameter, ‘5’ (which can be set from 0-10) is optional and specifies the default level at which this throttle will become active.

Remember that plugins should act to reduce load when the current load is above the set value. In most cases this means stopping an action once the level is reached. Using the example above a recent-posts plugin should stop some action when site load reaches Level 5.

Checking Status

Within plugins current Throttle status should be checked using the throttled(); function which returns true when site load has reached the throttle level, and false when site load is below this value, or the throttle is disabled.

Because normal usage is switch off functions of a plugin under heavy load, the standard way to use this check is with a negative operator. For example the following code would only output the most recent posts when the site load was under Level 5.

if(!throttle('Most Recent Posts')){
/* Output most recent Posts */
} else {
/* Output message */
}

Additional Functions

Additionally there are 2 functions for setting and checking the current status of your sites current named Throttle triggers.

get_throttle(throttle_name);
set_throttle(throttle_name,throttle_level);

Both these plugins work as would be expected – the first to get the current level of a named throttle, the second to set the current level. It is worth noting that under normal situations a plugin should not need to use either of these functions as all configuration is carried out by the Throttle management pages.

Setting Up a Plugin

Of course, in production installations you cannot guarantee that the Throttle plugin will be included in all setups. To ensure your plugin continues to function and output properly, the simplest method is to add a dud throttled() function where one is not currently installed:


if(!function_exists('throttled')){function throttled($null){return true;}}

Add this code to the initialisation section of your plugin/theme. Now when the throttle is not already installed the above code will create a dud response indicating “No Load”. The site will behave as though the Throttle is installed and at it’s lowest setting (default behaviour).

In Action

A working version of the Throttle code is available on the Extras page.

Advertisements

2 Responses to “Plugin Developers”

  1. max34 said

    Can you show the code of some example plugin already compatible with throttle?

  2. max34: You can download a demo Throttle-enabled plugin on the Extras page. I’ll also be modifying my other plugins to use the Throttle code where possible over time.

    If you need any more help let me know. Thanks for the feedback!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: