Skip to content

Commit

Permalink
Ipm: use dubbogo log lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Lvnszn committed Dec 15, 2021
1 parent 66e083f commit de68c22
Show file tree
Hide file tree
Showing 47 changed files with 218 additions and 339 deletions.
12 changes: 6 additions & 6 deletions examples/echo/tcp-echo/client/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
getty "github.com/apache/dubbo-getty"
)

var (
reqID uint32
src = rand.NewSource(time.Now().UnixNano())
log = getty.GetLogger()
)

func init() {
Expand Down Expand Up @@ -63,7 +63,7 @@ func (c *EchoClient) close() {
c.gettyClient.Close()
c.gettyClient = nil
for _, s := range c.sessions {
log.Info("close client session{%s, last active:%s, request number:%d}",
log.Infof("close client session{%s, last active:%s, request number:%d}",
s.session.Stat(), s.session.GetActive().String(), s.reqNum)
s.session.Close()
}
Expand All @@ -85,7 +85,7 @@ func (c *EchoClient) selectSession() getty.Session {
}

func (c *EchoClient) addSession(session getty.Session) {
log.Debug("add session{%s}", session.Stat())
log.Debugf("add session{%s}", session.Stat())
if session == nil {
return
}
Expand All @@ -105,11 +105,11 @@ func (c *EchoClient) removeSession(session getty.Session) {
for i, s := range c.sessions {
if s.session == session {
c.sessions = append(c.sessions[:i], c.sessions[i+1:]...)
log.Debug("delete session{%s}, its index{%d}", session.Stat(), i)
log.Debugf("delete session{%s}, its index{%d}", session.Stat(), i)
break
}
}
log.Info("after remove session{%s}, left session number:%d", session.Stat(), len(c.sessions))
log.Infof("after remove session{%s}, left session number:%d", session.Stat(), len(c.sessions))

c.lock.Unlock()
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *EchoClient) heartbeat(session getty.Session) {
pkg.H.Len = (uint16)(len(pkg.B) + 1)

if _, _, err := session.WritePkg(&pkg, WritePkgTimeout); err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
log.Warnf("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close()

c.removeSession(session)
Expand Down
3 changes: 0 additions & 3 deletions examples/echo/tcp-echo/client/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -156,7 +155,6 @@ func initConf() {
panic(fmt.Sprintf("time.ParseDuration(WaitTimeout{%#v}) = error{%v}", conf.GettySessionParam.WaitTimeout, err))
return
}
// gxlog.CInfo("config{%#v}\n", conf)

// log
confFile = os.Getenv(APP_LOG_CONF_FILE)
Expand All @@ -168,7 +166,6 @@ func initConf() {
panic(fmt.Sprintf("log configure file name{%v} suffix must be .xml", confFile))
return
}
log.LoadConfiguration(confFile)
log.Info("config{%#v}", conf)

return
Expand Down
6 changes: 1 addition & 5 deletions examples/echo/tcp-echo/client/app/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"unsafe"
)

import (
log "github.com/AlexStocks/log4go"
)

////////////////////////////////////////////
// echo command
////////////////////////////////////////////
Expand Down Expand Up @@ -131,7 +127,7 @@ func (p *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
return 0, err
}
if p.H.Magic != echoPkgMagic {
log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
log.Errorf("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
return 0, ErrIllegalMagic
}
if buf.Len() < (int)(p.H.Len) {
Expand Down
13 changes: 6 additions & 7 deletions examples/echo/tcp-echo/client/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
getty "github.com/apache/dubbo-getty"
)

Expand Down Expand Up @@ -54,34 +53,34 @@ func (h *EchoMessageHandler) OnOpen(session getty.Session) error {
}

func (h *EchoMessageHandler) OnError(session getty.Session, err error) {
log.Info("session{%s} got error{%v}, will be closed.", session.Stat(), err)
log.Infof("session{%s} got error{%v}, will be closed.", session.Stat(), err)
client.removeSession(session)
}

func (h *EchoMessageHandler) OnClose(session getty.Session) {
log.Info("session{%s} is closing......", session.Stat())
log.Infof("session{%s} is closing......", session.Stat())
client.removeSession(session)
}

func (h *EchoMessageHandler) OnMessage(session getty.Session, pkg interface{}) {
p, ok := pkg.(*EchoPackage)
if !ok {
log.Error("illegal packge{%#v}", pkg)
log.Errorf("illegal packge{%#v}", pkg)
return
}

log.Debug("get echo package{%s}", p)
log.Debugf("get echo package{%s}", p)
client.updateSession(session)
}

func (h *EchoMessageHandler) OnCron(session getty.Session) {
clientEchoSession, err := client.getClientEchoSession(session)
if err != nil {
log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
log.Errorf("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
return
}
if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() {
log.Warn("session{%s} timeout{%s}, reqNum{%d}",
log.Warnf("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(session.GetActive()).String(), clientEchoSession.reqNum)
client.removeSession(session)
return
Expand Down
18 changes: 5 additions & 13 deletions examples/echo/tcp-echo/client/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ import (
)

import (
gxlog "github.com/AlexStocks/goext/log"
gxnet "github.com/AlexStocks/goext/net"
gxtime "github.com/AlexStocks/goext/time"
log "github.com/AlexStocks/log4go"
getty "github.com/apache/dubbo-getty"
"github.com/dubbogo/gost/sync"
)
Expand Down Expand Up @@ -65,8 +63,7 @@ func main() {

taskPool = gxsync.NewTaskPoolSimple(0)
initClient()
gxlog.CInfo("%s starts successfull!", conf.AppName)
log.Info("%s starts successfull!\n", conf.AppName)
log.Infof("%s starts successfull!", conf.AppName)

go test()

Expand All @@ -77,7 +74,7 @@ func initProfiling() {
var addr string

addr = gxnet.HostAddress(conf.LocalHost, conf.ProfilePort)
log.Info("App Profiling startup on address{%v}", addr+pprofPath)
log.Infof("App Profiling startup on address{%v}", addr+pprofPath)
go func() {
log.Info(http.ListenAndServe(addr, nil))
}()
Expand Down Expand Up @@ -113,7 +110,7 @@ func newSession(session getty.Session) error {
session.SetWriteTimeout(conf.GettySessionParam.tcpWriteTimeout)
session.SetCronPeriod((int)(conf.heartbeatPeriod.Nanoseconds() / 1e6))
session.SetWaitTime(conf.GettySessionParam.waitTimeout)
log.Debug("client new session:%s\n", session.Stat())
log.Debugf("client new session:%s", session.Stat())

return nil
}
Expand Down Expand Up @@ -144,23 +141,19 @@ func initSignal() {
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
sig := <-signals
log.Info("get signal %s", sig.String())
log.Infof("get signal %s", sig.String())
switch sig {
case syscall.SIGHUP:
// reload()
default:
go time.AfterFunc(conf.failFastTimeout, func() {
// log.Warn("app exit now by force...")
// os.Exit(1)
log.Exit("app exit now by force...")
log.Close()
})

// 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出
uninitClient()
// fmt.Println("app exit now...")
log.Exit("app exit now...")
log.Close()
return
}
}
Expand Down Expand Up @@ -203,6 +196,5 @@ func test() {
echo()
}
cost = counter.Count()
log.Info("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6)
gxlog.CInfo("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6)
log.Infof("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6)
}
7 changes: 3 additions & 4 deletions examples/echo/tcp-echo/client/app/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
getty "github.com/apache/dubbo-getty"
)

Expand Down Expand Up @@ -68,15 +67,15 @@ func (h *EchoPackageHandler) Write(ss getty.Session, pkg interface{}) ([]byte, e

startTime = time.Now()
if echoPkg, ok = pkg.(*EchoPackage); !ok {
log.Error("illegal pkg:%+v\n", pkg)
log.Errorf("illegal pkg:%+v", pkg)
return nil, errors.New("invalid echo package!")
}

buf, err = echoPkg.Marshal()
if err != nil {
log.Warn("binary.Write(echoPkg{%#v}) = err{%#v}", echoPkg, err)
log.Warnf("binary.Write(echoPkg{%#v}) = err{%#v}", echoPkg, err)
return nil, err
}
log.Debug("WriteEchoPkgTimeMs = %s", time.Since(startTime).String())
log.Debugf("WriteEchoPkgTimeMs = %s", time.Since(startTime).String())
return buf.Bytes(), nil
}
5 changes: 1 addition & 4 deletions examples/echo/tcp-echo/server/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -138,7 +137,6 @@ func initConf() {
panic(fmt.Sprintf("time.ParseDuration(WaitTimeout{%#v}) = error{%v}", conf.GettySessionParam.WaitTimeout, err))
return
}
// gxlog.CInfo("config{%#v}\n", conf)

// log
confFile = os.Getenv(APP_LOG_CONF_FILE)
Expand All @@ -150,8 +148,7 @@ func initConf() {
panic(fmt.Sprintf("log configure file name{%v} suffix must be .xml", confFile))
return
}
log.LoadConfiguration(confFile)
log.Info("config{%#v}", conf)
log.Infof("config{%#v}", conf)

return
}
6 changes: 1 addition & 5 deletions examples/echo/tcp-echo/server/app/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"unsafe"
)

import (
log "github.com/AlexStocks/log4go"
)

////////////////////////////////////////////
// echo command
////////////////////////////////////////////
Expand Down Expand Up @@ -131,7 +127,7 @@ func (p *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
return 0, err
}
if p.H.Magic != echoPkgMagic {
log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
log.Errorf("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
return 0, ErrIllegalMagic
}
if buf.Len() < (int)(p.H.Len) {
Expand Down
19 changes: 9 additions & 10 deletions examples/echo/tcp-echo/server/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

import (
log "github.com/AlexStocks/log4go"
getty "github.com/apache/dubbo-getty"
)

Expand All @@ -50,7 +49,7 @@ type PackageHandler interface {
type HeartbeatHandler struct{}

func (h *HeartbeatHandler) Handle(session getty.Session, pkg *EchoPackage) error {
log.Debug("get echo heartbeat package{%s}", pkg)
log.Debugf("get echo heartbeat package{%s}", pkg)

var rspPkg EchoPackage
rspPkg.H = pkg.H
Expand All @@ -67,11 +66,11 @@ func (h *HeartbeatHandler) Handle(session getty.Session, pkg *EchoPackage) error
type MessageHandler struct{}

func (h *MessageHandler) Handle(session getty.Session, pkg *EchoPackage) error {
log.Debug("get echo package{%s}", pkg)
log.Debugf("get echo package{%s}", pkg)
// write echo message handle logic here.
_, _, err := session.WritePkg(pkg, WritePkgTimeout)
if err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
log.Warnf("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close()
}
return err
Expand Down Expand Up @@ -113,22 +112,22 @@ func (h *EchoMessageHandler) OnOpen(session getty.Session) error {
return err
}

log.Info("got session:%s", session.Stat())
log.Infof("got session:%s", session.Stat())
h.rwlock.Lock()
h.sessionMap[session] = &clientEchoSession{session: session}
h.rwlock.Unlock()
return nil
}

func (h *EchoMessageHandler) OnError(session getty.Session, err error) {
log.Info("session{%s} got error{%v}, will be closed.", session.Stat(), err)
log.Infof("session{%s} got error{%v}, will be closed.", session.Stat(), err)
h.rwlock.Lock()
delete(h.sessionMap, session)
h.rwlock.Unlock()
}

func (h *EchoMessageHandler) OnClose(session getty.Session) {
log.Info("session{%s} is closing......", session.Stat())
log.Infof("session{%s} is closing......", session.Stat())
h.rwlock.Lock()
delete(h.sessionMap, session)
h.rwlock.Unlock()
Expand All @@ -137,13 +136,13 @@ func (h *EchoMessageHandler) OnClose(session getty.Session) {
func (h *EchoMessageHandler) OnMessage(session getty.Session, pkg interface{}) {
p, ok := pkg.(*EchoPackage)
if !ok {
log.Error("illegal packge{%#v}", pkg)
log.Errorf("illegal packge{%#v}", pkg)
return
}

handler, ok := h.handlers[p.H.Command]
if !ok {
log.Error("illegal command{%d}", p.H.Command)
log.Errorf("illegal command{%d}", p.H.Command)
return
}
err := handler.Handle(session, p)
Expand All @@ -166,7 +165,7 @@ func (h *EchoMessageHandler) OnCron(session getty.Session) {
active = session.GetActive()
if conf.sessionTimeout.Nanoseconds() < time.Since(active).Nanoseconds() {
flag = true
log.Warn("session{%s} timeout{%s}, reqNum{%d}",
log.Warnf("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(active).String(), h.sessionMap[session].reqNum)
}
}
Expand Down
Loading

0 comments on commit de68c22

Please sign in to comment.