Skip to content

Commit

Permalink
Handle cached Lua middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasinsaurralde committed Aug 26, 2016
1 parent d5378a2 commit 9c61f7c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions coprocess_lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ package main
#include <lualib.h>
#include <lauxlib.h>
void LoadCachedMiddleware();
static void LuaInit() {
// TODO: Cache the middlewares.
}
static void LuaReload() {
}
static void LoadMiddleware(char* middleware_file, char* middleware_contents) {
}
static void LoadMiddlewareIntoState(lua_State* L, char* middleware_name, char* middleware_contents) {
luaL_dostring(L, middleware_contents);
}
static struct CoProcessMessage* LuaDispatchHook(struct CoProcessMessage* object) {
struct CoProcessMessage* outputObject = malloc(sizeof *outputObject);
Expand All @@ -36,6 +45,8 @@ static struct CoProcessMessage* LuaDispatchHook(struct CoProcessMessage* object)
luaL_openlibs(L);
luaL_dofile(L, "coprocess/lua/tyk/core.lua");
LoadCachedMiddleware(L);
lua_getglobal(L, "dispatch");
lua_pushlstring(L, object->p_data, object->length);
lua_pcall(L, 1, 1, 0);
Expand All @@ -60,7 +71,9 @@ static void LuaDispatchEvent(char* event_json) {
import "C"

import(
"io/ioutil"
"unsafe"
"path"

"github.com/TykTechnologies/tyk/coprocess"
"github.com/Sirupsen/logrus"
Expand All @@ -69,12 +82,17 @@ import(
// CoProcessName declares the driver name.
const CoProcessName string = "lua"

const MiddlewareBasePath = "middleware/lua"

// MessageType sets the default message type.
var MessageType = coprocess.JsonMessage

var gMiddlewareCache *map[string]string

// LuaDispatcher implements a coprocess.Dispatcher
type LuaDispatcher struct {
coprocess.Dispatcher
MiddlewareCache map[string]string
}

// Dispatch takes a CoProcessMessage and sends it to the CP.
Expand All @@ -89,9 +107,43 @@ func (d *LuaDispatcher) Dispatch(objectPtr unsafe.Pointer) unsafe.Pointer {
}

func (d *LuaDispatcher) Reload() {
files, _ := ioutil.ReadDir("./middleware/lua")
if d.MiddlewareCache == nil {
d.MiddlewareCache = make(map[string]string, len(files))
gMiddlewareCache = &d.MiddlewareCache
} else {
for k := range d.MiddlewareCache {
delete(d.MiddlewareCache, k)
}
}

for _, f := range files {
middlewarePath := path.Join(MiddlewareBasePath, f.Name())
contents, err := ioutil.ReadFile(middlewarePath)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).Error("Failed to read middleware file: ", err)
}
d.MiddlewareCache[f.Name()] = string(contents)
}

C.LuaReload()
}

//export LoadCachedMiddleware
func LoadCachedMiddleware(luaState unsafe.Pointer) {
for middlewareName, middlewareContents := range *gMiddlewareCache {
var cMiddlewareName, cMiddlewareContents *C.char
cMiddlewareName = C.CString(middlewareName)
cMiddlewareContents = C.CString(middlewareContents)
C.LoadMiddlewareIntoState(luaState, cMiddlewareName, cMiddlewareContents);
C.free(unsafe.Pointer(cMiddlewareName))
C.free(unsafe.Pointer(cMiddlewareContents))
}
return
}

func (d *LuaDispatcher) DispatchEvent(eventJSON []byte) {
var CEventJSON *C.char
CEventJSON = C.CString(string(eventJSON))
Expand All @@ -111,6 +163,8 @@ func NewCoProcessDispatcher() (dispatcher coprocess.Dispatcher, err error) {

dispatcher, err = &LuaDispatcher{}, nil

dispatcher.Reload()

if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
Expand Down

0 comments on commit 9c61f7c

Please sign in to comment.