Commit cef46d37 authored by alexstocks's avatar alexstocks

add (Session)clear

parent 05ebc1c5
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
## develop history ## ## develop history ##
--- ---
- 2016/09/07
> 1 session.go:(Session)Close() -> session.go:(Session)clear() to be invoked by session.go:(Session)handleLoop
- 2016/09/06 - 2016/09/06
> 1 codec.go:(Reader)Read(*Session, []byte) (interface{}, error) -> codec.go:(Reader)Read(*Session, []byte) (interface{}, int, error) > 1 codec.go:(Reader)Read(*Session, []byte) (interface{}, error) -> codec.go:(Reader)Read(*Session, []byte) (interface{}, int, error)
> >
......
...@@ -280,13 +280,12 @@ func (this *Session) RemoveAttribute(key string) { ...@@ -280,13 +280,12 @@ func (this *Session) RemoveAttribute(key string) {
} }
func (this *Session) sessionToken() string { func (this *Session) sessionToken() string {
return fmt.Sprintf( var localAddr, peerAddr string
"{%s, %d, %s <-> %s}", if this.conn != nil {
this.name, localAddr = this.conn.LocalAddr().String()
this.ID, peerAddr = this.conn.RemoteAddr().String()
this.conn.LocalAddr().String(), }
this.conn.RemoteAddr().String(), return fmt.Sprintf("{%s, %d, %s <-> %s}", this.name, this.ID, localAddr, peerAddr)
)
} }
// Queued write, for handler // Queued write, for handler
...@@ -361,7 +360,7 @@ func (this *Session) handleLoop() { ...@@ -361,7 +360,7 @@ func (this *Session) handleLoop() {
grNum = atomic.AddInt32(&(this.grNum), -1) grNum = atomic.AddInt32(&(this.grNum), -1)
this.listener.OnClose(this) this.listener.OnClose(this)
log.Info("statistic{%s}, [session.handleLoop] goroutine exit now, left gr num %d", this.Stat(), grNum) log.Info("statistic{%s}, [session.handleLoop] goroutine exit now, left gr num %d", this.Stat(), grNum)
this.Close() this.gc()
}() }()
ticker = time.NewTicker(this.peroid) ticker = time.NewTicker(this.peroid)
...@@ -509,20 +508,15 @@ func (this *Session) stop() { ...@@ -509,20 +508,15 @@ func (this *Session) stop() {
} }
} }
// this function will be invoked by NewSessionCallback(if return error is not nil) or (Session)handleLoop automatically. func (this *Session) gc() {
// It is goroutine-safe to be invoked many times.
func (this *Session) Close() error {
this.stop()
log.Info("%s closed now, its current gr num %d",
this.sessionToken(), atomic.LoadInt32(&(this.grNum)))
this.lock.Lock() this.lock.Lock()
if this.attrs != nil { if this.attrs != nil {
this.attrs = nil this.attrs = nil
select { // select {
case <-this.readerDone: // case <-this.readerDone:
default: // default:
close(this.readerDone) // close(this.readerDone)
} // }
close(this.wQ) close(this.wQ)
this.wQ = nil this.wQ = nil
close(this.rQ) close(this.rQ)
...@@ -530,6 +524,12 @@ func (this *Session) Close() error { ...@@ -530,6 +524,12 @@ func (this *Session) Close() error {
this.gettyConn.close((int)((int64)(this.wait))) this.gettyConn.close((int)((int64)(this.wait)))
} }
this.lock.Unlock() this.lock.Unlock()
}
return nil // this function will be invoked by NewSessionCallback(if return error is not nil) or (Session)handleLoop automatically.
// It is goroutine-safe to be invoked many times.
func (this *Session) Close() {
this.stop()
log.Info("%s closed now, its current gr num %d",
this.sessionToken(), atomic.LoadInt32(&(this.grNum)))
} }
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