Skip to content

Commit

Permalink
replace PagePool Get to MustGet, add Get method allow error return.
Browse files Browse the repository at this point in the history
  • Loading branch information
bestmountain committed Apr 30, 2024
1 parent 4efda70 commit 7eab95c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func ExamplePage_pool() {
}

yourJob := func() {
page := pool.Get(create)
page := pool.MustGet(create)

// Put the instance back to the pool after we're done,
// so the instance can be reused by other goroutines.
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/use-rod-like-chrome-extension/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func linkPreviewer(browser *rod.Browser) {

// Expose a function to the page to provide preview
page.MustExpose("getPreview", func(url gson.JSON) (interface{}, error) {
p := pool.Get(create)
p := pool.MustGet(create)
defer pool.Put(p)
p.MustNavigate(url.Str())
return base64.StdEncoding.EncodeToString(p.MustScreenshot()), nil
Expand Down
49 changes: 47 additions & 2 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,12 +977,57 @@ func TestPagePool(t *testing.T) {
g := setup(t)

pool := rod.NewPagePool(3)
defer pool.Cleanup(func(p *rod.Page) {
p.MustClose()
})
create := func() *rod.Page { return g.browser.MustPage() }
p := pool.Get(create)
p := pool.MustGet(create)
pool.Put(p)
pool.Cleanup(func(p *rod.Page) {
}

func TestPagePool_Get(t *testing.T) {
g := setup(t)

pool := rod.NewPagePool(3)
defer pool.Cleanup(func(p *rod.Page) {
p.MustClose()
})
create := func() (*rod.Page, error) {
b, err := g.browser.Incognito()
if err != nil {
return nil, err
}
return b.Page(proto.TargetCreateTarget{URL: ""})
}
p, err := pool.Get(create)
if err != nil {
t.Fatal(err)
}
pool.Put(p)
}

func TestPagePool_Get_Negative(t *testing.T) {
g := setup(t)
failContext, cancel := context.WithCancel(g.Context())
g.browser = g.browser.Context(failContext)
// manipulate browser canceled by another thread
cancel()
pool := rod.NewPagePool(3)

create := func() (*rod.Page, error) {
b, err := g.browser.Incognito()
if err != nil {
return nil, err
}
return b.Page(proto.TargetCreateTarget{URL: ""})
}
p, err := pool.Get(create)
if err != nil {
t.Log(err)
} else {
pool.Put(p)
t.FailNow()
}
}

func TestPageUseNonExistSession(t *testing.T) {
Expand Down
19 changes: 16 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,34 @@ func NewPagePool(limit int) PagePool {
return pp
}

// Get a page from the pool. Use the [PagePool.Put] to make it reusable later.
func (pp PagePool) Get(create func() *Page) *Page {
// MustGet a page from the pool. Use the [PagePool.Put] to make it reusable later.
func (pp PagePool) MustGet(create func() *Page) *Page {
p := <-pp
if p == nil {
p = create()
}
return p
}

// Get a page from the pool, allow error. Use the [PagePool.Put] to make it reusable later.
func (pp PagePool) Get(create func() (*Page, error)) (*Page, error) {
p := <-pp
var err error
if p == nil {
p, err = create()
if err != nil {
return nil, err
}
}
return p, nil
}

// Put a page back to the pool.
func (pp PagePool) Put(p *Page) {
pp <- p
}

// Cleanup helper.
// Cleanup helper, may stuck while PagePool is not full
func (pp PagePool) Cleanup(iteratee func(*Page)) {
for i := 0; i < cap(pp); i++ {
p := <-pp
Expand Down

0 comments on commit 7eab95c

Please sign in to comment.