Skip to content
This repository was archived by the owner on Oct 19, 2019. It is now read-only.

Add worker.SetNearHeapLimitCallback#19

Open
zhangyuanwei wants to merge 1 commit into
ry:masterfrom
zhangyuanwei:master
Open

Add worker.SetNearHeapLimitCallback#19
zhangyuanwei wants to merge 1 commit into
ry:masterfrom
zhangyuanwei:master

Conversation

@zhangyuanwei
Copy link
Copy Markdown

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)
	}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant