Skip to content
Permalink
Browse files
Ipm: use dubbogo log lib
  • Loading branch information
Lvnszn committed Dec 15, 2021
1 parent 66e083f commit de68c227d874eb4f8355256263d0d4770f8cb856
Showing 47 changed files with 218 additions and 339 deletions.
@@ -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() {
@@ -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()
}
@@ -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
}
@@ -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()
}
@@ -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)
@@ -26,7 +26,6 @@ import (
)

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

@@ -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)
@@ -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
@@ -25,10 +25,6 @@ import (
"unsafe"
)

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

////////////////////////////////////////////
// echo command
////////////////////////////////////////////
@@ -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) {
@@ -23,7 +23,6 @@ import (
)

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

@@ -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
@@ -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"
)
@@ -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()

@@ -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))
}()
@@ -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
}
@@ -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
}
}
@@ -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)
}
@@ -24,7 +24,6 @@ import (
)

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

@@ -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
}
@@ -26,7 +26,6 @@ import (
)

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

@@ -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)
@@ -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
}
@@ -25,10 +25,6 @@ import (
"unsafe"
)

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

////////////////////////////////////////////
// echo command
////////////////////////////////////////////
@@ -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) {
@@ -24,7 +24,6 @@ import (
)

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

@@ -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
@@ -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
@@ -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()
@@ -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)
@@ -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)
}
}

0 comments on commit de68c22

Please sign in to comment.