Commit 4a0f0138 authored by alexstocks's avatar alexstocks

delete session.go:(Session)handleLoop Last clause

parent e631ec31
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
- 2016/09/09 - 2016/09/09
> 1 delete session.go:(Session)readerDone > 1 delete session.go:(Session)readerDone
> >
> 2 delete session.go:(Session)handlePackage Last clause
>
> 3 version: 0.3.07 > 3 version: 0.3.07
- 2016/09/08 - 2016/09/08
......
...@@ -348,12 +348,14 @@ func (this *Session) RunEventLoop() { ...@@ -348,12 +348,14 @@ func (this *Session) RunEventLoop() {
func (this *Session) handleLoop() { func (this *Session) handleLoop() {
var ( var (
err error err error
flag bool
// start time.Time // start time.Time
counter CountWatch counter CountWatch
ticker *time.Ticker ticker *time.Ticker
inPkg interface{} inPkg interface{}
outPkg interface{} outPkg interface{}
once sync.Once
) )
defer func() { defer func() {
...@@ -372,62 +374,49 @@ func (this *Session) handleLoop() { ...@@ -372,62 +374,49 @@ func (this *Session) handleLoop() {
this.gc() this.gc()
}() }()
flag = true // do not do any read/write/cron operation while got write error
ticker = time.NewTicker(this.peroid) ticker = time.NewTicker(this.peroid)
LOOP: LOOP:
for { for {
select { select {
case <-this.done: case <-this.done:
// 这个分支确保(Session)handleLoop gr在(Session)handlePackage gr之后退出
once.Do(func() { ticker.Stop() })
log.Info("%s, [session.handleLoop] got done signal ", this.Stat()) log.Info("%s, [session.handleLoop] got done signal ", this.Stat())
break LOOP if atomic.LoadInt32(&(this.grNum)) == 1 { // make sure @(Session)handlePackage goroutine has been closed.
counter.Start()
// if time.Since(start).Nanoseconds() >= this.wait.Nanoseconds() {
if counter.Count() > this.wait.Nanoseconds() {
break LOOP
}
}
case inPkg = <-this.rQ: case inPkg = <-this.rQ:
this.listener.OnMessage(this, inPkg) // 这个条件分支通过(Session)rQ排空确保(Session)handlePackage gr不会阻塞在(Session)rQ上
this.incReadPkgCount() if flag {
this.listener.OnMessage(this, inPkg)
this.incReadPkgCount()
}
case outPkg = <-this.wQ: case outPkg = <-this.wQ:
if err = this.pkgHandler.Write(this, outPkg); err != nil { if flag {
log.Error("%s, [session.handleLoop] = error{%+v}", this.sessionToken(), err) if err = this.pkgHandler.Write(this, outPkg); err != nil {
this.stop() log.Error("%s, [session.handleLoop] = error{%+v}", this.sessionToken(), err)
break LOOP this.stop()
flag = false
// break LOOP
}
this.incWritePkgCount()
} }
this.incWritePkgCount()
case <-ticker.C: case <-ticker.C:
this.listener.OnCron(this) if flag {
} this.listener.OnCron(this)
}
ticker.Stop()
// wait for reader goroutine closed
// <-this.readerDone
// process pending pkg
// start = time.Now()
counter.Start()
LAST:
for {
if atomic.LoadInt32(&(this.grNum)) == 1 {
counter.Start()
// if time.Since(start).Nanoseconds() >= this.wait.Nanoseconds() {
if counter.Count() > this.wait.Nanoseconds() {
break
}
}
select {
case outPkg = <-this.wQ:
if err = this.pkgHandler.Write(this, outPkg); err != nil {
break LAST
} }
this.incWritePkgCount()
case inPkg = <-this.rQ:
this.listener.OnMessage(this, inPkg)
this.incReadPkgCount()
default:
log.Info("%s, [session.handleLoop] default", this.sessionToken())
break LAST
} }
} }
once.Do(func() { ticker.Stop() })
// ticker.Stop()
} }
// get package from tcp stream(packet) // get package from tcp stream(packet)
......
...@@ -10,5 +10,5 @@ ...@@ -10,5 +10,5 @@
package getty package getty
var ( var (
Version = "0.3.06" Version = "0.3.07"
) )
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