You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
2.7 KiB
121 lines
2.7 KiB
package pool
|
|
|
|
import (
|
|
"context"
|
|
"git.ouxuan.net/3136352472/go-service-template/jsruntime"
|
|
"github.com/jolestar/go-commons-pool"
|
|
"io/ioutil"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type JsRuntimePool struct {
|
|
ctx context.Context
|
|
|
|
jsRuntimePool *pool.ObjectPool
|
|
}
|
|
type Config struct {
|
|
MaxIdle int `json:"max_idle"`
|
|
MinIdle int `json:"min_idle"`
|
|
MaxTotal int `json:"max_total"`
|
|
}
|
|
|
|
var DefaultConfig Config
|
|
|
|
var useSrc string
|
|
|
|
func NewJsRuntimePool(mainScript string, useFileName string, staticPath string, relys jsruntime.Relys, mode jsruntime.RunMode, debug bool) (jrp *JsRuntimePool) {
|
|
jrp = &JsRuntimePool{}
|
|
|
|
factory := pool.NewPooledObjectFactorySimple(
|
|
func(context.Context) (interface{}, error) {
|
|
render, err := jsruntime.NewJsRuntime(mainScript, staticPath, relys, mode)
|
|
if err == nil {
|
|
render.IsDebugModel = debug
|
|
render.UseSrcFun = func() string {
|
|
return useSrc
|
|
}
|
|
}
|
|
return render, err
|
|
})
|
|
|
|
jrp.ctx = context.Background()
|
|
|
|
if DefaultConfig.MaxIdle == 0 {
|
|
DefaultConfig.MaxIdle = 3000
|
|
}
|
|
if DefaultConfig.MinIdle == 0 {
|
|
DefaultConfig.MinIdle = 20
|
|
}
|
|
|
|
if DefaultConfig.MaxTotal == 0 {
|
|
DefaultConfig.MaxTotal = 200
|
|
}
|
|
|
|
jrp.jsRuntimePool = pool.NewObjectPoolWithDefaultConfig(jrp.ctx, factory)
|
|
jrp.jsRuntimePool.Config.LIFO = false
|
|
|
|
jrp.jsRuntimePool.Config.MaxIdle = DefaultConfig.MaxIdle
|
|
jrp.jsRuntimePool.Config.MinIdle = DefaultConfig.MinIdle
|
|
jrp.jsRuntimePool.Config.MaxTotal = DefaultConfig.MaxTotal
|
|
jrp.jsRuntimePool.Config.BlockWhenExhausted = true
|
|
|
|
var preparePoolTask func()
|
|
preparePoolTask = func() {
|
|
defer func() {
|
|
recover()
|
|
time.Sleep(time.Second)
|
|
preparePoolTask()
|
|
}()
|
|
for {
|
|
jrp.jsRuntimePool.PreparePool(jrp.ctx)
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|
|
go preparePoolTask()
|
|
|
|
var useSrcTask func()
|
|
useSrcTask = func() {
|
|
defer func() {
|
|
recover()
|
|
time.Sleep(time.Second)
|
|
useSrcTask()
|
|
}()
|
|
for {
|
|
raw, _ := ioutil.ReadFile(useFileName)
|
|
useSrc = string(raw)
|
|
time.Sleep(time.Second * 3)
|
|
}
|
|
}
|
|
go useSrcTask()
|
|
|
|
return
|
|
}
|
|
|
|
func (jrp *JsRuntimePool) JsRuntimeCall(call func(jr *jsruntime.JsRuntime)) (err error) {
|
|
obj, err := jrp.jsRuntimePool.BorrowObject(jrp.ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
o := obj.(*jsruntime.JsRuntime)
|
|
call(o)
|
|
|
|
err = jrp.jsRuntimePool.InvalidateObject(jrp.ctx, obj)
|
|
//err = jrp.jsRuntimePool.ReturnObject(jrp.ctx, obj)
|
|
return
|
|
}
|
|
|
|
func (jrp *JsRuntimePool) Log() {
|
|
idle := jrp.jsRuntimePool.GetNumIdle()
|
|
active := jrp.jsRuntimePool.GetNumActive()
|
|
all := idle + active
|
|
log.Println("所有:", all, "空闲:", idle, "活动:", active)
|
|
}
|
|
|
|
func (jrp *JsRuntimePool) NumInfo() (int, int, int) {
|
|
idle := jrp.jsRuntimePool.GetNumIdle()
|
|
active := jrp.jsRuntimePool.GetNumActive()
|
|
all := idle + active
|
|
return all, idle, active
|
|
}
|