spawn_frame_budget.mx raw

   1  //:wasmtest skip
   2  package main
   3  
   4  import (
   5  	"fmt"
   6  	"time"
   7  )
   8  
   9  // Spawn channel frames are sized from the element type: a `cap` tag on every
  10  // variable-length field states its element count, and an unstated dimension is
  11  // one 4096-byte page. The ring is created at that size before the fork, so a
  12  // payload at the declared size must cross, and the declared depth must hold.
  13  type Req struct {
  14  	ID   int64
  15  	Tags []string `cap:"8,32"`
  16  	Body []byte   `cap:"8192"`
  17  }
  18  
  19  // Untagged carries the default: no cap tag on Body means 4096 bytes.
  20  type Untagged struct {
  21  	N    int32
  22  	Body []byte
  23  }
  24  
  25  func worker(in chan Req, one chan Untagged) (closed chan struct{}) {
  26  	time.Sleep(100 * time.Millisecond)
  27  	for i := 0; i < 2; i++ {
  28  		var r Req
  29  		select {
  30  		case <-closed:
  31  			return closed
  32  		case rv := <-in:
  33  			r = rv
  34  		}
  35  		fmt.Println("  req", r.ID, "tags", len(r.Tags), "body", len(r.Body))
  36  	}
  37  	var u Untagged
  38  	select {
  39  	case <-closed:
  40  		return closed
  41  	case uv := <-one:
  42  		u = uv
  43  	}
  44  	fmt.Println("  untagged body", len(u.Body))
  45  	return closed
  46  }
  47  
  48  func main() {
  49  	// Termination: a batch program must still be stoppable, so the entry
  50  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  51  	select {
  52  	case <-shutdown:
  53  		return
  54  	default:
  55  	}
  56  
  57  	in := chan Req{:4}
  58  	one := chan Untagged{}
  59  	done := spawn(worker, in, one)
  60  
  61  	a := Req{ID: 1, Tags: []string{"a", "b", "c"}, Body: []byte("hello")}
  62  	in <- a
  63  	big := []byte{:6000}
  64  	for i := 0; i < 6000; i++ {
  65  		big[i] = byte(i)
  66  	}
  67  	b := Req{ID: 2, Tags: []string{"x"}, Body: big}
  68  	in <- b
  69  
  70  	// Exactly the page default, through a one-slot mailbox.
  71  	fill := []byte{:4096}
  72  	u := Untagged{N: 7, Body: fill}
  73  	one <- u
  74  
  75  	<-done
  76  	fmt.Println("ok")
  77  }
  78