github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/poller/cmd/echo/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "net/http" 6 _ "net/http/pprof" 7 "os" 8 "os/signal" 9 "syscall" 10 "time" 11 12 "github.com/weedge/lib/log" 13 "github.com/weedge/lib/poller" 14 ) 15 16 type MockDecoder struct { 17 } 18 19 func (m *MockDecoder) Decode(buffer *poller.Buffer) (bytes []byte, err error) { 20 bytes = buffer.ReadAll() 21 //log.Infof("decode buffer :%s len:%d bytes", bytes, len(bytes)) 22 23 return bytes, nil 24 } 25 26 type MockServerHandler struct { 27 } 28 29 func (m *MockServerHandler) OnConnect(c *poller.Conn) { 30 log.Infof("connect fd %d addr %s", c.GetFd(), c.GetAddr()) 31 } 32 33 func (m *MockServerHandler) OnMessage(c *poller.Conn, bytes []byte) { 34 //log.Infof("read:%s len:%d bytes from fd:%d", bytes, len(bytes), c.GetFd()) 35 c.Write(bytes) 36 } 37 38 func (m *MockServerHandler) OnClose(c *poller.Conn, err error) { 39 log.Infof("close: %d err: %s", c.GetFd(), err.Error()) 40 } 41 42 var port = flag.String("port", "8081", "port") 43 var msgSize = flag.Int("size", 512, "size") 44 var ioMode = flag.String("ioMode", "", "ioMode") 45 var mapIoMode = map[string]poller.IOMode{ 46 "iouring": poller.IOModeUring, 47 "epollIouring": poller.IOModeEpollUring, 48 } 49 50 func main() { 51 flag.Parse() 52 //runtime.GOMAXPROCS(1) 53 54 go func() { 55 if err := http.ListenAndServe(":6060", nil); err != nil { 56 log.Errorf("pprof failed: %v", err) 57 return 58 } 59 }() 60 61 server, err := poller.NewServer(":"+*port, &MockServerHandler{}, 62 //poller.WithDecoder(&MockDecoder{}), 63 poller.WithIoMode(mapIoMode[*ioMode]), 64 poller.WithTimeout(3*time.Second, 100*time.Second), poller.WithReadBufferLen(*msgSize)) 65 if err != nil { 66 log.Info("err ", err.Error()) 67 return 68 } 69 70 go server.Run() 71 72 quit := make(chan os.Signal, 1) 73 signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) 74 <-quit 75 76 log.Info("server stop") 77 server.Stop() 78 }