refactor: move execution context and timeout management into pool

This commit is contained in:
Meo597
2026-10-04 05:22:47 +08:00
parent e38794ed88
commit 399563b6d9
8 changed files with 441 additions and 172 deletions
+7 -10
View File
@@ -1,7 +1,6 @@
package dns
import (
"context"
"time"
"github.com/xtls/xray-core/common/errors"
@@ -13,7 +12,7 @@ import (
lua "github.com/yuin/gopher-lua"
)
const scriptExecutionTimeout = 10 * time.Second
const scriptExecutionTimeout = 6 * time.Second
type scriptEngine struct {
dns *DNS
@@ -26,8 +25,8 @@ func newScriptEngine(path string, server *DNS) (*scriptEngine, error) {
return nil, err
}
e := &scriptEngine{dns: server}
e.pool, err = luamgr.NewPool(server.ctx, program.NewStateFactory(
scriptExecutionTimeout,
e.pool, err = luamgr.NewPool(server.ctx, scriptExecutionTimeout, program.NewStateFactory(
scriptExecutionTimeout*20,
func(L *lua.LState) {
geodata.RegisterLua(L)
log.RegisterLua(L)
@@ -51,12 +50,10 @@ func (e *scriptEngine) close() {
}
func (e *scriptEngine) query(domain string, option dns.IPOption) (ips []net.IP, ttl uint32, err error) {
err = e.pool.WithState(func(L *lua.LState) error {
luaCtx, cancel := context.WithTimeout(e.pool.Context(), scriptExecutionTimeout)
defer cancel()
var luaErr error
ips, ttl, luaErr = e.dns.CallLuaHook(L, luaCtx, domain, option)
return luaErr
err = e.pool.WithState(nil, 0, func(L *lua.LState) error {
var hookErr error
ips, ttl, hookErr = e.dns.callLuaHook(L, domain, option)
return hookErr
})
return
}