connection

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 New

func New(conf *config.Config) (*Connections, error)

New creates a new Connections

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

func (*Connections) Ping

func (d *Connections) Ping(ctx context.Context) error

Ping checks all database connections

func (*Connections) ReadDB added in v0.1.4

func (d *Connections) ReadDB() (*sql.DB, error)

ReadDB returns a slave database connection for read operations

type DBManager

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

DBManager manages database connections for read-write splitting

func NewDBManager

func NewDBManager(conf *config.Database) (*DBManager, error)

NewDBManager creates a new database manager with read-write splitting

func (*DBManager) Close

func (dm *DBManager) Close() error

Close closes all database connections

func (*DBManager) Health

func (dm *DBManager) Health(ctx context.Context) error

Health checks the health of all database connections

func (*DBManager) Master

func (dm *DBManager) Master() *sql.DB

Master returns the master database connection

func (*DBManager) Slave

func (dm *DBManager) Slave() (*sql.DB, error)

Slave returns a slave database connection based on the load balancing strategy

type LoadBalancer

type LoadBalancer interface {
	Next([]*sql.DB) (*sql.DB, error)
}

LoadBalancer LoadBalancer interface

type MongoLoadBalancer

type MongoLoadBalancer interface {
	Next([]*mongo.Client) (*mongo.Client, error)
}

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

func (m *MongoManager) GetDatabase(name string, readOnly bool) (*mongo.Database, error)

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 MongoRandomBalancer

type MongoRandomBalancer struct{}

MongoRandomBalancer random strategy

func (*MongoRandomBalancer) Next

func (rb *MongoRandomBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

type MongoRoundRobinBalancer

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

MongoRoundRobinBalancer round-robin strategy

func NewMongoRoundRobinBalancer

func NewMongoRoundRobinBalancer() *MongoRoundRobinBalancer

func (*MongoRoundRobinBalancer) Next

func (rb *MongoRoundRobinBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

type MongoWeightBalancer

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

MongoWeightBalancer weight strategy

func NewMongoWeightBalancer

func NewMongoWeightBalancer(nodes []*config.MongoNode) *MongoWeightBalancer

func (*MongoWeightBalancer) Next

func (wb *MongoWeightBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

type RandomBalancer

type RandomBalancer struct{}

RandomBalancer Implement random strategy

func (*RandomBalancer) Next

func (rb *RandomBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

type RoundRobinBalancer

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

RoundRobinBalancer Implement polling strategy

func NewRoundRobinBalancer

func NewRoundRobinBalancer() *RoundRobinBalancer

NewRoundRobinBalancer Create new RoundRobinBalancer

func (*RoundRobinBalancer) Next

func (rb *RoundRobinBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

type WeightBalancer

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

WeightBalancer Implement weight strategy

func NewWeightBalancer

func NewWeightBalancer(slaves []*config.DBNode) *WeightBalancer

func (*WeightBalancer) Next

func (wb *WeightBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

Jump to

Keyboard shortcuts

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