pool

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPoolFull     = errors.New("database pool is full")
	ErrInvalidState = errors.New("database state is not valid for this operation")
	ErrInvalidIndex = errors.New("invalid database index (id)")
	ErrTimeout      = errors.New("timeout when waiting for ready db")
	ErrTestDBInUse  = errors.New("test database is in use, close the connection before dropping")
)
View Source
var ErrUnknownHash = errors.New("no database pool exists for this hash")

Functions

This section is empty.

Types

type HashPool

type HashPool struct {
	PoolConfig

	sync.RWMutex
	// contains filtered or unexported fields
}

HashPool holds a test DB pool for a certain hash. Each HashPool is running cleanup workers in background.

func NewHashPool

func NewHashPool(cfg PoolConfig, templateDB db.Database, initDBFunc RecreateDBFunc) *HashPool

NewHashPool creates new hash pool with the given config. Starts the workers to extend the pool in background up to requested inital number.

func (*HashPool) GetTestDatabase

func (pool *HashPool) GetTestDatabase(ctx context.Context, timeout time.Duration) (db db.TestDatabase, err error)

func (*HashPool) RecreateTestDatabase

func (pool *HashPool) RecreateTestDatabase(ctx context.Context, id int) error

RecreateTestDatabase prioritizes the test DB to be recreated next via the dirty worker.

func (*HashPool) RemoveAll

func (pool *HashPool) RemoveAll(ctx context.Context, removeFunc RemoveDBFunc) error

func (*HashPool) ReturnTestDatabase

func (pool *HashPool) ReturnTestDatabase(ctx context.Context, id int) error

ReturnTestDatabase returns the given test DB directly to the pool, without cleaning (recreating it).

func (*HashPool) Start

func (pool *HashPool) Start()

func (*HashPool) Stop

func (pool *HashPool) Stop()

type PoolCollection

type PoolCollection struct {
	PoolConfig
	// contains filtered or unexported fields
}

we explicitly want to access this struct via pool.PoolCollection, thus we disable revive for the next line

func NewPoolCollection

func NewPoolCollection(cfg PoolConfig) *PoolCollection

enableDBRecreate set to false will allow reusing test databases that are marked as 'dirty'. Otherwise, test DB has to be returned when no longer needed and there are higher chances of getting ErrPoolFull when requesting a new DB.

func (*PoolCollection) GetTestDatabase

func (p *PoolCollection) GetTestDatabase(ctx context.Context, hash string, timeout time.Duration) (db db.TestDatabase, err error)

GetTestDatabase picks up a ready to use test DB. It waits the given timeout until a DB is available. If there is no DB ready and time elapses, ErrTimeout is returned. Otherwise, the obtained test DB is marked as 'dirty' and can be reused only if returned to the pool.

func (*PoolCollection) InitHashPool

func (p *PoolCollection) InitHashPool(_ context.Context, templateDB db.Database, initDBFunc RecreateDBFunc)

InitHashPool creates a new pool with a given template hash and starts the cleanup workers.

func (*PoolCollection) MakeDBName

func (p *PoolCollection) MakeDBName(hash string, id int) string

MakeDBName makes a test DB name with the configured prefix, template hash and ID of the DB.

func (*PoolCollection) RecreateTestDatabase

func (p *PoolCollection) RecreateTestDatabase(ctx context.Context, hash string, id int) error

RecreateTestDatabase recreates the test DB according to the template and returns it back to the pool.

func (*PoolCollection) RemoveAll

func (p *PoolCollection) RemoveAll(ctx context.Context, removeFunc RemoveDBFunc) error

RemoveAll removes all tracked pools.

func (*PoolCollection) RemoveAllWithHash

func (p *PoolCollection) RemoveAllWithHash(ctx context.Context, hash string, removeFunc RemoveDBFunc) error

RemoveAllWithHash removes a pool with a given template hash. All background workers belonging to this pool are stopped.

func (*PoolCollection) ReturnTestDatabase

func (p *PoolCollection) ReturnTestDatabase(ctx context.Context, hash string, id int) error

ReturnTestDatabase returns the given test DB directly to the pool, without cleaning (recreating it).

func (*PoolCollection) Start

func (p *PoolCollection) Start()

Start is used to start all background workers

func (*PoolCollection) Stop

func (p *PoolCollection) Stop()

Stop is used to stop all background workers

type PoolConfig

type PoolConfig struct {
	InitialPoolSize                   int           // Initial number of ready DBs prepared in background
	MaxPoolSize                       int           // Maximal pool size that won't be exceeded
	TestDBNamePrefix                  string        // Test-Database prefix: DatabasePrefix_TestDBNamePrefix_HASH_ID
	MaxParallelTasks                  int           // Maximal number of pool tasks running in parallel. Must be a number greater or equal 1.
	TestDatabaseRetryRecreateSleepMin time.Duration // Minimal time to wait after a test db recreate has failed (e.g. as client is still connected). Subsequent retries multiply this values until...
	TestDatabaseRetryRecreateSleepMax time.Duration // ... the maximum possible sleep time between retries (e.g. 3 seconds) is reached.
	TestDatabaseMinimalLifetime       time.Duration // After a testdatabase transitions from ready to dirty, always block auto-recreation for this duration (except manual recreate).
	// contains filtered or unexported fields
}

we explicitly want to access this struct via pool.PoolConfig, thus we disable revive for the next line

type RecreateDBFunc

type RecreateDBFunc func(ctx context.Context, testDB db.TestDatabase, templateName string) error

RecreateDBFunc callback executed when a pool is extended or the DB cleaned up by a worker.

type RemoveDBFunc

type RemoveDBFunc func(ctx context.Context, testDB db.TestDatabase) error

RemoveDBFunc callback executed to remove a database

Jump to

Keyboard shortcuts

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