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

Commit b762a08

Browse files
ankur22ka3de
authored andcommitted
Add msgIDGenerator to abstract away id
This will avoid accidental use of id when newID should be used. Resolves: #984 (comment)
1 parent c097382 commit b762a08

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

common/connection.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ var _ cdp.Executor = &Connection{}
3333
// chrome, it's best to work with unique ids to avoid the Execute
3434
// handlers working with the wrong response, or handlers deadlocking
3535
// when their response is rerouted to the wrong handler.
36+
//
37+
// Use the msgIDGenerator interface to abstract `id` away.
3638
type msgID struct {
3739
id int64
3840
}
3941

40-
func (m *msgID) new() int64 {
42+
func (m *msgID) newID() int64 {
4143
return atomic.AddInt64(&m.id, 1)
4244
}
4345

46+
type msgIDGenerator interface {
47+
newID() int64
48+
}
49+
4450
type executorEmitter interface {
4551
cdp.Executor
4652
EventEmitter
@@ -125,7 +131,7 @@ type Connection struct {
125131
done chan struct{}
126132
closing chan struct{}
127133
shutdownOnce sync.Once
128-
msgID *msgID
134+
msgIDGen msgIDGenerator
129135

130136
sessionsMu sync.RWMutex
131137
sessions map[target.SessionID]*Session
@@ -163,7 +169,7 @@ func NewConnection(ctx context.Context, wsURL string, logger *log.Logger) (*Conn
163169
errorCh: make(chan error),
164170
done: make(chan struct{}),
165171
closing: make(chan struct{}),
166-
msgID: &msgID{},
172+
msgIDGen: &msgID{},
167173
sessions: make(map[target.SessionID]*Session),
168174
}
169175

@@ -329,7 +335,7 @@ func (c *Connection) recvLoop() {
329335
sid, tid := eva.SessionID, eva.TargetInfo.TargetID
330336

331337
c.sessionsMu.Lock()
332-
session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgID)
338+
session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgIDGen)
333339
c.logger.Debugf("Connection:recvLoop:EventAttachedToTarget", "sid:%v tid:%v wsURL:%q", sid, tid, c.wsURL)
334340
c.sessions[sid] = session
335341
c.sessionsMu.Unlock()
@@ -509,7 +515,7 @@ func (c *Connection) Close(args ...goja.Value) {
509515
// Execute implements cdproto.Executor and performs a synchronous send and receive.
510516
func (c *Connection) Execute(ctx context.Context, method string, params easyjson.Marshaler, res easyjson.Unmarshaler) error {
511517
c.logger.Debugf("connection:Execute", "wsURL:%q method:%q", c.wsURL, method)
512-
id := c.msgID.new()
518+
id := c.msgIDGen.newID()
513519

514520
// Setup event handler used to block for response to message being sent.
515521
ch := make(chan *cdproto.Message, 1)

common/session.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Session struct {
2323
conn *Connection
2424
id target.SessionID
2525
targetID target.ID
26-
msgID *msgID
26+
msgIDGen msgIDGenerator
2727
readCh chan *cdproto.Message
2828
done chan struct{}
2929
closed bool
@@ -34,7 +34,7 @@ type Session struct {
3434

3535
// NewSession creates a new session.
3636
func NewSession(
37-
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger, msgID *msgID,
37+
ctx context.Context, conn *Connection, id target.SessionID, tid target.ID, logger *log.Logger, msgIDGen msgIDGenerator,
3838
) *Session {
3939
s := Session{
4040
BaseEventEmitter: NewBaseEventEmitter(ctx),
@@ -43,7 +43,7 @@ func NewSession(
4343
targetID: tid,
4444
readCh: make(chan *cdproto.Message),
4545
done: make(chan struct{}),
46-
msgID: msgID,
46+
msgIDGen: msgIDGen,
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 := s.msgID.new()
121+
id := s.msgIDGen.newID()
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: s.msgID.new(),
189+
ID: s.msgIDGen.newID(),
190190
// We use different sessions to send messages to "targets"
191191
// (browser, page, frame etc.) in CDP.
192192
//

0 commit comments

Comments
 (0)