Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions doc/admin-guide/plugins/lua.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@
Lua Plugin
**********

This module embeds Lua, via the standard Lua 5.1 interpreter, into |ATS|. With
This module embeds Lua, via the LuaJIT engine (>2.0.4), into |ATS|. With
this module, we can implement ATS plugin by writing Lua script instead of C
code. Lua code executed using this module can be 100% non-blocking because the
powerful Lua coroutines have been integrated into the ATS event model.

Synopsis
========
Installation
============

This plugin is only built if LuaJIT (>2.0.4) is installed. The configure option

::

--with-luajit=<path to luajit prefix>

can be used to specify a LuaJIT install. Otherwise, configure will use pkg-config to find a viable installation.

Example Scripts
===============

**test_hdr.lua**

Expand Down Expand Up @@ -64,20 +75,24 @@ Synopsis
return 0
end


Installation
============

This plugin is only built if LuaJIT (>2.0.4) is installed. The configure option
**test_global_hdr.lua**

::

--with-luajit=<path to luajit prefix>
function send_response()
ts.client_response.header['Rhost'] = ts.ctx['rhost']
return 0
end

can be used to specify a LuaJIT install. Otherwise, configure will use pkg-config to find a viable installation.
function do_global_read_request()
local req_host = ts.client_request.header.Host
ts.ctx['rhost'] = string.reverse(req_host)
ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response)
end

Configuration
=============

Usage with Example Scripts
==========================

This module acts as remap plugin of Traffic Server, so we should realize 'do_remap' or 'do_os_response' function in each
lua script. The path referencing a file with the lua script can be relative to the configuration directory or an absolute
Expand Down Expand Up @@ -115,6 +130,10 @@ We can write this in plugin.config:

tslua.so /etc/trafficserver/script/test_global_hdr.lua


Configuration for number of Lua states
======================================

We can also define the number of Lua states to be used for the plugin. If it is used as global plugin, we can write the
following in plugin.config

Expand All @@ -138,6 +157,16 @@ adding a configuration option to records.config.

Any per plugin --states value overrides this default value but must be less than or equal to this value. This setting is not reloadable since it must be applied when all the lua states are first initialized.

Configuration for JIT mode
==========================

We can also turn off JIT mode for LuaJIT when it is acting as global plugin for Traffic Server. The default is on (1). We can write this in plugin.config to turn off JIT

::

tslua.so --jit=0 /etc/trafficserver/script/test_global_hdr.lua


Profiling
=========

Expand Down
16 changes: 16 additions & 0 deletions plugins/lua/ts_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <pthread.h>

#include "ts_lua_util.h"
#include "luajit.h"

#define TS_LUA_MAX_STATE_COUNT 256

Expand Down Expand Up @@ -773,9 +774,11 @@ TSPluginInit(int argc, const char *argv[])

int states = ts_lua_max_state_count;

int jit = 1;
int reload = 0;
static const struct option longopt[] = {
{"states", required_argument, 0, 's'},
{"jit", required_argument, 0, 'j'},
{"enable-reload", no_argument, 0, 'r'},
{0, 0, 0, 0},
};
Expand All @@ -789,6 +792,19 @@ TSPluginInit(int argc, const char *argv[])
states = atoi(optarg);
// set state
break;
case 'j':
jit = atoi(optarg);
if (jit == 0) {
TSDebug(TS_LUA_DEBUG_TAG, "[%s] disable JIT mode", __FUNCTION__);
for (int index = 0; index < ts_lua_max_state_count; ++index) {
ts_lua_main_ctx *const main_ctx = (ts_lua_g_main_ctx_array + index);
lua_State *const lstate = main_ctx->lua;
if (luaJIT_setmode(lstate, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF) == 0) {
TSError("[ts_lua][%s] Failed to disable JIT mode", __FUNCTION__);
}
}
}
break;
case 'r':
reload = 1;
TSDebug(TS_LUA_DEBUG_TAG, "[%s] enable global plugin reload [%d]", __FUNCTION__, reload);
Expand Down