Documentation ¶
Overview ¶
Package boltdbpool implements a pool container for BoltDB github.com/coreos/bbolt databases. Pool elements called connections keep reference counts for each database to close it when it when the count is 0. Database is reused or opened based on database file path. Closing the database must not be done directly, instead Connection.Close() method should be used. Database is removed form the pool and closed by the goroutine in the background in respect to reference count and delay in time if it is specified.
Example:
package main import ( "fmt" "time" "resenje.org/boltdbpool" ) func main() { pool := boltdbpool.New(&boltdbpool.Options{ ConnectionExpires: 5 * time.Second, ErrorHandler: func(err error) { fmt.Printf("error: %v", err) }, }) defer p.Close() ... c, err := pool.Get("/tmp/db.bolt") if err != nil { panic(err) } defer c.Close() ... c.DB.Update(func(tx *bolt.TX) error { ... }) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultErrorHandler is the default function that prints errors from the Pool. DefaultErrorHandler = func(err error) { log.Printf("error: %v", err) } )
Functions ¶
This section is empty.
Types ¶
type Connection ¶
Connection encapsulates bolt.DB and keeps reference counter and closing time information.
func (*Connection) Close ¶
func (c *Connection) Close()
Close function on Connection decrements reference counter and closes the database if needed.
type Options ¶
type Options struct { // BoltOptions is used on bolt.Open(). BoltOptions *bolt.Options // ConnectionExpires is a duration between the reference count drops to 0 and // the time when the database is closed. It is useful to avoid frequent // openings of the same database. If the value is 0 (default), no caching is done. ConnectionExpires time.Duration // ErrorHandler is the function that handles errors. ErrorHandler func(error) }
Options are used when a new pool is created that.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool keeps track of connections.
func New ¶
New creates new pool with provided options and also starts database closing goroutone and goroutine for errors handling to ErrorHandler.
func (*Pool) Close ¶
func (p *Pool) Close()
Close function closes and removes from the pool all databases. After the execution pool is not usable.