Skip to content

Commit

Permalink
- loging -> logging
Browse files Browse the repository at this point in the history
- fix nbhttp parser test
  • Loading branch information
lesismal committed Jun 8, 2021
1 parent 87f92da commit f571bac
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 59 deletions.
8 changes: 4 additions & 4 deletions examples/echo/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/lesismal/nbio"
"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

func main() {
Expand All @@ -19,7 +19,7 @@ func main() {
ctx, _ = context.WithTimeout(context.Background(), time.Second)
)

loging.SetLevel(loging.LevelInfo)
logging.SetLevel(logging.LevelInfo)

rand.Read(buf)

Expand Down Expand Up @@ -50,8 +50,8 @@ func main() {

select {
case <-ctx.Done():
loging.Error("timeout")
logging.Error("timeout")
case <-done:
loging.Info("success")
logging.Info("success")
}
}
8 changes: 4 additions & 4 deletions examples/netstd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/lesismal/nbio"
"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

func main() {
Expand All @@ -20,7 +20,7 @@ func main() {
ctx, _ = context.WithTimeout(context.Background(), time.Second)
)

loging.SetLevel(loging.LevelInfo)
logging.SetLevel(logging.LevelInfo)

rand.Read(buf)

Expand Down Expand Up @@ -51,8 +51,8 @@ func main() {

select {
case <-ctx.Done():
loging.Error("timeout")
logging.Error("timeout")
case <-done:
loging.Info("success")
logging.Info("success")
}
}
10 changes: 5 additions & 5 deletions gopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
"time"

"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()
}
}()
Expand Down
6 changes: 3 additions & 3 deletions gopher_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

// Start init and start pollers
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions gopher_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"syscall"
"time"

"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

// Start init and start pollers
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion loging/log.go → logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion loging/log_test.go → logging/log_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package loging
package logging

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion nbhttp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ UPGRADER:

if p.TLSBuffer == nil {
defer func() {
if data != nil {
if data != nil && p.Server != nil {
p.Server.Free(data)
}
}()
Expand Down
22 changes: 20 additions & 2 deletions nbhttp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"testing"
"time"

"github.com/lesismal/nbio/mempool"
)

func TestServerParserContentLength(t *testing.T) {
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions nbhttp/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
}
}()
Expand Down
20 changes: 10 additions & 10 deletions nbhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions poller_epoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"
"unsafe"

"github.com/lesismal/nbio/loging"
"github.com/lesismal/nbio/logging"
)

const (
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit f571bac

Please sign in to comment.