github.com/songzhibin97/gkit@v1.2.13/watching/example/thread_trigger/thread_trigger.go (about)

     1  package main
     2  
     3  /*
     4  #include <stdio.h>
     5  #include <stdlib.h>
     6  #include <unistd.h>
     7  void output(char *str) {
     8      sleep(10000);
     9      printf("%s\n", str);
    10  }
    11  */
    12  import "C"
    13  
    14  import (
    15  	"fmt"
    16  	"net/http"
    17  	"time"
    18  	"unsafe"
    19  
    20  	"github.com/songzhibin97/gkit/watching"
    21  
    22  	_ "net/http/pprof"
    23  )
    24  
    25  func init() {
    26  	go func() {
    27  		w := watching.NewWatching(
    28  			watching.WithCollectInterval("2s"),
    29  			watching.WithCoolDown("5s"),
    30  			watching.WithDumpPath("/tmp"),
    31  			watching.WithTextDump(),
    32  			watching.WithThreadDump(10, 25, 100),
    33  		)
    34  		w.EnableThreadDump().Start()
    35  		time.Sleep(time.Hour)
    36  	}()
    37  }
    38  
    39  func leak(wr http.ResponseWriter, req *http.Request) {
    40  	go func() {
    41  		for i := 0; i < 1000; i++ {
    42  			go func() {
    43  				str := "hello cgo"
    44  				// change to char*
    45  				cstr := C.CString(str)
    46  				C.output(cstr)
    47  				C.free(unsafe.Pointer(cstr))
    48  			}()
    49  		}
    50  	}()
    51  }
    52  
    53  func main() {
    54  	http.HandleFunc("/leak", leak)
    55  	err := http.ListenAndServe(":10003", nil)
    56  	if err != nil {
    57  		fmt.Println(err)
    58  		return
    59  	}
    60  	select {}
    61  }