Skip to content

Commit

Permalink
add x/net/trace
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Jan 20, 2020
1 parent dd4c94b commit 52743fa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
14 changes: 12 additions & 2 deletions example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/textproto"
"strings"

"golang.org/x/net/trace"

"github.com/x-mod/routine"
"github.com/x-mod/tcpserver"
)
Expand All @@ -16,18 +18,22 @@ func main() {
tcpserver.Network("tcp"),
tcpserver.Address("127.0.0.1:8080"),
tcpserver.TCPHandler(EchoHandler),
tcpserver.NetTrace(true),
)
log.Println("tcpserver serving ...")
if err := routine.Main(
context.TODO(),
routine.ExecutorFunc(srv.Serve)); err != nil {
routine.ExecutorFunc(srv.Serve),
routine.Go(routine.Profiling("127.0.0.1:6060")),
); err != nil {
log.Println("tcpserver failed:", err)
}
}

func EchoHandler(ctx context.Context, con net.Conn) error {
defer con.Close()

c := textproto.NewConn(con)

for {
select {
case <-ctx.Done():
Expand All @@ -43,6 +49,10 @@ func EchoHandler(ctx context.Context, con net.Conn) error {
if err := c.PrintfLine(line); err != nil {
return err
}
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("echo request: %s", line)
}
}
}

}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/x-mod/tcpserver
go 1.13

require (
github.com/x-mod/routine v1.2.7
github.com/x-mod/tlsconfig v0.0.1
github.com/x-mod/routine v1.2.10 // indirect
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/x-mod/errors v0.1.6 h1:k0XsZCl/aQKPGo293ewOuYnRuxy9YVxDmUIC/b4C47s=
github.com/x-mod/errors v0.1.6/go.mod h1:sWzfaUOjYKcW4cQaF/VEUQibdxGjkD+8LnL+GJhkIs0=
github.com/x-mod/routine v1.2.7 h1:P55Bfv9YYOQDwisHyb/bjmcPw8ep+kYqSQ8WE5E/YQ8=
github.com/x-mod/routine v1.2.7/go.mod h1:FUJxsh8BYaKeT88jyhr0ubhumCMusZla3VlrrL46dcs=
github.com/x-mod/tlsconfig v0.0.1 h1:3LpCmjxPBZYuJ9mrRRxfoJRGd7n8fjtRp39b3xWRRE0=
github.com/x-mod/tlsconfig v0.0.1/go.mod h1:yiTPHfiJzNrZPOaXweRiTt7F4LWGVAqTVLP1N7UMy9g=
github.com/x-mod/routine v1.2.10 h1:l9ruReSSHGox2WnrajWA4kNWJSi2VQ2bBqw1pAR2ths=
github.com/x-mod/routine v1.2.10/go.mod h1:FUJxsh8BYaKeT88jyhr0ubhumCMusZla3VlrrL46dcs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
58 changes: 53 additions & 5 deletions tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package tcpserver
import (
"context"
"crypto/tls"
"log"
"fmt"
"net"
"runtime"
"sync"

"golang.org/x/net/trace"
)

//Handler connection handler definition
Expand All @@ -17,6 +20,9 @@ type Server struct {
network string
address string
handler Handler
traced bool
mu sync.Mutex
events trace.EventLog
listener net.Listener
tlsc *tls.Config
wgroup sync.WaitGroup
Expand Down Expand Up @@ -72,6 +78,12 @@ func TCPHandler(h Handler) ServerOpt {
}
}

func NetTrace(flag bool) ServerOpt {
return func(srv *Server) {
srv.traced = flag
}
}

//ServerOpt typedef
type ServerOpt func(*Server)

Expand All @@ -84,17 +96,40 @@ func New(opts ...ServerOpt) *Server {
for _, opt := range opts {
opt(serv)
}
if serv.traced {
_, file, line, _ := runtime.Caller(1)
serv.events = trace.NewEventLog(serv.name, fmt.Sprintf("%s:%d", file, line))
}
return serv
}

func (srv *Server) printf(format string, a ...interface{}) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.events != nil {
srv.events.Printf(format, a...)
}
}

func (srv *Server) errorf(format string, a ...interface{}) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.events != nil {
srv.events.Errorf(format, a...)
}
}

//Serve tcpserver serving
func (srv *Server) Serve(ctx context.Context) error {
if srv.handler == nil {
return fmt.Errorf("tcpserver.Handler required")
}
if srv.listener == nil {
ln, err := net.Listen(srv.network, srv.address)
if err != nil {
return err
}
log.Println(srv.name, " serving at ", srv.network, srv.address)
srv.printf("%s serving at %s:%s", srv.name, srv.network, srv.address)
srv.listener = ln
}
if srv.tlsc != nil {
Expand All @@ -108,18 +143,25 @@ func (srv *Server) Serve(ctx context.Context) error {
con, err := srv.listener.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
log.Printf("warning: accept temp err: %v", ne)
srv.errorf("accept temp err: %v", ne)
continue
}
log.Println("failed: ", err)
srv.errorf("accept failed: %v", err)
return err
}

srv.wgroup.Add(1)
go func() {
defer srv.wgroup.Done()
if srv.traced {
tr := trace.New("client", con.RemoteAddr().String())
ctx = trace.NewContext(ctx, tr)
}
if err := srv.handler(ctx, con); err != nil {
log.Printf("connection %s handle failed: %s\n", con.RemoteAddr().String(), err)
srv.errorf("client (%s) failed: %v", con.RemoteAddr().String(), err)
}
if tr, ok := trace.FromContext(ctx); ok {
tr.Finish()
}
}()
}
Expand All @@ -130,4 +172,10 @@ func (srv *Server) Serve(ctx context.Context) error {
func (srv *Server) Close() {
_ = srv.listener.Close()
srv.wgroup.Wait()
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.events != nil {
srv.events.Finish()
srv.events = nil
}
}

0 comments on commit 52743fa

Please sign in to comment.