~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to plugins/experimental/lua/remap.cc

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-05-09 01:00:04 UTC
  • mto: (1.1.11) (5.3.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20130509010004-9fqq9n0adseg3f8w
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include <ts/ts.h>
20
20
#include <ts/remap.h>
21
 
#include <unistd.h>
22
 
#include <string.h>
23
21
#include "lapi.h"
24
22
#include "lutil.h"
25
 
 
26
 
static TSReturnCode
27
 
LuaPluginRelease(lua_State * lua)
28
 
{
29
 
  lua_getglobal(lua, "release");
30
 
  if (lua_isnil(lua, -1)) {
31
 
    // No "release" callback.
32
 
    return TS_SUCCESS;
33
 
  }
34
 
 
35
 
  if (lua_pcall(lua, 0, 0, 0) != 0) {
36
 
    LuaLogDebug("release failed: %s", lua_tostring(lua, -1));
37
 
    lua_pop(lua, 1);
38
 
  }
39
 
 
40
 
  lua_close(lua);
41
 
  return TS_SUCCESS;
42
 
}
 
23
#include "state.h"
 
24
#include <unistd.h>
 
25
#include <pthread.h>
 
26
 
 
27
static pthread_mutex_t PluginInstanceLock = PTHREAD_MUTEX_INITIALIZER;
43
28
 
44
29
static TSRemapStatus
45
30
LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
68
53
  return rq->status;
69
54
}
70
55
 
71
 
void
72
 
TSRemapDeleteInstance(void * ih)
73
 
{
74
 
  lua_State * lua = (lua_State *)ih;
75
 
 
76
 
  if (lua) {
77
 
    LuaPluginRelease(lua);
78
 
    lua_close(lua);
79
 
  }
80
 
}
81
 
 
82
56
TSReturnCode
83
57
TSRemapInit(TSRemapInterface * api_info, char * errbuf, int errbuf_size)
84
58
{
85
59
  LuaLogDebug("loading lua plugin");
 
60
 
 
61
  // Allocate a TSHttpTxn argument index for handling per-transaction hooks.
 
62
  TSReleaseAssert(TSHttpArgIndexReserve("lua", "lua", &LuaHttpArgIndex) == TS_SUCCESS);
 
63
 
86
64
  return TS_SUCCESS;
87
65
}
88
66
 
89
67
TSReturnCode
90
68
TSRemapNewInstance(int argc, char * argv[], void ** ih, char * errbuf, int errsz)
91
69
{
92
 
  LuaPluginState * remap;
93
 
  lua_State * lua;
94
 
 
95
 
  // Copy the plugin arguments so that we can use them to allocate a per-thread Lua state. It would be cleaner
96
 
  // to clone a Lua state, but there's no built-in way to do that, and to implement that ourselves would require
97
 
  // locking the template state (we need to manipulate the stack to copy values out).
98
 
  remap = tsnew<LuaPluginState>();
99
 
  remap->init((unsigned)argc, (const char **)argv);
100
 
 
101
 
  // Test whether we can successfully load the Lua program.
102
 
  lua = LuaPluginNewState(remap);
103
 
  if (!lua) {
104
 
    tsdelete(remap);
105
 
    return TS_ERROR;
106
 
  }
107
 
 
108
 
  *ih = remap;
 
70
  instanceid_t instanceid;
 
71
 
 
72
  pthread_mutex_lock(&PluginInstanceLock);
 
73
 
 
74
  // Register a new Lua plugin instance, skipping the first two arguments (which are the remap URLs).
 
75
  instanceid = LuaPluginRegister((unsigned)argc - 2, (const char **)argv + 2);
 
76
  *ih = (void *)(intptr_t)instanceid;
 
77
 
 
78
  pthread_mutex_unlock(&PluginInstanceLock);
109
79
  return TS_SUCCESS;
110
80
}
111
81
 
 
82
void
 
83
TSRemapDeleteInstance(void * ih)
 
84
{
 
85
  instanceid_t instanceid = (intptr_t)ih;
 
86
 
 
87
  pthread_mutex_lock(&PluginInstanceLock);
 
88
  LuaPluginUnregister(instanceid);
 
89
  pthread_mutex_unlock(&PluginInstanceLock);
 
90
}
 
91
 
112
92
TSRemapStatus
113
93
TSRemapDoRemap(void * ih, TSHttpTxn txn, TSRemapRequestInfo * rri)
114
94
{
115
 
  LuaThreadInstance * lthread;
116
 
 
117
 
  // Find or clone the per-thread Lua state.
118
 
  lthread = LuaGetThreadInstance();
119
 
  if (!lthread) {
120
 
    LuaPluginState * lps;
121
 
 
122
 
    lps = (LuaPluginState *)ih;
123
 
    lthread = tsnew<LuaThreadInstance>();
124
 
 
125
 
    LuaLogDebug("allocating new Lua state on thread 0x%llx", (unsigned long long)pthread_self());
126
 
    lthread->lua = LuaPluginNewState(lps);
127
 
    LuaSetThreadInstance(lthread);
128
 
  }
129
 
 
130
 
  return LuaPluginRemap(lthread->lua, txn, rri);
 
95
  ScopedLuaState lstate((intptr_t)ih);
 
96
 
 
97
  TSReleaseAssert(lstate);
 
98
  return LuaPluginRemap(lstate->lua, txn, rri);
131
99
}