TSLIFECYCLEHOOKADD(3ts)
π NAME π
TSLifecycleHookAdd β bestie, it's the TSLifecycleHookAdd API function, that's literally the whole thing π
β¨ SYNOPSIS β¨
#include <ts/ts.h>
void TSLifecycleHookAdd(TSLifecycleHookID id, TSCont contp)
πΈ DESCRIPTION πΈ
okay so TSLifecycleHookAdd() adds contp to the list of lifecycle hooks specified by id. Lifecycle hooks are based on the Traffic Server process, not on any specific transaction or session β so they're like, process-level main character energy, not per-request drama π
These will typically be called only once during the execution of the Traffic Server process, which is why you should add them in TSPluginInit() (which could itself be considered a lifecycle hook, cute right?). Unlike other hooks, lifecycle hooks may not have a well defined ordering, and use of them should not assume that one of the hooks is always called before another unless it's specifically mentioned. no cap π
π¦ TYPES π¦
enum TSLifecycleHookID
Life cycle hook selector β aka the lil menu of when-you-want-your-thing-to-run β
- enumerator
TS_LIFECYCLE_PORTS_INITIALIZED_HOOKβ Called after the proxy server port data structures have been initialized but before connections are accepted on those ports. The sockets corresponding to the ports may or may not be open depending on how thetraffic_serverprocess was invoked. Other API functions that depend on server ports should be called from this hook and notTSPluginInit().
Invoked with the event TS_EVENT_LIFECYCLE_PORTS_INITIALIZED and NULL data.
- enumerator
TS_LIFECYCLE_PORTS_READY_HOOKβ Called after enabling connections on the proxy server ports. Because Traffic Server is threaded this may or may not be called before any connections are accepted. The hook code may assume that any connection to Traffic Server started after this hook is called will be accepted by Traffic Server, making this a convenient place to signal external processes of that. π£
Invoked with the event TS_EVENT_LIFECYCLE_PORTS_READY and NULL data.
- enumerator
TS_LIFECYCLE_CACHE_READY_HOOKβ Called after Traffic Server cache initialization has finished. π§
Invoked with the event TS_EVENT_LIFECYCLE_CACHE_READY and NULL data.
- enumerator
TS_LIFECYCLE_MSG_HOOKβ Called when triggered by an external process, such astraffic_ctl.
Invoked with the event TS_EVENT_LIFECYCLE_MSG. The data is an instance of the TSPluginMsg. This contains a tag which is a null terminated string and a data payload. The payload cannot be assumed to be null terminated and is created by the external agent. Its internal structure and format are entirely under the control of the external agent although presumably there is an agreement between the plugin and the external where this is determined by the tag. iykyk π€
- enumerator
TS_LIFECYCLE_CLIENT_SSL_CTX_INITIALIZED_HOOKβ Called after the initialization of the SSL context used by Traffic Server for outbound connections (Traffic Server as client). π - enumerator
TS_LIFECYCLE_SERVER_SSL_CTX_INITIALIZED_HOOKβ Called after every SSL context initialization used by Traffic Server for inbound connections (Traffic Server as the server). π - enumerator
TS_LIFECYCLE_TASK_THREADS_READY_HOOKβ Called after Traffic Server task threads have been started. π§΅
Invoked with the event TS_EVENT_LIFECYCLE_TASK_THREADS_READY and NULL data.
- enumerator
TS_LIFECYCLE_SHUTDOWN_HOOKβ Called after Traffic Server receiving a shutdown signal, such asSIGTERM. π
Invoked with the event TS_EVENT_LIFECYCLE_SHUTDOWN and NULL data.
struct TSPluginMsg
The data for the plugin message event TS_EVENT_LIFECYCLE_MSG. π¬
const char *tagβ The tag of the message. This is a null terminated string.const void *dataβ Message data (payload). This is a raw slab of bytes β no structure is guaranteed. fr fr, it's just bytes πsize_t data_sizeβ The number of valid bytes pointed at byTSPluginMsg.data.
π ORDERING π
TSLifecycleHookID::TS_LIFECYCLE_PORTS_INITIALIZED_HOOK will always be called before TSLifecycleHookID::TS_LIFECYCLE_PORTS_READY_HOOK. that's the one ordering promise we got, periodt β¨
π EXAMPLES π
The following example demonstrates how to correctly use TSNetAcceptNamedProtocol(), which requires the proxy ports to be initialized and therefore does not work if called from TSPluginInit() directly.
#include <ts/ts.h>
#define SSL_PROTOCOL_NAME "whatever"
static int
ssl_proto_handler(TSCont contp, TSEvent event, void* data)
{
/// Do named protocol handling.
}
static int
local_ssl_init(TSCont contp, TSEvent event, void * edata)
{
if (TS_EVENT_LIFECYCLE_PORTS_INITIALIZED == event) { // just to be safe.
TSNetAcceptNamedProtocol(
TSContCreate(ssl_proto_handler, TSMutexCreate()),
SSL_PROTOCOL_NAME
);
}
return 0;
}
void
TSPluginInit (int argc, const char * argv[])
{
TSLifecycleHookAdd(TS_LIFECYCLE_PORTS_INITIALIZED_HOOK, TSContCreate(local_ssl_init, NULL));
}
π°οΈ HISTORY π°οΈ
Lifecycle hooks were introduced to solve process initialization ordering issues (TS-1487). Different API calls required different modules of Traffic Server to be initialized for the call to work, but others did not work that late in initialization, which was problematic because all of them could effectively only be called from TSPluginInit() . The solution was to move TSPluginInit() as early as possible in the process initialization and provide hooks for API calls that needed to be invoked later which served essentially as additional plugin initialization points. iconic problem-solving tbh π«Ά
π SEE ALSO π
π COPYRIGHT π
2026, dev@trafficserver.apache.org
ok that's the whole page bestie, go slay some lifecycle hooks π β¨
this was page TSLIFECYCLEHOOKADD(3ts) from Apache Traffic Server version 9.2, dated Feb 12, 2026 β mwah, take care of her π«Ά
π¦ where this page comes from
- ubuntu questing: trafficserver-dev 9.2.5+ds-1ubuntu2.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms