Unverified Commit 6de78d9d authored by 望哥's avatar 望哥 Committed by GitHub

Merge pull request #24 from pantianying/pty_getty

Fix: cpu 100% in session.handleLoop
parents 0659b79f 82a97b4a
...@@ -88,6 +88,8 @@ type session struct { ...@@ -88,6 +88,8 @@ type session struct {
// goroutines sync // goroutines sync
grNum int32 grNum int32
// read goroutines done signal
rDone chan struct{}
lock sync.RWMutex lock sync.RWMutex
} }
...@@ -105,6 +107,8 @@ func newSession(endPoint EndPoint, conn Connection) *session { ...@@ -105,6 +107,8 @@ func newSession(endPoint EndPoint, conn Connection) *session {
done: make(chan struct{}), done: make(chan struct{}),
wait: pendingDuration, wait: pendingDuration,
attrs: NewValuesContext(nil), attrs: NewValuesContext(nil),
rDone: make(chan struct{}),
grNum: 0,
} }
ss.Connection.setSession(ss) ss.Connection.setSession(ss)
...@@ -145,6 +149,7 @@ func (s *session) Reset() { ...@@ -145,6 +149,7 @@ func (s *session) Reset() {
s.period = period s.period = period
s.wait = pendingDuration s.wait = pendingDuration
s.attrs = NewValuesContext(nil) s.attrs = NewValuesContext(nil)
s.rDone = make(chan struct{})
s.grNum = 0 s.grNum = 0
s.SetWriteTimeout(netIOTimeout) s.SetWriteTimeout(netIOTimeout)
...@@ -487,8 +492,6 @@ func (s *session) handleLoop() { ...@@ -487,8 +492,6 @@ func (s *session) handleLoop() {
) )
defer func() { defer func() {
var grNum int32
if r := recover(); r != nil { if r := recover(); r != nil {
const size = 64 << 10 const size = 64 << 10
rBuf := make([]byte, size) rBuf := make([]byte, size)
...@@ -496,7 +499,7 @@ func (s *session) handleLoop() { ...@@ -496,7 +499,7 @@ func (s *session) handleLoop() {
log.Errorf("[session.handleLoop] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf) log.Errorf("[session.handleLoop] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf)
} }
grNum = atomic.AddInt32(&(s.grNum), -1) grNum := atomic.AddInt32(&(s.grNum), -1)
s.listener.OnClose(s) s.listener.OnClose(s)
log.Info("%s, [session.handleLoop] goroutine exit now, left gr num %d", s.Stat(), grNum) log.Info("%s, [session.handleLoop] goroutine exit now, left gr num %d", s.Stat(), grNum)
s.gc() s.gc()
...@@ -511,7 +514,7 @@ LOOP: ...@@ -511,7 +514,7 @@ LOOP:
select { select {
case <-s.done: case <-s.done:
// this case branch assure the (session)handleLoop gr will exit before (session)handlePackage gr. // this case branch assure the (session)handleLoop gr will exit before (session)handlePackage gr.
if atomic.LoadInt32(&(s.grNum)) == 1 { // make sure @(session)handlePackage goroutine has been closed. <-s.rDone
if len(s.wQ) == 0 { if len(s.wQ) == 0 {
log.Infof("%s, [session.handleLoop] got done signal. wQ is nil.", s.Stat()) log.Infof("%s, [session.handleLoop] got done signal. wQ is nil.", s.Stat())
break LOOP break LOOP
...@@ -522,8 +525,6 @@ LOOP: ...@@ -522,8 +525,6 @@ LOOP:
log.Infof("%s, [session.handleLoop] got done signal ", s.Stat()) log.Infof("%s, [session.handleLoop] got done signal ", s.Stat())
break LOOP break LOOP
} }
}
case outPkg = <-s.wQ: case outPkg = <-s.wQ:
if flag { if flag {
if err = s.writer.Write(s, outPkg); err != nil { if err = s.writer.Write(s, outPkg); err != nil {
...@@ -568,8 +569,6 @@ func (s *session) handlePackage() { ...@@ -568,8 +569,6 @@ func (s *session) handlePackage() {
) )
defer func() { defer func() {
var grNum int32
if r := recover(); r != nil { if r := recover(); r != nil {
const size = 64 << 10 const size = 64 << 10
rBuf := make([]byte, size) rBuf := make([]byte, size)
...@@ -577,7 +576,8 @@ func (s *session) handlePackage() { ...@@ -577,7 +576,8 @@ func (s *session) handlePackage() {
log.Errorf("[session.handlePackage] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf) log.Errorf("[session.handlePackage] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf)
} }
grNum = atomic.AddInt32(&(s.grNum), -1) close(s.rDone)
grNum := atomic.AddInt32(&(s.grNum), -1)
log.Infof("%s, [session.handlePackage] gr will exit now, left gr num %d", s.sessionToken(), grNum) log.Infof("%s, [session.handlePackage] gr will exit now, left gr num %d", s.sessionToken(), grNum)
s.stop() s.stop()
if err != nil { if err != nil {
......
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