Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit c097382

Browse files
ankur22ka3de
authored andcommitted
Use the msgId defined in connection in session
The last piece of the puzzle in ensuring that all msgIds are unique at the connection scope level is to ensure that same msgId type is used in the session that is associated to the connection.
1 parent 11c33a2 commit c097382

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

common/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (c *Connection) recvLoop() {
329329
sid, tid := eva.SessionID, eva.TargetInfo.TargetID
330330

331331
c.sessionsMu.Lock()
332-
session := NewSession(c.ctx, c, sid, tid, c.logger)
332+
session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgID)
333333
c.logger.Debugf("Connection:recvLoop:EventAttachedToTarget", "sid:%v tid:%v wsURL:%q", sid, tid, c.wsURL)
334334
c.sessions[sid] = session
335335
c.sessionsMu.Unlock()

common/session.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package common
33
import (
44
"context"
55
"errors"
6-
"sync/atomic"
76

87
"github.com/chromedp/cdproto"
98
"github.com/chromedp/cdproto/cdp"
@@ -24,7 +23,7 @@ type Session struct {
2423
conn *Connection
2524
id target.SessionID
2625
targetID target.ID
27-
msgID int64
26+
msgID *msgID
2827
readCh chan *cdproto.Message
2928
done chan struct{}
3029
closed bool
@@ -35,7 +34,7 @@ type Session struct {
3534

3635
// NewSession creates a new session.
3736
func NewSession(
38-
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger,
37+
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger, msgID *msgID,
3938
) *Session {
4039
s := Session{
4140
BaseEventEmitter: NewBaseEventEmitter(ctx),
@@ -44,6 +43,7 @@ func NewSession(
4443
targetID: tid,
4544
readCh: make(chan *cdproto.Message),
4645
done: make(chan struct{}),
46+
msgID: msgID,
4747

4848
logger: logger,
4949
}
@@ -118,7 +118,7 @@ func (s *Session) Execute(ctx context.Context, method string, params easyjson.Ma
118118
return ErrTargetCrashed
119119
}
120120

121-
id := atomic.AddInt64(&s.msgID, 1)
121+
id := s.msgID.new()
122122

123123
// Setup event handler used to block for response to message being sent.
124124
ch := make(chan *cdproto.Message, 1)
@@ -186,7 +186,7 @@ func (s *Session) ExecuteWithoutExpectationOnReply(ctx context.Context, method s
186186
}
187187
}
188188
msg := &cdproto.Message{
189-
ID: atomic.AddInt64(&s.msgID, 1),
189+
ID: s.msgID.new(),
190190
// We use different sessions to send messages to "targets"
191191
// (browser, page, frame etc.) in CDP.
192192
//

0 commit comments

Comments
 (0)