Commit 3a269e7a authored by AlexStocks's avatar AlexStocks

add keep alive period & write pkg timeout

parent 1af7c15f
...@@ -10,6 +10,15 @@ ...@@ -10,6 +10,15 @@
## develop history ## ## develop history ##
--- ---
- 2018/03/14
> improvement
* add keep alive period
* add write package timeout
- 2018/03/10
> improvement
* use getty 0.8.1
- 2016/11/19 - 2016/11/19
> 1 add client/app/config.go:GettySessionParam{CompressEncoding} to test compress websocket compression extension. > 1 add client/app/config.go:GettySessionParam{CompressEncoding} to test compress websocket compression extension.
> >
......
...@@ -155,7 +155,7 @@ func (this *EchoClient) heartbeat(session getty.Session) { ...@@ -155,7 +155,7 @@ func (this *EchoClient) heartbeat(session getty.Session) {
pkg.B = echoHeartbeatRequestString pkg.B = echoHeartbeatRequestString
pkg.H.Len = (uint16)(len(pkg.B) + 1) pkg.H.Len = (uint16)(len(pkg.B) + 1)
if err := session.WritePkg(&pkg); err != nil { if err := session.WritePkg(&pkg, WritePkgTimeout); err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err) log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close() session.Close()
......
...@@ -35,6 +35,8 @@ type ( ...@@ -35,6 +35,8 @@ type (
CompressEncoding bool `default:"false"` CompressEncoding bool `default:"false"`
TcpNoDelay bool `default:"true"` TcpNoDelay bool `default:"true"`
TcpKeepAlive bool `default:"true"` TcpKeepAlive bool `default:"true"`
KeepAlivePeriod string `default:"180s"`
keepAlivePeriod time.Duration
TcpRBufSize int `default:"262144"` TcpRBufSize int `default:"262144"`
TcpWBufSize int `default:"65536"` TcpWBufSize int `default:"65536"`
PkgRQSize int `default:"1024"` PkgRQSize int `default:"1024"`
...@@ -123,6 +125,11 @@ func initConf() { ...@@ -123,6 +125,11 @@ func initConf() {
panic(fmt.Sprintf("time.ParseDuration(FailFastTimeout{%#v}) = error{%v}", conf.FailFastTimeout, err)) panic(fmt.Sprintf("time.ParseDuration(FailFastTimeout{%#v}) = error{%v}", conf.FailFastTimeout, err))
return return
} }
conf.GettySessionParam.keepAlivePeriod, err = time.ParseDuration(conf.GettySessionParam.KeepAlivePeriod)
if err != nil {
panic(fmt.Sprintf("time.ParseDuration(KeepAlivePeriod{%#v}) = error{%v}", conf.GettySessionParam.KeepAlivePeriod, err))
return
}
conf.GettySessionParam.tcpReadTimeout, err = time.ParseDuration(conf.GettySessionParam.TcpReadTimeout) conf.GettySessionParam.tcpReadTimeout, err = time.ParseDuration(conf.GettySessionParam.TcpReadTimeout)
if err != nil { if err != nil {
panic(fmt.Sprintf("time.ParseDuration(TcpReadTimeout{%#v}) = error{%v}", conf.GettySessionParam.TcpReadTimeout, err)) panic(fmt.Sprintf("time.ParseDuration(TcpReadTimeout{%#v}) = error{%v}", conf.GettySessionParam.TcpReadTimeout, err))
......
...@@ -35,6 +35,10 @@ const ( ...@@ -35,6 +35,10 @@ const (
pprofPath = "/debug/pprof/" pprofPath = "/debug/pprof/"
) )
const (
WritePkgTimeout = 1e8
)
var ( var (
client EchoClient client EchoClient
) )
...@@ -85,6 +89,9 @@ func newSession(session getty.Session) error { ...@@ -85,6 +89,9 @@ func newSession(session getty.Session) error {
tcpConn.SetNoDelay(conf.GettySessionParam.TcpNoDelay) tcpConn.SetNoDelay(conf.GettySessionParam.TcpNoDelay)
tcpConn.SetKeepAlive(conf.GettySessionParam.TcpKeepAlive) tcpConn.SetKeepAlive(conf.GettySessionParam.TcpKeepAlive)
if conf.GettySessionParam.TcpKeepAlive {
tcpConn.SetKeepAlivePeriod(conf.GettySessionParam.keepAlivePeriod)
}
tcpConn.SetReadBuffer(conf.GettySessionParam.TcpRBufSize) tcpConn.SetReadBuffer(conf.GettySessionParam.TcpRBufSize)
tcpConn.SetWriteBuffer(conf.GettySessionParam.TcpWBufSize) tcpConn.SetWriteBuffer(conf.GettySessionParam.TcpWBufSize)
...@@ -155,7 +162,7 @@ func echo() { ...@@ -155,7 +162,7 @@ func echo() {
pkg.H.Len = (uint16)(len(pkg.B)) + 1 pkg.H.Len = (uint16)(len(pkg.B)) + 1
if session := client.selectSession(); session != nil { if session := client.selectSession(); session != nil {
err := session.WritePkg(&pkg) err := session.WritePkg(&pkg, WritePkgTimeout)
if err != nil { if err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err) log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close() session.Close()
......
...@@ -38,6 +38,7 @@ FailFastTimeout = "3s" ...@@ -38,6 +38,7 @@ FailFastTimeout = "3s"
CompressEncoding = true CompressEncoding = true
TcpNoDelay = true TcpNoDelay = true
TcpKeepAlive = true TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144 TcpRBufSize = 262144
TcpWBufSize = 65536 TcpWBufSize = 65536
PkgRQSize = 512 PkgRQSize = 512
......
<logging> <logging>
<filter enabled="false"> <filter enabled="true">
<tag>stdout</tag> <tag>stdout</tag>
<type>console</type> <type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) --> <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
......
...@@ -35,6 +35,8 @@ type ( ...@@ -35,6 +35,8 @@ type (
CompressEncoding bool `default:"false"` CompressEncoding bool `default:"false"`
TcpNoDelay bool `default:"true"` TcpNoDelay bool `default:"true"`
TcpKeepAlive bool `default:"true"` TcpKeepAlive bool `default:"true"`
KeepAlivePeriod string `default:"180s"`
keepAlivePeriod time.Duration
TcpRBufSize int `default:"262144"` TcpRBufSize int `default:"262144"`
TcpWBufSize int `default:"65536"` TcpWBufSize int `default:"65536"`
PkgRQSize int `default:"1024"` PkgRQSize int `default:"1024"`
...@@ -88,6 +90,7 @@ func initConf() { ...@@ -88,6 +90,7 @@ func initConf() {
} }
conf = new(Config) conf = new(Config)
config.MustLoadWithPath(confFile, conf) config.MustLoadWithPath(confFile, conf)
conf.sessionTimeout, err = time.ParseDuration(conf.SessionTimeout) conf.sessionTimeout, err = time.ParseDuration(conf.SessionTimeout)
if err != nil { if err != nil {
panic(fmt.Sprintf("time.ParseDuration(SessionTimeout{%#v}) = error{%v}", conf.SessionTimeout, err)) panic(fmt.Sprintf("time.ParseDuration(SessionTimeout{%#v}) = error{%v}", conf.SessionTimeout, err))
...@@ -98,6 +101,11 @@ func initConf() { ...@@ -98,6 +101,11 @@ func initConf() {
panic(fmt.Sprintf("time.ParseDuration(FailFastTimeout{%#v}) = error{%v}", conf.FailFastTimeout, err)) panic(fmt.Sprintf("time.ParseDuration(FailFastTimeout{%#v}) = error{%v}", conf.FailFastTimeout, err))
return return
} }
conf.GettySessionParam.keepAlivePeriod, err = time.ParseDuration(conf.GettySessionParam.KeepAlivePeriod)
if err != nil {
panic(fmt.Sprintf("time.ParseDuration(KeepAlivePeriod{%#v}) = error{%v}", conf.GettySessionParam.KeepAlivePeriod, err))
return
}
conf.GettySessionParam.tcpReadTimeout, err = time.ParseDuration(conf.GettySessionParam.TcpReadTimeout) conf.GettySessionParam.tcpReadTimeout, err = time.ParseDuration(conf.GettySessionParam.TcpReadTimeout)
if err != nil { if err != nil {
panic(fmt.Sprintf("time.ParseDuration(TcpReadTimeout{%#v}) = error{%v}", conf.GettySessionParam.TcpReadTimeout, err)) panic(fmt.Sprintf("time.ParseDuration(TcpReadTimeout{%#v}) = error{%v}", conf.GettySessionParam.TcpReadTimeout, err))
......
...@@ -20,6 +20,10 @@ import ( ...@@ -20,6 +20,10 @@ import (
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
) )
const (
WritePkgTimeout = 1e8
)
var ( var (
errTooManySessions = errors.New("Too many echo sessions!") errTooManySessions = errors.New("Too many echo sessions!")
) )
...@@ -42,7 +46,7 @@ func (this *HeartbeatHandler) Handle(session getty.Session, pkg *EchoPackage) er ...@@ -42,7 +46,7 @@ func (this *HeartbeatHandler) Handle(session getty.Session, pkg *EchoPackage) er
rspPkg.B = echoHeartbeatResponseString rspPkg.B = echoHeartbeatResponseString
rspPkg.H.Len = uint16(len(rspPkg.B) + 1) rspPkg.H.Len = uint16(len(rspPkg.B) + 1)
return session.WritePkg(&rspPkg) return session.WritePkg(&rspPkg, WritePkgTimeout)
} }
//////////////////////////////////////////// ////////////////////////////////////////////
...@@ -53,7 +57,7 @@ type MessageHandler struct{} ...@@ -53,7 +57,7 @@ type MessageHandler struct{}
func (this *MessageHandler) Handle(session getty.Session, pkg *EchoPackage) error { func (this *MessageHandler) Handle(session getty.Session, pkg *EchoPackage) error {
log.Debug("get echo package{%s}", pkg) log.Debug("get echo package{%s}", pkg)
return session.WritePkg(pkg) return session.WritePkg(pkg, WritePkgTimeout)
} }
//////////////////////////////////////////// ////////////////////////////////////////////
......
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"github.com/AlexStocks/goext/log" "github.com/AlexStocks/goext/log"
"github.com/AlexStocks/goext/net" "github.com/AlexStocks/goext/net"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
"github.com/pingcap/tidb/ast"
) )
const ( const (
...@@ -90,6 +91,9 @@ func newSession(session getty.Session) error { ...@@ -90,6 +91,9 @@ func newSession(session getty.Session) error {
tcpConn.SetNoDelay(conf.GettySessionParam.TcpNoDelay) tcpConn.SetNoDelay(conf.GettySessionParam.TcpNoDelay)
tcpConn.SetKeepAlive(conf.GettySessionParam.TcpKeepAlive) tcpConn.SetKeepAlive(conf.GettySessionParam.TcpKeepAlive)
if conf.GettySessionParam.TcpKeepAlive {
tcpConn.SetKeepAlivePeriod(conf.GettySessionParam.keepAlivePeriod)
}
tcpConn.SetReadBuffer(conf.GettySessionParam.TcpRBufSize) tcpConn.SetReadBuffer(conf.GettySessionParam.TcpRBufSize)
tcpConn.SetWriteBuffer(conf.GettySessionParam.TcpWBufSize) tcpConn.SetWriteBuffer(conf.GettySessionParam.TcpWBufSize)
......
...@@ -22,6 +22,7 @@ FailFastTimeout = "3s" ...@@ -22,6 +22,7 @@ FailFastTimeout = "3s"
CompressEncoding = true CompressEncoding = true
TcpNoDelay = true TcpNoDelay = true
TcpKeepAlive = true TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144 TcpRBufSize = 262144
TcpWBufSize = 524288 TcpWBufSize = 524288
PkgRQSize = 1024 PkgRQSize = 1024
......
<logging> <logging>
<filter enabled="false"> <filter enabled="true">
<tag>stdout</tag> <tag>stdout</tag>
<type>console</type> <type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) --> <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment