Table of Contents
2、Nginx API for lua 在lua-resty-core中怎么实现的
二、lua-resty-core 和nginx-lua-module API执行效率对比
三、为什么在openresty中要使用lua-resty-core这个lua模块
背景
最近在开发高铁CDN项目,由于我们的边缘节点的系统不是标准的centos(而是openwrt),所以在搭建lua-ngx开发环境中就遇到很多问题,最主要的问题是不能直接使用openresty这个平台。为了在nginx 中引入lua,我不得不考虑在ningx中把nginx-lua-module模块编译进来,此外,还需要在openwrt中编译出luajit。关于在openwrt系统中nginx-lua-module,请参考我的另外一篇博文openwrt中编译nginx-lua-module模块。
在openwrt中编译nginx-lua-module模块时,我就在想,nginx-lua-moudle里面已经有的lua API 为什么还要在openresty中的一个核心库lua-resty-core中重新实现一遍。我这里说的lua API 指的就是Nginx API for Lua,下面是一小部分API 截图。
其实,lua-nginx-module 提供的 API,并不是完全意义上的用 C 实现的。准确来说,是通过 C 实现,并通过 Lua CFunction 暴露出来的。而 lua-resty-core 提供的 API,也不是表面看上去那样用 Lua 实现的。准确来说,是通过在 lua-nginx-module 里面的以 *_lua_ffi_*
形式命名的 C 函数实现的,并在 lua-resty-core 里面通过 LuaJIT FFI 暴露出来的。所以其实两者都是 C 实现。两者的比较,就是 Lua CFunction 和 LuaJIT FFI 的比较。后续我会对他们的效率进行对比,好了,咱们直接进入正题:
一、lua-resty-core 是什么?
1、介绍
lua-resty-core是openresty的一个核心库,看过lua-resty-core 源码的同学,应该知道,lua-resty-core 有两个文件目录,分别是:ngx和core。 resty.core 里面是对nginx-lua-module已有接口的实现;resty.ngx里面是一些新的接口,并且后续新的接口都会放在resty.ngx里面。