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>
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.Returns Promise<void>
reset
Discards all sessions in the pool, both usable and retired, resetting it to a blank state.
The persisted record is left alone - use SessionPool.resetStore to clear that as well.
Returns void
resetStore
Clears the persisted pool record, leaving the in-memory sessions alone.
Throws while the state is still being persisted periodically - the next PERSIST_STATE event would write the record straight back. Call SessionPool.teardown first, or use SessionPool.reset to discard the sessions themselves. A no-op if persistence is disabled.
Returns Promise<void>
retiredSessionsCount
Gets count of retired sessions in the pool.
Returns Promise<number>
teardown
Stops the periodic state persistence and persists the state one last time. This function should be called after you are done with using the
SessionPoolinstance. Using the pool again afterwards initializes it anew.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.