v8worker2
v8worker2 copied to clipboard
Add worker.SetNearHeapLimitCallback
Add a worker.SetNearHeapLimitCallback API to monitor third-party code memory usag. This API providing an opportunity to terminate when memory exceeds a threshold.
eg:
package main
import "fmt"
import "time"
import "github.com/zhangyuanwei/v8worker2"
func main() {
// set v8 heap limit to 10M
args := []string{"--max-semi-space-size=10", "--max-old-space-size=10"}
v8worker2.SetFlags(args)
go func() {
worker := v8worker2.New(func(msg []byte) []byte {
return nil
})
worker.SetNearHeapLimitCallback(func(currentHeapLimit uint, initialHeapLimit uint) uint {
fmt.Printf("near heap limit:[currentHeapLimit:%d, initialHeapLimit:%d]\n", currentHeapLimit, initialHeapLimit)
worker.TerminateExecution()
return currentHeapLimit + (10 * 1024 * 1024)
})
fmt.Println("worker start")
// This code will cause a fatal error (OOM)
worker.Load("oom.js", "var strArr = [\"abcd\"]; while(true){ strArr.push(strArr.join()); }")
fmt.Println("worker end")
}()
for {
time.Sleep(time.Second)
}
}