- 新增 contentstore 统一存放版本内容并支持定期 GC - 新增 jsexec 沙箱执行动态接口代码(带超时保护) - 补丁版本自动合并完整包,筛选仅含完整包的版本 - 创建版本写库失败时记录日志并返回具体错误
171 lines
3.5 KiB
Go
171 lines
3.5 KiB
Go
package jsexec
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"verification-platform-backend/internal/database"
|
|
"verification-platform-backend/internal/model"
|
|
|
|
"github.com/dop251/goja"
|
|
)
|
|
|
|
// 云端函数执行限制,配置存在 settings 表的 dynamic_code 分类下
|
|
const (
|
|
settingCategory = "dynamic_code"
|
|
// key 在 settings 表是全局唯一索引,加前缀避免和别的分类撞名
|
|
keyTimeout = "dynamic_code_timeout"
|
|
keyMaxConcurrent = "dynamic_code_max_concurrent"
|
|
|
|
DefaultTimeoutSeconds = 10
|
|
DefaultMaxConcurrent = 20
|
|
|
|
// 配置缓存时间,避免每次执行都查库
|
|
configCacheTTL = 10 * time.Second
|
|
)
|
|
|
|
// ErrTimeout 云端函数执行超时
|
|
var ErrTimeout = errors.New("dynamic code execution timed out")
|
|
|
|
// Config 云端函数执行限制
|
|
type Config struct {
|
|
TimeoutSeconds int
|
|
MaxConcurrent int
|
|
}
|
|
|
|
var (
|
|
configMu sync.RWMutex
|
|
cachedCfg Config
|
|
cachedAt time.Time
|
|
hasCached bool
|
|
)
|
|
|
|
// GetConfig 读取云端函数执行限制,读不到时使用默认值
|
|
func GetConfig() Config {
|
|
configMu.RLock()
|
|
if hasCached && time.Since(cachedAt) < configCacheTTL {
|
|
cfg := cachedCfg
|
|
configMu.RUnlock()
|
|
return cfg
|
|
}
|
|
configMu.RUnlock()
|
|
|
|
cfg := loadConfig()
|
|
|
|
configMu.Lock()
|
|
cachedCfg = cfg
|
|
cachedAt = time.Now()
|
|
hasCached = true
|
|
configMu.Unlock()
|
|
|
|
return cfg
|
|
}
|
|
|
|
// InvalidateConfigCache 配置变更后立即生效,不用等缓存过期
|
|
func InvalidateConfigCache() {
|
|
configMu.Lock()
|
|
hasCached = false
|
|
configMu.Unlock()
|
|
}
|
|
|
|
func loadConfig() Config {
|
|
cfg := Config{
|
|
TimeoutSeconds: DefaultTimeoutSeconds,
|
|
MaxConcurrent: DefaultMaxConcurrent,
|
|
}
|
|
|
|
var settings []model.Setting
|
|
if err := database.DB.Where("category = ?", settingCategory).Find(&settings).Error; err != nil {
|
|
return cfg
|
|
}
|
|
|
|
for _, s := range settings {
|
|
switch s.Key {
|
|
case keyTimeout:
|
|
cfg.TimeoutSeconds = parseInt(s.Value, DefaultTimeoutSeconds)
|
|
case keyMaxConcurrent:
|
|
cfg.MaxConcurrent = parseInt(s.Value, DefaultMaxConcurrent)
|
|
}
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func parseInt(value string, defaultVal int) int {
|
|
if value == "" {
|
|
return defaultVal
|
|
}
|
|
var result int
|
|
if _, err := fmt.Sscanf(value, "%d", &result); err != nil || result <= 0 {
|
|
return defaultVal
|
|
}
|
|
return result
|
|
}
|
|
|
|
type runResult struct {
|
|
value goja.Value
|
|
err error
|
|
}
|
|
|
|
// Run 在独立 goroutine 里执行代码,超过 timeout 后中断 VM。
|
|
// timeout <= 0 表示不限制,直接同步执行。
|
|
func Run(vm *goja.Runtime, code string, timeout time.Duration) (goja.Value, error) {
|
|
if timeout <= 0 {
|
|
return vm.RunString(code)
|
|
}
|
|
|
|
done := make(chan runResult, 1)
|
|
|
|
go func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
done <- runResult{nil, fmt.Errorf("代码执行异常: %v", r)}
|
|
}
|
|
}()
|
|
value, err := vm.RunString(code)
|
|
done <- runResult{value, err}
|
|
}()
|
|
|
|
timer := time.NewTimer(timeout)
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
case res := <-done:
|
|
return res.value, res.err
|
|
case <-timer.C:
|
|
// 中断后仍要等 goroutine 退出,脚本可能卡在原生调用里
|
|
vm.Interrupt(ErrTimeout)
|
|
<-done
|
|
return nil, ErrTimeout
|
|
}
|
|
}
|
|
|
|
var running int64
|
|
|
|
// Acquire 尝试占用一个执行槽位,达到并发上限时返回 false
|
|
func Acquire() bool {
|
|
limit := GetConfig().MaxConcurrent
|
|
for {
|
|
cur := atomic.LoadInt64(&running)
|
|
if cur >= int64(limit) {
|
|
return false
|
|
}
|
|
if atomic.CompareAndSwapInt64(&running, cur, cur+1) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
// Release 归还执行槽位
|
|
func Release() {
|
|
atomic.AddInt64(&running, -1)
|
|
}
|
|
|
|
// Running 当前正在执行的云端函数数量
|
|
func Running() int64 {
|
|
return atomic.LoadInt64(&running)
|
|
}
|