Documentation
¶
Index ¶
- Variables
- type Connections
- type DBManager
- type LoadBalancer
- type MongoLoadBalancer
- type MongoManager
- func (m *MongoManager) Close(ctx context.Context) error
- func (m *MongoManager) GetCollection(dbName, collName string, readOnly bool) (*mongo.Collection, error)
- func (m *MongoManager) GetDatabase(name string, readOnly bool) (*mongo.Database, error)
- func (m *MongoManager) Health(ctx context.Context) error
- func (m *MongoManager) Master() *mongo.Client
- func (m *MongoManager) Slave() (*mongo.Client, error)
- func (m *MongoManager) WithTransaction(ctx context.Context, fn func(mongo.SessionContext) error, ...) error
- type MongoRandomBalancer
- type MongoRoundRobinBalancer
- type MongoWeightBalancer
- type RandomBalancer
- type RoundRobinBalancer
- type WeightBalancer
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoAvailableSlaves = errors.New("no available slave databases") ErrInvalidStrategy = errors.New("invalid load balance strategy") )
Functions ¶
This section is empty.
Types ¶
type Connections ¶
type Connections struct {
DBM *DBManager
RC *redis.Client
MS *meili.Client
ES *elastic.Client
OS *opensearch.Client
MGM *MongoManager
Neo neo4j.DriverWithContext
RMQ *amqp.Connection
KFK *kafka.Conn
// contains filtered or unexported fields
}
Connections struct to hold all database connections and clients
func (*Connections) Close ¶
func (d *Connections) Close() (errs []error)
Close closes all data connections
func (*Connections) DB ¶
func (d *Connections) DB() *sql.DB
DB returns the master database connection for write operations
func (*Connections) DBRead ¶
func (d *Connections) DBRead() (*sql.DB, error)
DBRead returns a slave database connection for read operations Deprecated: Use ReadDB() for better clarity
type DBManager ¶
type DBManager struct {
// contains filtered or unexported fields
}
DBManager manages database connections for read-write splitting
func NewDBManager ¶
NewDBManager creates a new database manager with read-write splitting
type LoadBalancer ¶
LoadBalancer LoadBalancer interface
type MongoLoadBalancer ¶
MongoLoadBalancer MongoDB load balancer
type MongoManager ¶
type MongoManager struct {
// contains filtered or unexported fields
}
MongoManager represents a MongoDB connection manager
func NewMongoManager ¶
func NewMongoManager(conf *config.MongoDB) (*MongoManager, error)
NewMongoManager creates a new MongoDB connection manager
func (*MongoManager) Close ¶
func (m *MongoManager) Close(ctx context.Context) error
Close closes all MongoDB connections
func (*MongoManager) GetCollection ¶
func (m *MongoManager) GetCollection(dbName, collName string, readOnly bool) (*mongo.Collection, error)
GetCollection returns a collection from master/slave client dbName: database name collName: collection name readOnly: if true, returns collection from slave, otherwise from master
func (*MongoManager) GetDatabase ¶
GetDatabase returns a database from master/slave client
func (*MongoManager) Health ¶
func (m *MongoManager) Health(ctx context.Context) error
Health checks the health of all MongoDB connections
func (*MongoManager) Master ¶
func (m *MongoManager) Master() *mongo.Client
Master returns the master client
func (*MongoManager) Slave ¶
func (m *MongoManager) Slave() (*mongo.Client, error)
Slave returns a slave client based on the load balancing strategy
func (*MongoManager) WithTransaction ¶
func (m *MongoManager) WithTransaction(ctx context.Context, fn func(mongo.SessionContext) error, opts ...*options.TransactionOptions) error
WithTransaction wraps a function within a transaction
type MongoRoundRobinBalancer ¶
type MongoRoundRobinBalancer struct {
// contains filtered or unexported fields
}
MongoRoundRobinBalancer round-robin strategy
func NewMongoRoundRobinBalancer ¶
func NewMongoRoundRobinBalancer() *MongoRoundRobinBalancer
type MongoWeightBalancer ¶
type MongoWeightBalancer struct {
// contains filtered or unexported fields
}
MongoWeightBalancer weight strategy
func NewMongoWeightBalancer ¶
func NewMongoWeightBalancer(nodes []*config.MongoNode) *MongoWeightBalancer
type RoundRobinBalancer ¶
type RoundRobinBalancer struct {
// contains filtered or unexported fields
}
RoundRobinBalancer Implement polling strategy
func NewRoundRobinBalancer ¶
func NewRoundRobinBalancer() *RoundRobinBalancer
NewRoundRobinBalancer Create new RoundRobinBalancer
type WeightBalancer ¶
type WeightBalancer struct {
// contains filtered or unexported fields
}
WeightBalancer Implement weight strategy
func NewWeightBalancer ¶
func NewWeightBalancer(slaves []*config.DBNode) *WeightBalancer