Skip to content

Commit dd35dee

Browse files
authored
chore: lint and refactor for Godoc needs (#15)
1 parent fb1607c commit dd35dee

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Golang Database Resolver and Wrapper for any multiple database connections topology, eg. master-slave replication database, cross-region application.
44

55
[![Go](https://github.com/bxcodec/dbresolver/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/bxcodec/dbresolver/actions/workflows/go.yml)
6-
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/bxcodec/dbresolver?tab=doc)
6+
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/bxcodec/dbresolver/v2?tab=doc)
77

88
## Idea and Inspiration
99

db.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
// DB interface is a contract that supported by this library.
1313
// All offered function of this library defined here.
1414
// This supposed to be aligned with sql.DB, but since some of the functions is not relevant
15-
// with multi dbs connection, we decided to not support it
15+
// with multi dbs connection, we decided to forward all single connection DB related function to the first primary DB
16+
// For example, function like, `Conn()“, or `Stats()` only available for the primary DB, or the first primary DB (if using multi-primary)
1617
type DB interface {
1718
Begin() (*sql.Tx, error)
1819
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
1920
Close() error
20-
Conn(ctx context.Context) (*sql.Conn, error) // db stats for only one of the primary db
21+
// Conn only available for the primary db or the first primary db (if using multi-primary)
22+
Conn(ctx context.Context) (*sql.Conn, error)
2123
Driver() driver.Driver
2224
Exec(query string, args ...interface{}) (sql.Result, error)
2325
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
@@ -35,14 +37,15 @@ type DB interface {
3537
SetMaxOpenConns(n int)
3638
PrimaryDBs() []*sql.DB
3739
ReplicaDBs() []*sql.DB
38-
Stats() sql.DBStats // db stats for only one of the primary db
40+
// Stats only available for the primary db or the first primary db (if using multi-primary)
41+
Stats() sql.DBStats
3942
}
4043

41-
// Supported LoadBalancer
42-
type (
43-
DBLoadBalancer LoadBalancer[*sql.DB]
44-
StmtLoadBalancer LoadBalancer[*sql.Stmt]
45-
)
44+
// DBLoadBalancer is loadbalancer for physical DBs
45+
type DBLoadBalancer LoadBalancer[*sql.DB]
46+
47+
// StmtLoadBalancer is loadbalancer for query prepared statements
48+
type StmtLoadBalancer LoadBalancer[*sql.Stmt]
4649

4750
// sqlDB is a logical database with multiple underlying physical databases
4851
// forming a single ReadWrite (primary) with multiple ReadOnly(replicas) db.

example_wrap_dbs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
_ "github.com/lib/pq"
1111
)
1212

13-
func ExampleNewResolver() {
13+
func ExampleNew() {
1414
var (
1515
host1 = "localhost"
1616
port1 = 5432
@@ -42,7 +42,7 @@ func ExampleNewResolver() {
4242
// configure the DBs for other setup eg, tracing, etc
4343
// eg, tracing.Postgres(dbReadOnlyReplica)
4444

45-
connectionDB := dbresolver.NewResolver(
45+
connectionDB := dbresolver.New(
4646
dbresolver.WithPrimaryDBs(dbPrimary),
4747
dbresolver.WithReplicaDBs(dbReadOnlyReplica),
4848
dbresolver.WithLoadBalancer(dbresolver.RoundRobinLB))

resolver.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package dbresolver
22

3-
// Resolver will resolve all the passed connection
4-
// first DB connection is the primary-writer connection (RW),
5-
// the rest connection will be used for RO connection
6-
func NewResolver(opts ...OptionFunc) DB {
3+
// New will resolve all the passed connection with configurable parameters
4+
func New(opts ...OptionFunc) DB {
75
opt := defaultOption()
86
for _, optFunc := range opts {
97
optFunc(opt)
108
}
119

1210
if len(opt.PrimaryDBs) == 0 {
1311
panic("required primary db connection, set the primary db " +
14-
"connection with dbresolver.Resolver(dbresolver.WithPrimaryDBs(primaryDB))")
12+
"connection with dbresolver.New(dbresolver.WithPrimaryDBs(primaryDB))")
1513
}
1614
return &sqlDB{
1715
primaries: opt.PrimaryDBs,

resolver_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestWrapDBWithMultiDBs(t *testing.T) {
1212
db2 := &sql.DB{}
1313
db3 := &sql.DB{}
1414

15-
db := dbresolver.NewResolver(dbresolver.WithPrimaryDBs(db1), dbresolver.WithReplicaDBs(db2, db3))
15+
db := dbresolver.New(dbresolver.WithPrimaryDBs(db1), dbresolver.WithReplicaDBs(db2, db3))
1616

1717
if db == nil {
1818
t.Errorf("expected %v, got %v", "not nil", db)
@@ -22,7 +22,7 @@ func TestWrapDBWithMultiDBs(t *testing.T) {
2222
func TestWrapDBWithOneDB(t *testing.T) {
2323
db1 := &sql.DB{}
2424

25-
db := dbresolver.NewResolver(dbresolver.WithPrimaryDBs(db1))
25+
db := dbresolver.New(dbresolver.WithPrimaryDBs(db1))
2626

2727
if db == nil {
2828
t.Errorf("expected %v, got %v", "not nil", db)

0 commit comments

Comments
 (0)