This repository has been archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Limit browser implementation to hold a single browserContext #929
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a782c2c
Refactor contexts to context in browser
ankur22 cad4a92
Add check to ensure only one browserContext
ankur22 177cf7e
Remove mutex from browserContext in browser
ankur22 f6109ef
Revert back to working with the defaultContext
ankur22 626d4be
Apply suggestions from code review
ankur22 0a37f5b
Update error to include context details
ankur22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,8 +50,8 @@ type Browser struct { | |
// A *Connection is saved to this field, see: connect(). | ||
conn connection | ||
|
||
contextsMu sync.RWMutex | ||
contexts map[cdp.BrowserContextID]*BrowserContext | ||
contextMu sync.RWMutex | ||
context *BrowserContext | ||
defaultContext *BrowserContext | ||
|
||
// Cancel function to stop event listening | ||
|
@@ -103,7 +103,6 @@ func newBrowser( | |
state: int64(BrowserStateOpen), | ||
browserProc: browserProc, | ||
browserOpts: browserOpts, | ||
contexts: make(map[cdp.BrowserContextID]*BrowserContext), | ||
pages: make(map[target.ID]*Page), | ||
sessionIDtoTargetID: make(map[target.SessionID]target.ID), | ||
vu: k6ext.GetVU(ctx), | ||
|
@@ -137,23 +136,29 @@ func (b *Browser) disposeContext(id cdp.BrowserContextID) error { | |
return fmt.Errorf("disposing browser context ID %s: %w", id, err) | ||
} | ||
|
||
b.contextsMu.Lock() | ||
defer b.contextsMu.Unlock() | ||
delete(b.contexts, id) | ||
b.contextMu.Lock() | ||
defer b.contextMu.Unlock() | ||
b.context = nil | ||
|
||
return nil | ||
} | ||
|
||
// getDefaultBrowserContextOrByID returns the BrowserContext for the given page ID. | ||
// getDefaultBrowserContextOrMatchedID returns the BrowserContext for the given browser context ID. | ||
// If the browser context is not found, the default BrowserContext is returned. | ||
func (b *Browser) getDefaultBrowserContextOrByID(id cdp.BrowserContextID) *BrowserContext { | ||
b.contextsMu.RLock() | ||
defer b.contextsMu.RUnlock() | ||
browserCtx := b.defaultContext | ||
if bctx, ok := b.contexts[id]; ok { | ||
browserCtx = bctx | ||
} | ||
return browserCtx | ||
// If the existing browser context id doesn't match an error is returned. | ||
func (b *Browser) getDefaultBrowserContextOrMatchedID(id cdp.BrowserContextID) (*BrowserContext, error) { | ||
b.contextMu.RLock() | ||
defer b.contextMu.RUnlock() | ||
|
||
if b.context == nil { | ||
return b.defaultContext, nil | ||
} | ||
|
||
if b.context.id != id { | ||
return nil, fmt.Errorf("missing browser context. Have: %s, want: %s", b.context.id, id) | ||
ka3de marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return b.context, nil | ||
} | ||
|
||
func (b *Browser) getPages() []*Page { | ||
|
@@ -225,10 +230,12 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) { | |
b.logger.Debugf("Browser:onAttachedToTarget", "sid:%v tid:%v bctxid:%v", | ||
ev.SessionID, ev.TargetInfo.TargetID, ev.TargetInfo.BrowserContextID) | ||
|
||
var ( | ||
targetPage = ev.TargetInfo | ||
browserCtx = b.getDefaultBrowserContextOrByID(targetPage.BrowserContextID) | ||
) | ||
targetPage := ev.TargetInfo | ||
|
||
browserCtx, err := b.getDefaultBrowserContextOrMatchedID(targetPage.BrowserContextID) | ||
if err != nil { | ||
k6ext.Panic(b.ctx, "attaching target to browserContext: %v", err) | ||
inancgumus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if !b.isAttachedPageValid(ev, browserCtx) { | ||
return // Ignore this page. | ||
|
@@ -377,10 +384,10 @@ func (b *Browser) onDetachedFromTarget(ev *target.EventDetachedFromTarget) { | |
} | ||
|
||
func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) { | ||
b.contextsMu.RLock() | ||
browserCtx, ok := b.contexts[id] | ||
b.contextsMu.RUnlock() | ||
if !ok { | ||
b.contextMu.RLock() | ||
browserCtx := b.context | ||
b.contextMu.RUnlock() | ||
if browserCtx == nil || browserCtx.id != id { | ||
return nil, fmt.Errorf("missing browser context: %s", id) | ||
inancgumus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
@@ -494,15 +501,14 @@ func (b *Browser) Close() { | |
|
||
// Contexts returns list of browser contexts. | ||
func (b *Browser) Contexts() []api.BrowserContext { | ||
b.contextsMu.RLock() | ||
defer b.contextsMu.RUnlock() | ||
b.contextMu.RLock() | ||
defer b.contextMu.RUnlock() | ||
|
||
contexts := make([]api.BrowserContext, 0, len(b.contexts)) | ||
for _, b := range b.contexts { | ||
contexts = append(contexts, b) | ||
if b.context == nil { | ||
return []api.BrowserContext{} | ||
} | ||
|
||
return contexts | ||
return []api.BrowserContext{b.context} | ||
} | ||
|
||
// IsConnected returns whether the WebSocket connection to the browser process | ||
|
@@ -513,6 +519,13 @@ func (b *Browser) IsConnected() bool { | |
|
||
// NewContext creates a new incognito-like browser context. | ||
func (b *Browser) NewContext(opts goja.Value) (api.BrowserContext, error) { | ||
b.contextMu.RLock() | ||
browserCtx := b.context | ||
b.contextMu.RUnlock() | ||
if browserCtx != nil { | ||
return nil, errors.New("close the existing browser context before creating a new one") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know how you feel about this. I could change this in this PR (was going to do it in a later PR) so that we don't allow more than one
ankur22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
action := target.CreateBrowserContext().WithDisposeOnDetach(true) | ||
browserContextID, err := action.Do(cdp.WithExecutor(b.ctx, b.conn)) | ||
b.logger.Debugf("Browser:NewContext", "bctxid:%v", browserContextID) | ||
|
@@ -525,13 +538,13 @@ func (b *Browser) NewContext(opts goja.Value) (api.BrowserContext, error) { | |
k6ext.Panic(b.ctx, "parsing newContext options: %w", err) | ||
} | ||
|
||
b.contextsMu.Lock() | ||
defer b.contextsMu.Unlock() | ||
browserCtx, err := NewBrowserContext(b.ctx, b, browserContextID, browserCtxOpts, b.logger) | ||
b.contextMu.Lock() | ||
defer b.contextMu.Unlock() | ||
browserCtx, err = NewBrowserContext(b.ctx, b, browserContextID, browserCtxOpts, b.logger) | ||
if err != nil { | ||
return nil, fmt.Errorf("new context: %w", err) | ||
} | ||
b.contexts[browserContextID] = browserCtx | ||
b.context = browserCtx | ||
|
||
return browserCtx, nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done a stress test on this mutex removal change, and it all seems to work still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱🤞🙃