mafredri / cdp

Package cdp provides type-safe bindings for the Chrome DevTools Protocol (CDP), written in the Go programming language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connection for every thread

girafferiel opened this issue · comments

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)

same question

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.