github.com/sunvim/utils@v0.1.0/examples/netepoll/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	_ "net/http/pprof"
     7  	"os"
     8  	"syscall"
     9  	"time"
    10  
    11  	"github.com/oxtoacart/bpool"
    12  	"github.com/rcrowley/go-metrics"
    13  	"github.com/sunvim/utils/netpoll"
    14  )
    15  
    16  func main() {
    17  	setLimit()
    18  	go metrics.Log(metrics.DefaultRegistry, 5*time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
    19  
    20  	go func() {
    21  		if err := http.ListenAndServe(":6060", nil); err != nil {
    22  			log.Fatalf("pprof failed: %v", err)
    23  		}
    24  	}()
    25  
    26  	var handler = &netpoll.DataHandler{
    27  		Pool: bpool.NewBytePool(1024, 12*1024),
    28  		HandlerFunc: func(req []byte) (res []byte) {
    29  			res = req
    30  			return
    31  		},
    32  	}
    33  	if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
    34  		panic(err)
    35  	}
    36  }
    37  
    38  func setLimit() {
    39  	var rLimit syscall.Rlimit
    40  	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
    41  		panic(err)
    42  	}
    43  	rLimit.Cur = rLimit.Max
    44  	if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	log.Printf("set cur limit: %d", rLimit.Cur)
    49  }