Skip to content

Commit

Permalink
eorm: 微调读写分离相关命名
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Feb 14, 2023
1 parent 84d805f commit 08fc73f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions internal/integration/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (s *MasterSlaveSuite) initDb() (*eorm.MasterSlavesDB, error) {
}
slaves = append(slaves, slave)
}
s.slaveNamegeter = newSlaveNameGet(roundrobin.NewRoundrobin(slaves...))
return eorm.OpenMasterSlaveDB(s.driver, master, eorm.MasterSlaveWithSlaveGeter(s.slaveNamegeter))
s.slaveNamegeter = newSlaveNameGet(roundrobin.NewSlaves(slaves...))
return eorm.OpenMasterSlaveDB(s.driver, master, eorm.MasterSlaveWithSlaves(s.slaveNamegeter))

}

Expand Down
2 changes: 1 addition & 1 deletion internal/integration/select_masterslave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *MasterSlaveSelectTestSuite) TestMasterSlave() {
wantRes: s.data,
ctx: func() context.Context {
c := context.Background()
c = eorm.UserMaster(c)
c = eorm.UseMaster(c)
return c
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/slaves/roundrobin/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"github.com/gotomicro/eorm/internal/slaves"
)

type Roundrobin struct {
type Slaves struct {
slaves []slaves.Slave
cnt uint32
}

func (r *Roundrobin) Next(ctx context.Context) (slaves.Slave, error) {
func (r *Slaves) Next(ctx context.Context) (slaves.Slave, error) {
if ctx.Err() != nil {
return slaves.Slave{}, ctx.Err()
}
Expand All @@ -41,8 +41,8 @@ func (r *Roundrobin) Next(ctx context.Context) (slaves.Slave, error) {
return r.slaves[index], nil
}

func NewRoundrobin(slavedbs ...*sql.DB) *Roundrobin {
r := &Roundrobin{}
func NewSlaves(slavedbs ...*sql.DB) *Slaves {
r := &Slaves{}
r.slaves = make([]slaves.Slave, 0, len(slavedbs))
for idx, slavedb := range slavedbs {
s := slaves.Slave{
Expand Down
13 changes: 6 additions & 7 deletions masterslavedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

type MasterSlavesDB struct {
master *sql.DB
slaves.Slaves
slaves slaves.Slaves
core
}

Expand All @@ -46,7 +46,7 @@ func (m *MasterSlavesDB) queryContext(ctx context.Context, query string, args ..
if ok {
return m.master.QueryContext(ctx, query, args...)
}
slave, err := m.Slaves.Next(ctx)
slave, err := m.slaves.Next(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -93,13 +93,12 @@ func OpenMasterSlaveDB(driver string, master *sql.DB, opts ...MasterSlaveDBOptio

type MasterSlaveDBOption func(db *MasterSlavesDB)

func MasterSlaveWithSlaveGeter(geter slaves.Slaves) MasterSlaveDBOption {
func MasterSlaveWithSlaves(s slaves.Slaves) MasterSlaveDBOption {
return func(db *MasterSlavesDB) {
db.Slaves = geter
db.slaves = s
}
}

func UserMaster(ctx context.Context) context.Context {
mctx := context.WithValue(ctx, master, true)
return mctx
func UseMaster(ctx context.Context) context.Context {
return context.WithValue(ctx, master, true)
}

0 comments on commit 08fc73f

Please sign in to comment.