Initial commit (code only without large binaries)
This commit is contained in:
79
EdgeNode/internal/nodes/script_plus.go
Normal file
79
EdgeNode/internal/nodes/script_plus.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build script
|
||||
|
||||
package nodes
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/js"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SharedJSPool *js.IsolatePool
|
||||
var commonScriptsChangesChan = make(chan bool, 8)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
var createPool = func() {
|
||||
pool, err := js.NewAutoIsolatePool()
|
||||
if err != nil {
|
||||
remotelogs.Error("SCRIPT", "can not create isolate pool: "+err.Error())
|
||||
} else {
|
||||
remotelogs.Println("SCRIPT", "loaded "+types.String(pool.MaxSize())+" isolates")
|
||||
|
||||
var oldPool = SharedJSPool
|
||||
SharedJSPool = pool
|
||||
|
||||
// dispose old pool
|
||||
if oldPool != nil {
|
||||
var timeout = time.NewTimer(10 * time.Minute)
|
||||
goman.New(func() {
|
||||
<-timeout.C
|
||||
oldPool.Dispose()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events.On(events.EventReload, func() {
|
||||
if sharedNodeConfig != nil {
|
||||
var scripts = sharedNodeConfig.CommonScripts // 拷贝为了安全操作
|
||||
if js.IsSameCommonScripts(scripts) {
|
||||
if SharedJSPool == nil {
|
||||
createPool()
|
||||
}
|
||||
return
|
||||
}
|
||||
js.SetCommonScripts(scripts)
|
||||
|
||||
// create new pool
|
||||
createPool()
|
||||
}
|
||||
})
|
||||
|
||||
goman.New(func() {
|
||||
for range commonScriptsChangesChan {
|
||||
if sharedNodeConfig != nil {
|
||||
var scripts = sharedNodeConfig.CommonScripts // 拷贝为了安全操作
|
||||
if js.IsSameCommonScripts(scripts) {
|
||||
if SharedJSPool == nil {
|
||||
createPool()
|
||||
}
|
||||
return
|
||||
}
|
||||
js.SetCommonScripts(scripts)
|
||||
|
||||
// create new pool
|
||||
remotelogs.Println("SCRIPT", "reloading common scripts ...")
|
||||
createPool()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user