<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>stream_lua :: NJET文档</title>
    <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/index.html</link>
    <description>stream模块的lua功能，基本等同于http模块的lua配置，需要在stream块中配置lua_ssl_certificate和lua_ssl_certificate_key指令。&#xA;具体配置可以参考lua模块</description>
    <generator>Hugo</generator>
    <language>zh</language>
    <lastBuildDate>Tue, 21 Oct 2025 14:36:16 +0800</lastBuildDate>
    <atom:link href="https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Stream模块lua深层清理</title>
      <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/stream_lua_clean/index.html</link>
      <pubDate>Tue, 21 Oct 2025 14:36:16 +0800</pubDate>
      <guid>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/stream_lua_clean/index.html</guid>
      <description>1. 功能描述 NJet 集成了Lua 的运行环境，在不配置 lua_code_cache off; 的情况下，worker收到请求时，会从context中获取已经创建的Lua VM (第一次执行Lua相关代码时创建）。Lua VM中, package.loaded 是一个核心的 Lua 表，它扮演着 Lua 模块缓存的角色。当使用 require(“module_name”) 加载一个 Lua 模块时，Lua 解释器会首先检查 package.loaded 表中是否已经存在名为 module_name 的模块。如果存在，它会直接返回已缓存的模块值；如果不存在，它才会去文件系统（根据 package.path）查找、加载并执行该模块文件，然后将其返回值存储到 package.loaded 中，供后续调用。&#xA;在配置了多个 worker 进程时， 每个worker 进程都拥有自己独立的 LuaJIT 虚拟机实例 (Lua State)。这意味着每个 worker 进程的 Lua 虚拟机都有自己独立的 package.loaded 表，它们之间互不影响。有可能造成部分worker进程缓存了模块，部分进程没有对模块进行缓存。&#xA;当更新了磁盘上的 Lua 模块文件时，例如 mylib.lua, package.loaded 中已经缓存了旧版本的 mylib的worker进程将执行旧版的代码，而未缓存该模块的部分worker进程收到请求时将直接使用新版本，这样照成了各个worker的行为不一致。并且那些被间接 require 的模块（即主 Lua 脚本 require 了 A，而 A 又 require 了 B），Lua 自身也没有机制去追踪这种深层次的依赖关系并自动刷新。&#xA;2. 依赖模块 stream模块lua深层清理依赖于stream动态lua功能：&#xA;load_module modules/njt_stream_lua_module.so; load_module modules/njt_stream_dyn_lua_module.so; 特殊说明：njt_stream_lua_module.so要在njt_stream_lua_module.so上面。</description>
    </item>
    <item>
      <title>stream动态lua配置</title>
      <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/stream_dyn_lua/index.html</link>
      <pubDate>Thu, 16 Oct 2025 14:59:33 +0800</pubDate>
      <guid>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/stream/stream_lua/stream_dyn_lua/index.html</guid>
      <description>1. 功能描述 该模块允许通过 /api/v1/config/stream_lua 动态配置接口, 动态设置stream模块的balancer_by和content_by的内容。&#xA;其中balancer_by用于配置在upstream块内，content_by配置在server块内。&#xA;2. 依赖模块 stream动态lua功能依赖模块：&#xA;load_module modules/njt_stream_lua_module.so; load_module modules/njt_stream_dyn_lua_module.so; 特殊说明：njt_stream_lua_module.so要在njt_stream_lua_module.so上面。&#xA;3. 配置说明 load_module modules/njt_stream_lua_module.so; load_module modules/njt_stream_dyn_lua_module.so; stream { ... server { lua_package_path &#34;$prefix/lualib/lib/?.lua;/usr/local/njet/modules/?.lua;;&#34;; lua_package_cpath &#34;$prefix/lualib/clib/?.so;;&#34;; ... } } 4. 调用样例 4.1 API说明 查询接口：&#xA;GET http://IP+port/api/v1/config/stream_dyn_lua 修改接口：&#xA;PUT http://IP+port/api/v1/config/stream_dyn_lua 4.2 通过PUT接口添加content_by 发送&#xA;curl -X PUT http://127.0.0.1:8081/api/v1/config/stream_dyn_lua -d&#39;{ &#34;upstreams&#34;: [ { &#34;name&#34;: &#34;backend_s1&#34;, &#34;balancer_by&#34;: &#34;&#34; }, { &#34;name&#34;: &#34;backend_s2&#34;, &#34;balancer_by&#34;: &#34;&#34; }, { &#34;name&#34;: &#34;backend_s3&#34;, &#34;balancer_by&#34;: &#34;&#34; } ], &#34;servers&#34;: [ { &#34;listens&#34;: [ &#34;0.0.0.0:22224&#34; ], &#34;serverNames&#34;: [ &#34;&#34; ], &#34;lua&#34;: { &#34;content_by&#34;: &#34;njt.say(\&#34;test lua ok\&#34;)&#34; } } ] }&#39; 返回</description>
    </item>
  </channel>
</rss>