Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit cd7b9ca

Browse files
authored
Merge pull request #23 from lvan100/master
使用全局状态保存 trace 对应的信息
2 parents c006ab7 + 5b7b959 commit cd7b9ca

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

replayer-agent/logic/outbound/connection.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -29,9 +29,36 @@ var (
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 连接管理
3361
type 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
168195
func (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

replayer-agent/logic/outbound/outbound.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ func handleOutbound(serverAddr *net.TCPAddr, conn *net.TCPConn) {
103103
tlog.Handler.Debugf(ctx, tlog.DebugTag, "new outbound||addr=%s||begin", tcpAddr.String())
104104

105105
cs := &ConnState{
106-
LastMatchedIndex: -1,
107-
conn: conn,
108-
tcpAddr: tcpAddr,
109-
proxyer: NewProxyer(conn),
106+
conn: conn,
107+
tcpAddr: tcpAddr,
108+
proxyer: NewProxyer(conn),
110109
}
111110

112111
for i := 0; ; i++ {

0 commit comments

Comments
 (0)