db

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2019 License: MIT Imports: 8 Imported by: 0

README

golang-mongo-pool

This repository is uploaded for academic learning of database pooling class. I have used official golang mongo-driver for mongodb.

For pooling following functions are required in class:

  1. Init(string)
  2. CreateConnection() (*mongo.Client, error)
  3. GetDatabase() (*mongo.Database, error)
  4. GetBusyConnectionsCount() int
  5. GetMaxConnections() int
  6. GetMinConnections() int
  7. GetOpenConnections() []*mongo.Client
  8. GetOpenConnectionsCount() int
  9. GetPoolName() string
  10. GetTimeOut() time.Duration
  11. SetErrorOnBusy()
  12. SetPoolSize(uint32, uint32)
  13. TerminateConnection(*mongo.Client)

Init(string)

This function is use for initializing important config of mongo db eg: connection client

CreateConnection() (*mongo.Client, error)

This function is used as getObject of pooling class. It will return new connection if required on calling.

GetDatabase() (*mongo.Database, error)

This function is call to initialize database and return db instance from mongo client.

GetBusyConnectionsCount() int

This Function return no of busy connection count.

GetMaxConnections() int

This function return count of maximum allowed connections in pool.

GetMinConnections() int

This function return count of minimum allowed connections in pool.

GetOpenConnections() []*mongo.Client

This function return all the open connection instances in pool.

GetOpenConnectionsCount() int

This function return count of open connections in pool.

GetPoolName() string

This function return pool name. Default is root

GetTimeOut() time.Duration

This function return timeout duration of connection in pool.

SetErrorOnBusy()

If this function is called then if all connections are busy CreateConnection() function will return MongoPoolError.

SetPoolSize(uint32, uint32)

This will resize mongo pool without affecting currently open connection. This function still need to be implemented. All PR for this are welcome.

TerminateConnection(*mongo.Client)

This will terminate the connection and release same object from pool.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCollection

func GetCollection(model interface{}) (*mongo.Collection, error)

GetCollection return collection obj

Types

type IMongoPool

type IMongoPool interface {
	Init(string)
	CreateConnection() (*mongo.Client, error)
	GetDatabase() (*mongo.Database, error)
	GetBusyConnectionsCount() int
	GetMaxConnections() int
	GetMinConnections() int
	GetOpenConnections() []*mongo.Client
	GetOpenConnectionsCount() int
	GetPoolName() string
	GetTimeOut() time.Duration
	SetErrorOnBusy()
	SetPoolSize(uint32, uint32)
	TerminateConnection(*mongo.Client)
}

IMongoPool mongo db pooling class's interface

func GetMongoPool

func GetMongoPool(name ...string) IMongoPool

GetMongoPool get mongo pool instace

type MongoPool

type MongoPool struct {
	// contains filtered or unexported fields
}

MongoPool mongo db pooling class

func (*MongoPool) CreateConnection

func (mp *MongoPool) CreateConnection() (c *mongo.Client, e error)

CreateConnection create or get a connect

func (*MongoPool) GetBusyConnectionsCount

func (mp *MongoPool) GetBusyConnectionsCount() int

GetBusyConnectionsCount return no of busy connections

func (*MongoPool) GetDatabase

func (mp *MongoPool) GetDatabase() (*mongo.Database, error)

GetDatabase return database obj

func (*MongoPool) GetMaxConnections

func (mp *MongoPool) GetMaxConnections() int

GetMaxConnections return no of max connections

func (*MongoPool) GetMinConnections

func (mp *MongoPool) GetMinConnections() int

GetMinConnections return no of min connections

func (*MongoPool) GetOpenConnections

func (mp *MongoPool) GetOpenConnections() []*mongo.Client

GetOpenConnections return no of open connections

func (*MongoPool) GetOpenConnectionsCount

func (mp *MongoPool) GetOpenConnectionsCount() int

GetOpenConnectionsCount return no of open connections

func (*MongoPool) GetPoolName

func (mp *MongoPool) GetPoolName() string

GetPoolName return pool name

func (*MongoPool) GetTimeOut

func (mp *MongoPool) GetTimeOut() time.Duration

GetTimeOut return timeout

func (*MongoPool) Init

func (mp *MongoPool) Init(name string)

Init init MongoPool

func (*MongoPool) SetErrorOnBusy

func (mp *MongoPool) SetErrorOnBusy()

SetErrorOnBusy return error when all connection are busy

func (*MongoPool) SetPoolSize

func (mp *MongoPool) SetPoolSize(minConn uint32, maxConn uint32)

SetPoolSize set the pool size

func (*MongoPool) TerminateConnection

func (mp *MongoPool) TerminateConnection(conn *mongo.Client)

TerminateConnection terminate the connection

type MongoPoolError

type MongoPoolError struct {
	When time.Time
	What string
}

MongoPoolError MongoPool error

func (*MongoPoolError) Error

func (e *MongoPoolError) Error() string

Directories

Path Synopsis
config

Jump to

Keyboard shortcuts

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