github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/proto/v2/server/main.go (about) 1 package main 2 3 import ( 4 "io" 5 "io/ioutil" 6 "log" 7 "net" 8 "net/http" 9 _ "net/http/pprof" 10 "runtime" 11 ) 12 import "fmt" 13 14 func main() { 15 16 ln, err := net.Listen("tcp", ":8972") 17 if err != nil { 18 panic(err) 19 } 20 go func() { 21 if err := http.ListenAndServe(":6060", nil); err != nil { 22 log.Fatalf("pprof failed: %v", err) 23 } 24 }() 25 var connections []net.Conn 26 defer func() { 27 for _, conn := range connections { 28 conn.Close() 29 } 30 }() 31 for { 32 conn, e := ln.Accept() 33 if e != nil { 34 if ne, ok := e.(net.Error); ok && ne.Temporary() { 35 log.Printf("accept temp err: %v", ne) 36 continue 37 } 38 log.Printf("accept err: %v", e) 39 return 40 } 41 go handleConn(conn) 42 connections = append(connections, conn) 43 if len(connections)%100 == 0 { 44 log.Printf("total number of connections: %v", len(connections)) 45 } 46 } 47 48 } 49 func handleConn(conn net.Conn) { 50 io.Copy(ioutil.Discard, conn) 51 } 52 53 func TraceCode(code ...interface{}) { 54 var buf [4096]byte 55 n := runtime.Stack(buf[:], false) 56 data := "" 57 for _, v := range code { 58 data += fmt.Sprintf("%v", v) 59 } 60 61 data += string(buf[:n]) 62 fmt.Printf("==> %s\n", data) 63 }