The Traffic Server API enables you to create plugins, using the C programming language, that customize the behavior of your Traffic Server installation. This chapter contains the following sections:
Traffic Server enables sophisticated caching and processing of web-related traffic, such as DNS and HTTP requests and responses.
Traffic Server itself consists of an event-driven loop that can be simplified as follows:
for (;;) {
event = get_next_event();
handle_event (event);
}
You compile your plugin source code to create a shared library that Traffic Server loads when it is started. Your plugin contains callback functions that are registered for specific Traffic Server events. When Traffic Server needs to process an event, it invokes any and all call-back functions you’ve registered for that event type.
Caution
Since plugins add object code to Traffic Server, programming errors in a plugin can have serious implications. Bugs in your plugin, such as an out-of-range pointer, can cause Traffic Server processes to crash and may ultimately result in unpredictable behavior.
Plugin Process {#PluginProcess}
Plugin Process
Possible uses for plugins include the following:
Some examples of plugins include:
The figure below, Possible Traffic Server Plugins, illustrates several types of plugins.
Possible Traffic Server Plugins {#possibleTSplugins}
Possible Traffic Server Plugins
You can find basic examples for many plugins in the SDK sample code:
When Traffic Server is first started, it consults the plugin.config file to determine the names of all shared plugin libraries that need to be loaded. The plugin.config file also defines arguments that are to be passed to each plugin’s initialization function, TSPluginInit. The records.config file defines the path to each plugin shared library, as described in Specify the Plugin’s Location.
Note
The path for each of these files is <root_dir>/config/, where <root_dir> is where you installed Traffic Server.
The sample plugin.config file below contains a comment line, a blank line, and two plugin configurations:
# This is a comment line.
my-plugin.so junk.example.com trash.example.org garbage.example.edu
some-plugin.so arg1 arg2 $proxy.config.http.cache.on
Each plugin configuration in the plugin.config file resembles a UNIX or DOS shell command; each line in plugin.config cannot exceed 1023 characters.
The first plugin configuration is for a plugin named my-plugin.so. It contains three arguments that are to be passed to that plugin’s initialization routine. The second configuration is for a plugin named some-plugin.so; it contains three arguments. The last argument, ``$proxy.config.http.cache.on``, is actually a configuration variable. Traffic Server will look up the specified configuration variable and substitute its value.
Plugins with global variables should not appear more than once in plugin.config. For example, if you enter:
add-header.so header1
add-header.so header2
then the second global variable, header2, will be used for both instances. A simple workaround is to give different names to different instances of the same plugin. For example:
cp add-header.so add-header1.so
cp add-header.so add-header2.so
These entries will produce the desired result below:
add-header1.so header1
add-header2.so header2
Each plugin must define an initialization function named TSPluginInit that Traffic Server invokes when the plugin is loaded. The TSPluginInit function is commonly used to read configuration information and register hooks for event notification.
The TSPluginInit function has two arguments:
See TSPluginInit for details about TSPluginInit.