Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cpu 100% in session.handleLoop #24

Merged
merged 6 commits into from
Sep 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type session struct {

// goroutines sync
grNum int32
// read goroutines done signal
rDone chan struct{}
lock sync.RWMutex
}

Expand All @@ -105,6 +107,8 @@ func newSession(endPoint EndPoint, conn Connection) *session {
done: make(chan struct{}),
wait: pendingDuration,
attrs: NewValuesContext(nil),
rDone: make(chan struct{}),
grNum: 0,
}

ss.Connection.setSession(ss)
Expand Down Expand Up @@ -145,6 +149,7 @@ func (s *session) Reset() {
s.period = period
s.wait = pendingDuration
s.attrs = NewValuesContext(nil)
s.rDone = make(chan struct{})
s.grNum = 0

s.SetWriteTimeout(netIOTimeout)
Expand Down Expand Up @@ -487,16 +492,14 @@ func (s *session) handleLoop() {
)

defer func() {
var grNum int32

if r := recover(); r != nil {
const size = 64 << 10
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
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)
log.Info("%s, [session.handleLoop] goroutine exit now, left gr num %d", s.Stat(), grNum)
s.gc()
Expand All @@ -511,19 +514,17 @@ LOOP:
select {
case <-s.done:
// 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.
if len(s.wQ) == 0 {
log.Infof("%s, [session.handleLoop] got done signal. wQ is nil.", s.Stat())
break LOOP
}
counter.Start()
// if time.Since(start).Nanoseconds() >= s.wait.Nanoseconds() {
if counter.Count() > s.wait.Nanoseconds() {
log.Infof("%s, [session.handleLoop] got done signal ", s.Stat())
break LOOP
}
<-s.rDone
if len(s.wQ) == 0 {
log.Infof("%s, [session.handleLoop] got done signal. wQ is nil.", s.Stat())
break LOOP
}
counter.Start()
// if time.Since(start).Nanoseconds() >= s.wait.Nanoseconds() {
if counter.Count() > s.wait.Nanoseconds() {
log.Infof("%s, [session.handleLoop] got done signal ", s.Stat())
break LOOP
}

case outPkg = <-s.wQ:
if flag {
if err = s.writer.Write(s, outPkg); err != nil {
Expand Down Expand Up @@ -568,16 +569,15 @@ func (s *session) handlePackage() {
)

defer func() {
var grNum int32

if r := recover(); r != nil {
const size = 64 << 10
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
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)
s.stop()
if err != nil {
Expand Down