Add ts.run_plugin() Lua API for dynamic remap plugin execution#7
Draft
Add ts.run_plugin() Lua API for dynamic remap plugin execution#7
Conversation
Co-authored-by: shukitchan <946001+shukitchan@users.noreply.github.com>
…mize config reads
Copilot
AI
changed the title
[WIP] Add run-plugin functionality to Lua plugin
Add ts.run_plugin() Lua API for dynamic remap plugin execution
Feb 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
ts.run_plugin()Lua API enabling conditional plugin loading at runtime, matching header_rewrite'srun-pluginoperator functionality.Implementation
New files:
ts_lua_run_plugin.{h,cc}- Core API implementationPluginFactory::getRemapPlugin()withElevateAccessstd::quotedfor quoted string supportIntegration:
ts_lua_common.h- Addedloaded_pluginslinked list tots_lua_instance_confts_lua_util.cc- Registered API ints_lua_inject_ts_api()ts_lua.cc- PluginFactory initialization inTSRemapInit(), cleanup inTSRemapDeleteInstance()CMakeLists.txt- Added to buildMemory Management
RemapPluginInst::done()called for reference countingTSRemapRequestInfo::{mapFromUrl,mapToUrl}freed after sync plugin initAPI
Parameters:
plugin_name(string, required): Plugin path/nameargs(string, optional): Plugin argumentsReturns: boolean success/failure
Constraints: Remap context only (validates
rri != nullptr)Original prompt
Add run-plugin functionality to Lua plugin
Summary
Add a
ts.run_plugin()Lua API function that allows dynamically loading and executing remap plugins at runtime, similar to therun-pluginoperator added to the header_rewrite plugin in PR apache#11320.Background
PR apache#11320 added a
run-pluginoperator to the header_rewrite plugin that allows conditionally loading and running other plugins. This feature is useful for scenarios where you want to enable/disable plugins based on request conditions.Example use case from header_rewrite:
Objective
Implement similar functionality for the Lua plugin, allowing Lua scripts to dynamically load and execute remap plugins.
Implementation Details
1. Create
plugins/lua/ts_lua_run_plugin.ccThis file should implement:
ts_lua_run_plugin(lua_State *L)- Main Lua C function that handles the API callts_lua_load_plugin()- Loads a plugin using the plugin factoryts_lua_find_loaded_plugin()- Finds already loaded plugins to avoid reloadingts_lua_inject_run_plugin_api()- Registers the API with LuaKey implementation requirements:
plugin_factory.getRemapPlugin()approach as header_rewriteElevateAccess)rri != nullptr)doRemap()function with transaction and remap info2. Create
plugins/lua/ts_lua_run_plugin.hHeader file defining:
ts_lua_loaded_pluginstructure to track loaded plugins3. Update
plugins/lua/ts_lua_common.hAdd to the
ts_lua_instance_confstructure:4. Update Lua API initialization
Find where the Lua
tstable is initialized and add:This is likely in a file like
ts_lua_client_request.cor similar where otherts.*APIs are registered.5. Update build system
Add
ts_lua_run_plugin.ccto the appropriate CMakeLists.txt or Makefile for the lua plugin.6. Add includes
The implementation will need these includes:
And reference to the plugin factory:
extern PluginFactory plugin_factory;API Specification
Lua Function Signature
Parameters:
plugin_name(required): Name or path of the plugin to load (e.g., "rate_limit.so")args(optional): String of arguments to pass to the plugin (e.g., "--limit=300 --error=429")Returns:
trueif the plugin was loaded and executed successfullyfalseif loading or execution failedErrors:
Example Usage
Reference Implementation
Base the implementation on the
OperatorRunPluginclass from:plugins/header_rewrite/operators.cclines 1273-1342plugins/header_rewrite/operators.h(OperatorRunPlugin class definition)Key patterns to follow:
Testing Considerations
Notes
This pull request was created from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.