From f571bac0d45b3082d81727f39b482d9ce68289ae Mon Sep 17 00:00:00 2001 From: lesismal <40462947+lesismal@users.noreply.github.com> Date: Wed, 9 Jun 2021 01:15:35 +0800 Subject: [PATCH] - loging -> logging - fix nbhttp parser test --- examples/echo/client/client.go | 8 ++++---- examples/netstd/client/client.go | 8 ++++---- gopher.go | 10 +++++----- gopher_std.go | 6 +++--- gopher_unix.go | 6 +++--- {loging => logging}/log.go | 2 +- {loging => logging}/log_test.go | 2 +- nbhttp/parser.go | 2 +- nbhttp/parser_test.go | 22 ++++++++++++++++++++-- nbhttp/processor.go | 4 ++-- nbhttp/server.go | 20 ++++++++++---------- poller_epoll.go | 14 +++++++------- poller_kqueue.go | 12 ++++++------ poller_std.go | 12 ++++++------ taskpool/caller.go | 4 ++-- taskpool/mixedpool.go | 4 ++-- 16 files changed, 77 insertions(+), 59 deletions(-) rename {loging => logging}/log.go (99%) rename {loging => logging}/log_test.go (98%) diff --git a/examples/echo/client/client.go b/examples/echo/client/client.go index cebe8306..29b101d2 100644 --- a/examples/echo/client/client.go +++ b/examples/echo/client/client.go @@ -8,7 +8,7 @@ import ( "time" "github.com/lesismal/nbio" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) func main() { @@ -19,7 +19,7 @@ func main() { ctx, _ = context.WithTimeout(context.Background(), time.Second) ) - loging.SetLevel(loging.LevelInfo) + logging.SetLevel(logging.LevelInfo) rand.Read(buf) @@ -50,8 +50,8 @@ func main() { select { case <-ctx.Done(): - loging.Error("timeout") + logging.Error("timeout") case <-done: - loging.Info("success") + logging.Info("success") } } diff --git a/examples/netstd/client/client.go b/examples/netstd/client/client.go index 936dadeb..b2b5368d 100644 --- a/examples/netstd/client/client.go +++ b/examples/netstd/client/client.go @@ -9,7 +9,7 @@ import ( "time" "github.com/lesismal/nbio" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) func main() { @@ -20,7 +20,7 @@ func main() { ctx, _ = context.WithTimeout(context.Background(), time.Second) ) - loging.SetLevel(loging.LevelInfo) + logging.SetLevel(logging.LevelInfo) rand.Read(buf) @@ -51,8 +51,8 @@ func main() { select { case <-ctx.Done(): - loging.Error("timeout") + logging.Error("timeout") case <-done: - loging.Info("success") + logging.Info("success") } } diff --git a/gopher.go b/gopher.go index 5ac06707..e8f05bb9 100644 --- a/gopher.go +++ b/gopher.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) const ( @@ -144,7 +144,7 @@ func (g *Gopher) Stop() { } g.Wait() - loging.Info("Gopher[%v] stop", g.Name) + logging.Info("Gopher[%v] stop", g.Name) } // AddConn adds conn to a poller @@ -315,8 +315,8 @@ func (g *Gopher) resetTimer(it *htimer) { func (g *Gopher) timerLoop() { defer g.Done() - loging.Debug("Gopher[%v] timer start", g.Name) - defer loging.Debug("Gopher[%v] timer stopped", g.Name) + logging.Debug("Gopher[%v] timer start", g.Name) + defer logging.Debug("Gopher[%v] timer stopped", g.Name) for { select { case <-g.trigger.C: @@ -335,7 +335,7 @@ func (g *Gopher) timerLoop() { defer func() { err := recover() if err != nil { - loging.Error("Gopher[%v] exec timer failed: %v", g.Name, err) + logging.Error("Gopher[%v] exec timer failed: %v", g.Name, err) debug.PrintStack() } }() diff --git a/gopher_std.go b/gopher_std.go index 46160804..fd2f29ef 100644 --- a/gopher_std.go +++ b/gopher_std.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) // Start init and start pollers @@ -58,9 +58,9 @@ func (g *Gopher) Start() error { go g.timerLoop() if len(g.addrs) == 0 { - loging.Info("Gopher[%v] start", g.Name) + logging.Info("Gopher[%v] start", g.Name) } else { - loging.Info("Gopher[%v] start listen on: [\"%v\"]", g.Name, strings.Join(g.addrs, `", "`)) + logging.Info("Gopher[%v] start listen on: [\"%v\"]", g.Name, strings.Join(g.addrs, `", "`)) } return nil } diff --git a/gopher_unix.go b/gopher_unix.go index 4ed18f17..e4b42b9a 100644 --- a/gopher_unix.go +++ b/gopher_unix.go @@ -12,7 +12,7 @@ import ( "syscall" "time" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) // Start init and start pollers @@ -61,9 +61,9 @@ func (g *Gopher) Start() error { go g.timerLoop() if len(g.addrs) == 0 { - loging.Info("Gopher[%v] start", g.Name) + logging.Info("Gopher[%v] start", g.Name) } else { - loging.Info("Gopher[%v] start listen on: [\"%v\"]", g.Name, strings.Join(g.addrs, `", "`)) + logging.Info("Gopher[%v] start listen on: [\"%v\"]", g.Name, strings.Join(g.addrs, `", "`)) } return nil } diff --git a/loging/log.go b/logging/log.go similarity index 99% rename from loging/log.go rename to logging/log.go index 0c14e2ee..0d96ab64 100644 --- a/loging/log.go +++ b/logging/log.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. -package loging +package logging import ( "fmt" diff --git a/loging/log_test.go b/logging/log_test.go similarity index 98% rename from loging/log_test.go rename to logging/log_test.go index d16d3394..71e53a52 100644 --- a/loging/log_test.go +++ b/logging/log_test.go @@ -1,4 +1,4 @@ -package loging +package logging import "testing" diff --git a/nbhttp/parser.go b/nbhttp/parser.go index 0211ad3e..c91c3527 100644 --- a/nbhttp/parser.go +++ b/nbhttp/parser.go @@ -101,7 +101,7 @@ UPGRADER: if p.TLSBuffer == nil { defer func() { - if data != nil { + if data != nil && p.Server != nil { p.Server.Free(data) } }() diff --git a/nbhttp/parser_test.go b/nbhttp/parser_test.go index d969b29b..2ca0c86c 100644 --- a/nbhttp/parser_test.go +++ b/nbhttp/parser_test.go @@ -7,6 +7,8 @@ import ( "net/http" "testing" "time" + + "github.com/lesismal/nbio/mempool" ) func TestServerParserContentLength(t *testing.T) { @@ -92,7 +94,13 @@ func testParser(t *testing.T, isClient bool, data []byte) error { nRequest++ }) } + svr := &Server{ + Malloc: mempool.Malloc, + Realloc: mempool.Realloc, + Free: mempool.Free, + } parser = NewParser(processor, isClient, maxReadSize, minBufferSize) + parser.Server = svr tBegin := time.Now() loop := 10000 for i := 0; i < loop; i++ { @@ -120,16 +128,26 @@ func testParser(t *testing.T, isClient bool, data []byte) error { } func newParser(isClient bool) *Parser { + svr := &Server{ + Malloc: mempool.Malloc, + Realloc: mempool.Realloc, + Free: mempool.Free, + } maxReadSize := 1024 * 1024 * 4 minBufferSize := 1024 * 4 if isClient { processor := NewClientProcessor(nil, func(*http.Response) {}) - return NewParser(processor, isClient, maxReadSize, minBufferSize) + parser := NewParser(processor, isClient, maxReadSize, minBufferSize) + parser.Server = svr + return parser } mux := &http.ServeMux{} mux.HandleFunc("/", pirntMessage) processor := NewServerProcessor(nil, mux, nil, 2048, DefaultKeepaliveTime, false) - return NewParser(processor, isClient, maxReadSize, minBufferSize) + + parser := NewParser(processor, isClient, maxReadSize, minBufferSize) + parser.Server = svr + return parser } func pirntMessage(w http.ResponseWriter, request *http.Request) { diff --git a/nbhttp/processor.go b/nbhttp/processor.go index a9cd59b7..93b641c4 100644 --- a/nbhttp/processor.go +++ b/nbhttp/processor.go @@ -16,7 +16,7 @@ import ( "time" "github.com/lesismal/nbio" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" "github.com/lesismal/nbio/mempool" ) @@ -307,7 +307,7 @@ func (p *ServerProcessor) HandleMessage(handler http.Handler) { func (p *ServerProcessor) call(f func()) { defer func() { if err := recover(); err != nil { - loging.Error("ServerProcessor call failed: %v", err) + logging.Error("ServerProcessor call failed: %v", err) debug.PrintStack() } }() diff --git a/nbhttp/server.go b/nbhttp/server.go index dc529c94..28fb9512 100644 --- a/nbhttp/server.go +++ b/nbhttp/server.go @@ -15,7 +15,7 @@ import ( "github.com/lesismal/llib/std/crypto/tls" "github.com/lesismal/nbio" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" "github.com/lesismal/nbio/mempool" "github.com/lesismal/nbio/taskpool" ) @@ -208,7 +208,7 @@ func (s *Server) Shutdown(ctx context.Context) error { Exit: s.Stop() - loging.Info("Gopher[%v] shutdown", s.Name) + logging.Info("Gopher[%v] shutdown", s.Name) return nil } @@ -243,7 +243,7 @@ func NewServer(conf Config, handler http.Handler, messageHandlerExecutor func(in const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] - loging.Error("execute parser failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) + logging.Error("execute parser failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) } }() f() @@ -316,7 +316,7 @@ func NewServer(conf Config, handler http.Handler, messageHandlerExecutor func(in g.OnClose(func(c *nbio.Conn, err error) { parser := c.Session().(*Parser) if parser == nil { - loging.Error("nil parser") + logging.Error("nil parser") } parser.onClose(err) svr._onClose(c, err) @@ -330,13 +330,13 @@ func NewServer(conf Config, handler http.Handler, messageHandlerExecutor func(in data = newData parser := c.Session().(*Parser) if parser == nil { - loging.Error("nil parser") + logging.Error("nil parser") return } svr.ParserExecutor(c.Hash(), func() { err := parser.Read(data) if err != nil { - loging.Debug("parser.Read failed: %v", err) + logging.Debug("parser.Read failed: %v", err) c.CloseWithError(err) } }) @@ -394,7 +394,7 @@ func NewServerTLS(conf Config, handler http.Handler, messageHandlerExecutor func const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] - loging.Error("execute parser failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) + logging.Error("execute parser failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) } }() f() @@ -486,7 +486,7 @@ func NewServerTLS(conf Config, handler http.Handler, messageHandlerExecutor func g.OnClose(func(c *nbio.Conn, err error) { parser := c.Session().(*Parser) if parser == nil { - loging.Error("nil parser") + logging.Error("nil parser") return } parser.onClose(err) @@ -498,7 +498,7 @@ func NewServerTLS(conf Config, handler http.Handler, messageHandlerExecutor func g.OnData(func(c *nbio.Conn, data []byte) { parser := c.Session().(*Parser) if parser == nil { - loging.Error("nil parser") + logging.Error("nil parser") c.Close() return } @@ -515,7 +515,7 @@ func NewServerTLS(conf Config, handler http.Handler, messageHandlerExecutor func if n > 0 { err := parser.Read(buffer[:n]) if err != nil { - loging.Debug("parser.Read failed: %v", err) + logging.Debug("parser.Read failed: %v", err) c.CloseWithError(err) return } diff --git a/poller_epoll.go b/poller_epoll.go index f553854f..20fff885 100644 --- a/poller_epoll.go +++ b/poller_epoll.go @@ -14,7 +14,7 @@ import ( "time" "unsafe" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) const ( @@ -51,7 +51,7 @@ func (p *poller) addConn(c *Conn) { if err != nil { p.g.connsUnix[fd] = nil c.closeWithError(err) - loging.Error("[%v] add read event failed: %v", c.fd, err) + logging.Error("[%v] add read event failed: %v", c.fd, err) return } } @@ -75,8 +75,8 @@ func (p *poller) deleteConn(c *Conn) { func (p *poller) start() { defer p.g.Done() - loging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) - defer loging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) + defer logging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) defer func() { syscall.Close(p.epfd) syscall.Close(p.evtfd) @@ -108,10 +108,10 @@ func (p *poller) acceptorLoop() { o.addConn(c) } else { if ne, ok := err.(net.Error); ok && ne.Temporary() { - loging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) + logging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) time.Sleep(time.Second / 20) } else { - loging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) + logging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) break } } @@ -155,7 +155,7 @@ func (p *poller) readWriteLoop() { } func (p *poller) stop() { - loging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) p.shutdown = true if p.listener != nil { p.listener.Close() diff --git a/poller_kqueue.go b/poller_kqueue.go index f882eebf..c054663f 100644 --- a/poller_kqueue.go +++ b/poller_kqueue.go @@ -13,7 +13,7 @@ import ( "syscall" "time" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) type poller struct { @@ -132,8 +132,8 @@ func (p *poller) start() { } defer p.g.Done() - loging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) - defer loging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) + defer logging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) defer syscall.Close(p.kfd) if p.isListener { @@ -162,10 +162,10 @@ func (p *poller) acceptorLoop() { o.addConn(c) } else { if ne, ok := err.(net.Error); ok && ne.Temporary() { - loging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) + logging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) time.Sleep(time.Second / 20) } else { - loging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) + logging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) break } } @@ -203,7 +203,7 @@ func (p *poller) readWriteLoop() { } func (p *poller) stop() { - loging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) p.shutdown = true if p.listener != nil { p.listener.Close() diff --git a/poller_std.go b/poller_std.go index 40086d4b..8247cdab 100644 --- a/poller_std.go +++ b/poller_std.go @@ -12,7 +12,7 @@ import ( "sync" "time" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) type poller struct { @@ -85,8 +85,8 @@ func (p *poller) start() { } defer p.g.Done() - loging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) - defer loging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] start", p.g.Name, p.pollType, p.index) + defer logging.Debug("Poller[%v_%v_%v] stopped", p.g.Name, p.pollType, p.index) if p.isListener { var err error @@ -95,10 +95,10 @@ func (p *poller) start() { err = p.accept() if err != nil { if ne, ok := err.(net.Error); ok && ne.Temporary() { - loging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) + logging.Error("Poller[%v_%v_%v] Accept failed: temporary error, retrying...", p.g.Name, p.pollType, p.index) time.Sleep(time.Second / 20) } else { - loging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) + logging.Error("Poller[%v_%v_%v] Accept failed: %v, exit...", p.g.Name, p.pollType, p.index, err) break } } @@ -109,7 +109,7 @@ func (p *poller) start() { } func (p *poller) stop() { - loging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) + logging.Debug("Poller[%v_%v_%v] stop...", p.g.Name, p.pollType, p.index) p.shutdown = true if p.isListener { p.listener.Close() diff --git a/taskpool/caller.go b/taskpool/caller.go index 805d0839..d5392bea 100644 --- a/taskpool/caller.go +++ b/taskpool/caller.go @@ -8,7 +8,7 @@ import ( "runtime" "unsafe" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) func call(f func()) { @@ -17,7 +17,7 @@ func call(f func()) { const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] - loging.Error("taskpool call failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) + logging.Error("taskpool call failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) } }() f() diff --git a/taskpool/mixedpool.go b/taskpool/mixedpool.go index 64b9bf8d..c63465ed 100644 --- a/taskpool/mixedpool.go +++ b/taskpool/mixedpool.go @@ -9,7 +9,7 @@ import ( "sync/atomic" "unsafe" - "github.com/lesismal/nbio/loging" + "github.com/lesismal/nbio/logging" ) type MixedPool struct { @@ -24,7 +24,7 @@ func (mp *MixedPool) call(f func()) { const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] - loging.Error("taskpool call failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) + logging.Error("taskpool call failed: %v\n%v\n", err, *(*string)(unsafe.Pointer(&buf))) } atomic.AddInt32(&mp.cuncurrent, -1) }()