Skip to content

Commit

Permalink
Merge pull request TarsCloud#259 from defool/master
Browse files Browse the repository at this point in the history
Fix panic when invoke timeout
  • Loading branch information
ruanshudong authored Nov 16, 2020
2 parents a2ac329 ef07d46 commit 7301d63
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions tars/transport/tarsserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,6 @@ import (
"time"

"github.com/TarsCloud/TarsGo/tars/util/rogger"
"github.com/TarsCloud/TarsGo/tars/util/rtimer"
)

// TLOG is logger for transport.
Expand Down Expand Up @@ -120,17 119,17 @@ func (ts *TarsServer) invoke(ctx context.Context, pkg []byte) []byte {
if cfg.HandleTimeout == 0 {
rsp = ts.svr.Invoke(ctx, pkg)
} else {
done := make(chan struct{})
invokeDone, cancelFunc := context.WithTimeout(context.Background(), cfg.HandleTimeout)
go func() {
rsp = ts.svr.Invoke(ctx, pkg)
done <- struct{}{}
cancelFunc()
}()
select {
case <-rtimer.After(cfg.HandleTimeout):
rsp = ts.svr.InvokeTimeout(pkg)
case <-done:
case <-invokeDone.Done():
if len(rsp) == 0 { // The rsp must be none-empty
rsp = ts.svr.InvokeTimeout(pkg)
}
}
close(done)
}
return rsp
}

0 comments on commit 7301d63

Please sign in to comment.