SessionPool
Implements
Index
Constructors
constructor
Parameters
options: SessionPoolOptions = {}
Returns SessionPool
Properties
readonlyid
Methods
[asyncDispose]
Returns Promise<void>
addSession
Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool, but this allows you to add more sessions once the max pool size is reached. This also allows you to add session with overridden session options (e.g. with specific session id).
Parameters
optionaloptions: SessionOptions | Session = {}
The configuration options for the session being added to the session pool.
Returns Promise<void>
getSession
Gets session. If there is space for new session, it creates and returns new session. If the session pool is full, it picks a session from the pool, If the picked session is usable it is returned, otherwise it creates and returns a new one.
Parameters
optionalsessionId: string
If provided, it returns the usable session with this id,
undefinedotherwise.
Returns Promise<undefined | Session>
getState
Returns an object representing the internal state of the
SessionPoolinstance. Note that the object's fields can change in future releases.Returns Promise<{ retiredSessionsCount: number; sessions: SessionState[]; usableSessionsCount: number }>
newSession
Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool, but this allows you to add more sessions once the max pool size is reached. This also allows you to add session with overridden session options (e.g. with specific session id).
Parameters
optionalsessionOptions: SessionOptions
Returns Promise<Session>
persistState
Persists the current state of the
SessionPoolinto the default KeyValueStore. The state is persisted automatically in regular intervals.Parameters
optionaloptions: PersistenceOptions
Override the persistence options provided in the constructor
Returns Promise<void>
resetStore
Parameters
optionaloptions: PersistenceOptions
Override the persistence options provided in the constructor
Returns Promise<void>
retiredSessionsCount
Gets count of retired sessions in the pool.
Returns Promise<number>
teardown
Removes listener from
persistStateevent. This function should be called after you are done with using theSessionPoolinstance.Parameters
options: { persistState?: boolean } = {}
Set
persistStateto false when the final state was already persisted by the event manager.optionalpersistState: boolean = true
Returns Promise<void>
usableSessionsCount
Gets count of usable sessions in the pool.
Returns Promise<number>
Handles the rotation, creation and persistence of user-like sessions. Creates a pool of Session instances, that are randomly rotated. When some session is marked as blocked, it is removed and new one is created instead (the pool never returns an unusable session). Learn more in the Session management guide.
Session pool is already integrated into crawlers and is always active. All public methods are lazy-initialized — the pool initializes itself on first use.
You can configure the pool with many options. See the SessionPoolOptions. Session pool is by default persisted in default KeyValueStore. If you want to have one pool for all runs you have to specify SessionPoolOptions.persistStateKeyValueStoreId.
Advanced usage:
*Default session allocation flow:
SessionPoolreachesmaxPoolSize, new sessions are created, provided to the user and added to the poolmaxPoolSize), a random session from the pool is provided.