@@ -8,13 +8,13 @@ import (
88 "net"
99 "regexp"
1010 "strconv"
11+ "sync"
1112 "time"
1213
1314 "github.com/didi/sharingan/replayer-agent/common/handlers/conf"
1415 "github.com/didi/sharingan/replayer-agent/common/handlers/tlog"
1516 "github.com/didi/sharingan/replayer-agent/logic/outbound/match"
1617 "github.com/didi/sharingan/replayer-agent/model/pool"
17- "github.com/didi/sharingan/replayer-agent/model/recording"
1818 "github.com/didi/sharingan/replayer-agent/model/replaying"
1919 "github.com/didi/sharingan/replayer-agent/utils/helper"
2020 "go.uber.org/zap/zapcore"
2929 errMissMatchTalk = errors .New ("MISS match" )
3030)
3131
32+ var traceMap sync.Map
33+
34+ type SessionInfo struct {
35+ sync.Mutex
36+ LastMatchedIndex int
37+ }
38+
39+ // ClearSession 回放结束后掉一下这个方法。
40+ func ClearSession (traceID string ) {
41+ traceMap .Delete (traceID )
42+ }
43+
44+ func getLastMatchedIndex (traceID string ) int {
45+ actual , _ := traceMap .LoadOrStore (traceID , & SessionInfo {LastMatchedIndex : - 1 })
46+ session := actual .(* SessionInfo )
47+ session .Lock ()
48+ defer session .Unlock ()
49+ return session .LastMatchedIndex
50+ }
51+
52+ func setLastMatchedIndex (traceID string , matchedIndex int ) {
53+ actual , _ := traceMap .Load (traceID )
54+ session := actual .(* SessionInfo )
55+ session .Lock ()
56+ defer session .Unlock ()
57+ session .LastMatchedIndex = matchedIndex
58+ }
59+
3260// ConnState 连接管理
3361type ConnState struct {
34- LastMatchedIndex int
3562
3663 // 原始连接信息
3764 conn * net.TCPConn
@@ -168,7 +195,7 @@ func (cs *ConnState) rmTrafixPrefix(ctx context.Context, request []byte) []byte
168195func (cs * ConnState ) match (ctx context.Context , request []byte ) error {
169196 quotedRequest := strconv .Quote (helper .BytesToString (request ))
170197
171- cs .Handler = loadHandler (ctx , string ( cs .traceID ) )
198+ cs .Handler = loadHandler (ctx , cs .traceID )
172199 if cs .Handler == nil {
173200 tlog .Handler .Warnf (ctx , tlog .DebugTag , "errmsg=find Handler failed||request=%s||traceID=%s" , quotedRequest , string (cs .traceID ))
174201 return nil
@@ -194,12 +221,11 @@ func (cs *ConnState) match(ctx context.Context, request []byte) error {
194221 return err
195222 }
196223
197- var matchedTalk * recording.CallOutbound
198- var mark float64
199- cs .LastMatchedIndex , mark , matchedTalk = cs .Handler .Matcher .MatchOutboundTalk (ctx , cs .Handler .ReplayingSession , cs .LastMatchedIndex , request )
224+ lastMatchedIndex := getLastMatchedIndex (cs .traceID )
225+ matchedIndex , mark , matchedTalk := cs .Handler .Matcher .MatchOutboundTalk (ctx , cs .Handler .ReplayingSession , lastMatchedIndex , request )
200226 if callOutbound .MatchedActionIndex != fakeIndexSimulated {
201- if matchedTalk == nil && cs . LastMatchedIndex != 0 {
202- cs . LastMatchedIndex , mark , matchedTalk = cs .Handler .Matcher .MatchOutboundTalk (ctx , cs .Handler .ReplayingSession , - 1 , request )
227+ if matchedTalk == nil && matchedIndex != 0 {
228+ matchedIndex , mark , matchedTalk = cs .Handler .Matcher .MatchOutboundTalk (ctx , cs .Handler .ReplayingSession , - 1 , request )
203229 }
204230 if matchedTalk == nil {
205231 callOutbound .MatchedRequest = nil
@@ -254,6 +280,7 @@ func (cs *ConnState) match(ctx context.Context, request []byte) error {
254280 callOutbound .MatchedActionIndex ,
255281 strconv .Quote (helper .BytesToString (callOutbound .MatchedResponse )))
256282
283+ setLastMatchedIndex (cs .traceID , matchedIndex )
257284 return nil
258285}
259286
0 commit comments