pool

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pool keeps one browser alive across reads instead of launching a new one for each.

Measured on a real read, launching Chrome costs 0.83s, quitting it costs 1.00s, and a cold browser loads a page more slowly than a warm one. Reuse cut the median uncached read from 3.58s to 2.02s.

The constraint that shapes everything here is the profile lock: exactly one Chrome may hold a user-data-dir. That makes the profile a resource with a single owner, so the pool has to guarantee two things that a plain cache does not:

  • Only one browser is ever launched at a time. Two concurrent launches both pass Chrome's own profile-in-use check and then contend for the directory.
  • Something outside the pool -- an interactive login, a write in a visible browser -- can take the profile and hold it, with reads kept out for the whole time rather than only at the moment of handover.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("browser pool is closed")

ErrClosed reports use of a pool that has been shut down.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	Alive(ctx context.Context) bool
}

Checker is a session that can report whether it is still usable.

A pooled handle can stop working without the pool being told: Chrome can crash, be killed, or be quit by the user, and the session then fails every call with a closed-connection error. Sessions implementing this are checked before being handed out so a dead browser is replaced rather than reused.

The probe takes a context because it is browser I/O: a Chrome that is running but whose connection has wedged would otherwise never answer.

type Lease

type Lease struct {
	Session Session
	// contains filtered or unexported fields
}

Lease is a borrowed session. Release must be called exactly once.

func (*Lease) Release

func (l *Lease) Release()

Release returns the session to the pool.

type Opener

type Opener func(ctx context.Context) (Session, error)

Opener starts a new session.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool hands out a shared browser session, keeping it warm between uses.

func New

func New(open Opener, idle time.Duration) *Pool

New builds a pool. A session with no outstanding leases is closed once it has been idle for the given duration; zero disables warming entirely.

func (*Pool) Acquire

func (p *Pool) Acquire(ctx context.Context) (*Lease, error)

Acquire borrows the shared session, opening one if none is warm.

It blocks while the profile is reserved and while another caller is already launching, rather than proceeding in parallel: both would put a second Chrome on a directory that allows one.

func (*Pool) Close

func (p *Pool) Close()

Close shuts the pool down. Further calls to Acquire fail.

func (*Pool) Reserve

func (p *Pool) Reserve(ctx context.Context) (Reservation, error)

Reserve takes exclusive use of the profile for something outside the pool.

New acquires are blocked first, then outstanding leases are waited out, then the warm browser is closed. Blocking first is the point: a reservation that only closed the current browser would let the next read open another one while the login window or a write still had the profile.

func (*Pool) Warm

func (p *Pool) Warm() bool

Warm reports whether a session is currently held.

type Reservation

type Reservation interface {
	Release()
}

Reservation is exclusive use of the profile, held by something outside the pool. Release must be called, or reads stay blocked.

type Reserver

type Reserver interface {
	Reserve(ctx context.Context) (Reservation, error)
}

Reserver hands out exclusive use of the profile.

type Session

type Session interface {
	Close() error
}

Session is the part of a browser session the pool manages.

Close reports whether the profile was confirmed released. A browser that outlives its shutdown still owns the directory, and the pool must not hand that directory to a login or a write on the strength of Close merely having returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL