Skip to content

Commit

Permalink
Use the msgId defined in connection in session
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Jul 27, 2023
1 parent e10fcfe commit 9e8919c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (c *Connection) recvLoop() {
sid, tid := eva.SessionID, eva.TargetInfo.TargetID

c.sessionsMu.Lock()
session := NewSession(c.ctx, c, sid, tid, c.logger)
session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgID)
c.logger.Debugf("Connection:recvLoop:EventAttachedToTarget", "sid:%v tid:%v wsURL:%q", sid, tid, c.wsURL)
c.sessions[sid] = session
c.sessionsMu.Unlock()
Expand Down
10 changes: 5 additions & 5 deletions common/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"context"
"errors"
"sync/atomic"

"github.com/chromedp/cdproto"
"github.com/chromedp/cdproto/cdp"
Expand All @@ -24,7 +23,7 @@ type Session struct {
conn *Connection
id target.SessionID
targetID target.ID
msgID int64
msgID *msgID
readCh chan *cdproto.Message
done chan struct{}
closed bool
Expand All @@ -35,7 +34,7 @@ type Session struct {

// NewSession creates a new session.
func NewSession(
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger,
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger, msgID *msgID,
) *Session {
s := Session{
BaseEventEmitter: NewBaseEventEmitter(ctx),
Expand All @@ -44,6 +43,7 @@ func NewSession(
targetID: tid,
readCh: make(chan *cdproto.Message),
done: make(chan struct{}),
msgID: msgID,

logger: logger,
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *Session) Execute(ctx context.Context, method string, params easyjson.Ma
return ErrTargetCrashed
}

id := atomic.AddInt64(&s.msgID, 1)
id := s.msgID.new()

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

0 comments on commit 9e8919c

Please sign in to comment.