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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pool
  2. import (
  3. "context"
  4. "git.ouxuan.net/3136352472/go-service-template/jsruntime"
  5. "github.com/jolestar/go-commons-pool"
  6. "io/ioutil"
  7. "log"
  8. "time"
  9. )
  10. type JsRuntimePool struct {
  11. ctx context.Context
  12. jsRuntimePool *pool.ObjectPool
  13. }
  14. type Config struct {
  15. MaxIdle int `json:"max_idle"`
  16. MinIdle int `json:"min_idle"`
  17. MaxTotal int `json:"max_total"`
  18. }
  19. var DefaultConfig Config
  20. var useSrc string
  21. func NewJsRuntimePool(mainScript string, useFileName string, staticPath string, relys jsruntime.Relys, mode jsruntime.RunMode, debug bool) (jrp *JsRuntimePool) {
  22. jrp = &JsRuntimePool{}
  23. factory := pool.NewPooledObjectFactorySimple(
  24. func(context.Context) (interface{}, error) {
  25. render, err := jsruntime.NewJsRuntime(mainScript, staticPath, relys, mode)
  26. if err == nil {
  27. render.IsDebugModel = debug
  28. render.UseSrcFun = func() string {
  29. return useSrc
  30. }
  31. }
  32. return render, err
  33. })
  34. jrp.ctx = context.Background()
  35. if DefaultConfig.MaxIdle == 0 {
  36. DefaultConfig.MaxIdle = 3000
  37. }
  38. if DefaultConfig.MinIdle == 0 {
  39. DefaultConfig.MinIdle = 20
  40. }
  41. if DefaultConfig.MaxTotal == 0 {
  42. DefaultConfig.MaxTotal = 200
  43. }
  44. jrp.jsRuntimePool = pool.NewObjectPoolWithDefaultConfig(jrp.ctx, factory)
  45. jrp.jsRuntimePool.Config.LIFO = false
  46. jrp.jsRuntimePool.Config.MaxIdle = DefaultConfig.MaxIdle
  47. jrp.jsRuntimePool.Config.MinIdle = DefaultConfig.MinIdle
  48. jrp.jsRuntimePool.Config.MaxTotal = DefaultConfig.MaxTotal
  49. jrp.jsRuntimePool.Config.BlockWhenExhausted = true
  50. var preparePoolTask func()
  51. preparePoolTask = func() {
  52. defer func() {
  53. recover()
  54. time.Sleep(time.Second)
  55. preparePoolTask()
  56. }()
  57. for {
  58. jrp.jsRuntimePool.PreparePool(jrp.ctx)
  59. time.Sleep(time.Second)
  60. }
  61. }
  62. go preparePoolTask()
  63. var useSrcTask func()
  64. useSrcTask = func() {
  65. defer func() {
  66. recover()
  67. time.Sleep(time.Second)
  68. useSrcTask()
  69. }()
  70. for {
  71. raw, _ := ioutil.ReadFile(useFileName)
  72. useSrc = string(raw)
  73. time.Sleep(time.Second * 3)
  74. }
  75. }
  76. go useSrcTask()
  77. return
  78. }
  79. func (jrp *JsRuntimePool) JsRuntimeCall(call func(jr *jsruntime.JsRuntime)) (err error) {
  80. obj, err := jrp.jsRuntimePool.BorrowObject(jrp.ctx)
  81. if err != nil {
  82. return
  83. }
  84. o := obj.(*jsruntime.JsRuntime)
  85. call(o)
  86. err = jrp.jsRuntimePool.InvalidateObject(jrp.ctx, obj)
  87. //err = jrp.jsRuntimePool.ReturnObject(jrp.ctx, obj)
  88. return
  89. }
  90. func (jrp *JsRuntimePool) Log() {
  91. idle := jrp.jsRuntimePool.GetNumIdle()
  92. active := jrp.jsRuntimePool.GetNumActive()
  93. all := idle + active
  94. log.Println("所有:", all, "空闲:", idle, "活动:", active)
  95. }
  96. func (jrp *JsRuntimePool) NumInfo() (int, int, int) {
  97. idle := jrp.jsRuntimePool.GetNumIdle()
  98. active := jrp.jsRuntimePool.GetNumActive()
  99. all := idle + active
  100. return all, idle, active
  101. }