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: boltdbpool.ErrorHandlerFunc(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 ( // DefaultFileMode is used in bolt.Open() as file mode for database file // if FileMode is not specified in boltdbpool.Options. DefaultFileMode = os.FileMode(0640) // DefaultDirMode is used in os.MkdirAll() as file mode for database directories // if DirMode is not specified in boltdbpool.Options. DefaultDirMode = os.FileMode(0750) // DefaultErrorHandler accepts errors from // goroutine that closes the databases if ErrorHandler is not specified in // boltdbpool.Options. DefaultErrorHandler = ErrorHandlerFunc(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 ErrorHandler ¶
type ErrorHandler interface {
HandleError(err error)
}
ErrorHandler interface can be used for objects that log or panic on error
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(err error)
The ErrorHandlerFunc type is an adapter to allow the use of ordinary functions as error handlers.
func (ErrorHandlerFunc) HandleError ¶
func (f ErrorHandlerFunc) HandleError(err error)
HandleError calls f(err).
type Options ¶
type Options struct { // BoltOptions is used on bolt.Open(). BoltOptions *bolt.Options // FileMode is used in bolt.Open() as file mode for database file. Default: 0640. FileMode os.FileMode // DirMode is used in os.MkdirAll() as file mode for database directories. Default: 0750. DirMode os.FileMode // 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 represents interface that accepts errors from goroutine that closes the databases. ErrorHandler ErrorHandler }
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.