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

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/songzhibin97/gkit/watching"
     8  )
     9  
    10  func init() {
    11  	http.HandleFunc("/leak", leak)
    12  	go http.ListenAndServe(":10003", nil)
    13  }
    14  
    15  func main() {
    16  	w := watching.NewWatching(
    17  		watching.WithCollectInterval("2s"),
    18  		watching.WithCoolDown("1m"),
    19  		watching.WithDumpPath("/tmp"),
    20  		watching.WithTextDump(),
    21  		watching.WithGoroutineDump(10, 25, 80, 1000),
    22  	)
    23  	w.EnableGoroutineDump().Start()
    24  	time.Sleep(time.Hour)
    25  }
    26  
    27  func leak(wr http.ResponseWriter, req *http.Request) {
    28  	taskChan := make(chan int)
    29  	consumer := func() {
    30  		for task := range taskChan {
    31  			_ = task // do some tasks
    32  		}
    33  	}
    34  
    35  	producer := func() {
    36  		for i := 0; i < 10; i++ {
    37  			taskChan <- i // generate some tasks
    38  		}
    39  		// forget to close the taskChan here
    40  	}
    41  
    42  	go consumer()
    43  	go producer()
    44  }