Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection for every thread #127

Open
girafferiel opened this issue May 22, 2021 · 2 comments
Open

Connection for every thread #127

girafferiel opened this issue May 22, 2021 · 2 comments

Comments

@girafferiel
Copy link

Hi, apologize if its in the docs. I would like to create a link preview rpc API so that client can send a URL and get the title, desc, etc. Should I call devtool.New inside the RPC handler or only once when the service is initialized? How many connections to the devtool can be managed at a time ? I'm planning to use the docker as described here: https://stackoverflow.com/questions/59110235/how-to-run-chromedp-in-docker
I am confused if 10k users call my RPC, that means 10k connections to the devtool and how is it going to be handled by the chromedriver
thank you

	ctx2, cancel := context.WithTimeout(context.Background(), 100*time.Second)
	defer cancel()

	// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).
	devt := devtool.New("http://127.0.0.1:9222")
	pt, err := devt.Get(ctx2, devtool.Page)
	if err != nil {
		pt, err = devt.Create(ctx2)
		if err != nil {
			return nil, err
		}
	}

	// Initiate a new RPC connection to the Chrome DevTools Protocol target.
	conn, err := rpcc.DialContext(ctx2, pt.WebSocketDebuggerURL)
	if err != nil {
		return nil, err
	}
	defer conn.Close() // Leaving connections open will leak memory.

	c := cdp.NewClient(conn)
@sailinglawliet
Copy link

same question

@mafredri
Copy link
Owner

With 10k users it would definitely be very resource intensive to use devtool.New and rpcc.Dial for each request. If you're looking to use only one Chrome instance with many connections, I'd recommend taking a look at using the session package. This package allows all tabs to be controlled via a single websocket connection.

For a complete example where the session package is utilized, check out Example (Incognito).

But keep in mind it's not necessary to use CreateBrowserContext, this all depends on your use-case and what level of isolation you want. I recommend reading up on what it entails. But essentially you could use one context per tab, or one context for a group of tabs, or just rely on the default context.

With this, devtool.New + rpcc.Dial is essentially replaced by bc.Target.CreateTarget + sess.Dial, and that's how you'd create more tabs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants