Skip to content

Commit

Permalink
Merge 5db3bff into 332ddbb
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu committed Apr 7, 2021
2 parents 332ddbb + 5db3bff commit 39f1213
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 59 deletions.
95 changes: 49 additions & 46 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
name: CI
name: CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- uses: mirromutth/mysql-action@v1.1
with:
character set server: 'utf8' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
collation server: 'utf8_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
mysql database: 'test' # Optional, default value is "test". The specified database which will be create
mysql user: 'test' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
mysql password: 'test' # Required if "mysql user" exists. The password for the "mysql user"

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Test Coverage
env:
MSSQLX_MYSQL_DSN: test:test@tcp(localhost:3306)/test
run: go test -v -race -count=1 -coverprofile=coverage.out

- name: Convert coverage to lcov
uses: jandelgado/gcov2lcov-action@v1.0.4
with:
infile: coverage.out
outfile: coverage.lcov

- name: Coveralls
uses: coverallsapp/github-action@v1.1.0
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov
- uses: mirromutth/mysql-action@v1.1
with:
character set server: "utf8" # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
collation server: "utf8_general_ci" # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
mysql database: "test" # Optional, default value is "test". The specified database which will be create
mysql user: "test" # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
mysql password: "test" # Required if "mysql user" exists. The password for the "mysql user"

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Linter
uses: golangci/golangci-lint-action@v2
with:
version: latest

- name: Test Coverage
env:
MSSQLX_MYSQL_DSN: test:test@tcp(localhost:3306)/test
run: go test -v -race -count=1 -coverprofile=coverage.out

- name: Convert coverage to lcov
uses: jandelgado/gcov2lcov-action@v1.0.4
with:
infile: coverage.out
outfile: coverage.lcov

- name: Coveralls
uses: coverallsapp/github-action@v1.1.0
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov
44 changes: 31 additions & 13 deletions mssqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,43 +1458,61 @@ func ConnectMasterSlaves(driverName string, masterDSNs []string, slaveDSNs []str
_all: make([]*wrapper, nAll),
}

// channel to sync routines
c := make(chan byte, len(errResult))
dbInstantiate := func(driverName, dsn string) (dbx *sqlx.DB, err error) {
var db *sql.DB
if opts.instantiate != nil {
db, err = opts.instantiate(driverName, dsn)
} else {
db, err = sql.Open(driverName, dsn)
}

if err == nil {
dbx = sqlx.NewDb(db, driverName)
}

return
}

var (
wg sync.WaitGroup
n int
)

// Concurrency connect to master
n := 0
// concurrent connect to masters
for i := range masterDSNs {
wg.Add(1)
go func(mId, eId int) {
dbConn, err := sqlx.Open(driverName, masterDSNs[mId])
defer wg.Done()

dbConn, err := dbInstantiate(driverName, masterDSNs[mId])
dbs._masters[mId], errResult[eId] = &wrapper{db: dbConn, dsn: masterDSNs[mId]}, err
dbs.masters.add(dbs._masters[mId])

dbs._all[eId] = dbs._masters[mId]
dbs.all.add(dbs._masters[mId])

c <- 0
}(i, n)
n++
}

// Concurrency connect to slaves
// concurrent connect to slaves
for i := range slaveDSNs {
wg.Add(1)
go func(sId, eId int) {
dbConn, err := sqlx.Open(driverName, slaveDSNs[sId])
defer wg.Done()

dbConn, err := dbInstantiate(driverName, slaveDSNs[sId])
dbs._slaves[sId], errResult[eId] = &wrapper{db: dbConn, dsn: slaveDSNs[sId]}, err
dbs.slaves.add(dbs._slaves[sId])

dbs._all[eId] = dbs._slaves[sId]
dbs.all.add(dbs._slaves[sId])

c <- 0
}(i, n)
n++
}

for i := 0; i < len(errResult); i++ {
<-c
}
// wait all done
wg.Wait()

return dbs, errResult
}
9 changes: 9 additions & 0 deletions mssqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1947,3 +1947,12 @@ func TestStressQueries(t *testing.T) {
wg.Wait()
})
}

func TestFailOpenDB(t *testing.T) {
_, err := ConnectMasterSlaves("mysql", []string{"abc.com"}, []string{"abc.com"},
WithDBInstantiate(func(driverName, dsn string) (*sql.DB, error) {
return nil, fmt.Errorf("fake")
}),
)
require.NotEmpty(t, err)
}
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mssqlx

import "database/sql"

// ReadQuerySource enums.
type ReadQuerySource int

Expand All @@ -19,11 +21,15 @@ const (
type clusterOptions struct {
isWsrep bool
readQuerySource ReadQuerySource
instantiate Instantiate
}

// Option setter.
type Option func(*clusterOptions)

// Instantiate db.
type Instantiate func(driverName, dsn string) (*sql.DB, error)

// WithWsrep indicates galera/wsrep cluster
func WithWsrep() Option {
return func(o *clusterOptions) {
Expand All @@ -37,3 +43,10 @@ func WithReadQuerySource(source ReadQuerySource) Option {
o.readQuerySource = source
}
}

// WithDBInstantiate overwrite instantiate for db conn.
func WithDBInstantiate(f Instantiate) Option {
return func(o *clusterOptions) {
o.instantiate = f
}
}

0 comments on commit 39f1213

Please sign in to comment.