Skip to content

Commit

Permalink
Support primary and secondary mysql connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 8, 2020
1 parent 44a0f02 commit 0e2b722
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 79 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file starting fro
This project adheres to [Semantic Versioning](http://semver.org/).

-----
## [1.7.0] - 2020-07-08

**Change:**
- Support primary and secondary mysql connections.

## [1.6.0] - 2020-07-07

**Change:**
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.PHONY: dev test image doc

APP_NAME := urbs-setting
APP_PATH := github.com/teambition/urbs-setting
APP_VERSION := $(shell git describe --tags --always --match "v[0-9]*")
APP_PATH := $(shell echo ${PWD} | sed -e "s\#${GOPATH}/src/\#\#g")

dev:
@CONFIG_FILE_PATH=${PWD}/config/default.yml APP_ENV=development go run main.go
Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ mysql:
parameters: loc=UTC&readTimeout=10s&writeTimeout=10s&timeout=10s&multiStatements=false
max_idle_conns: 8
max_open_conns: 64
mysql_read:
host: localhost:3306
user: root
password: password
database: urbs
parameters: loc=UTC&readTimeout=10s&writeTimeout=10s&timeout=10s&multiStatements=false
max_idle_conns: 8
max_open_conns: 64
channels:
- stable
- beta
Expand Down
17 changes: 9 additions & 8 deletions src/bll/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type Group struct {
ms *model.Models
}

// List 返回群组列表,TODO:支持分页
// List 返回群组列表
func (b *Group) List(ctx context.Context, kind string, pg tpl.Pagination) (*tpl.GroupsRes, error) {
groups, total, err := b.ms.Group.Find(ctx, kind, pg)
groups, total, err := b.ms.Group.Find(context.WithValue(ctx, model.ReadDB, true), kind, pg)
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func (b *Group) List(ctx context.Context, kind string, pg tpl.Pagination) (*tpl.

// ListLabels ...
func (b *Group) ListLabels(ctx context.Context, uid string, pg tpl.Pagination) (*tpl.MyLabelsRes, error) {
group, err := b.ms.Group.Acquire(ctx, uid)
group, err := b.ms.Group.Acquire(context.WithValue(ctx, model.ReadDB, true), uid)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +50,7 @@ func (b *Group) ListLabels(ctx context.Context, uid string, pg tpl.Pagination) (

// ListMembers ...
func (b *Group) ListMembers(ctx context.Context, uid string, pg tpl.Pagination) (*tpl.GroupMembersRes, error) {
group, err := b.ms.Group.Acquire(ctx, uid)
group, err := b.ms.Group.Acquire(context.WithValue(ctx, model.ReadDB, true), uid)
if err != nil {
return nil, err
}
Expand All @@ -76,32 +76,33 @@ func (b *Group) ListSettings(ctx context.Context, req tpl.MySettingsQueryURL) (*
return nil, err
}

readCtx := context.WithValue(ctx, model.ReadDB, true)
var productID int64
var moduleID int64
var settingID int64

if req.Product != "" {
productID, err = b.ms.Product.AcquireID(ctx, req.Product)
productID, err = b.ms.Product.AcquireID(readCtx, req.Product)
if err != nil {
return nil, err
}
}
if productID > 0 && req.Module != "" {
moduleID, err = b.ms.Module.AcquireID(ctx, productID, req.Module)
moduleID, err = b.ms.Module.AcquireID(readCtx, productID, req.Module)
if err != nil {
return nil, err
}
}

if moduleID > 0 && req.Setting != "" {
settingID, err = b.ms.Setting.AcquireID(ctx, moduleID, req.Setting)
settingID, err = b.ms.Setting.AcquireID(readCtx, moduleID, req.Setting)
if err != nil {
return nil, err
}
}

pg := req.Pagination
settings, total, err := b.ms.Group.FindSettings(ctx, group.ID, productID, moduleID, settingID, pg, req.Channel, req.Client)
settings, total, err := b.ms.Group.FindSettings(readCtx, group.ID, productID, moduleID, settingID, pg, req.Channel, req.Client)
if err != nil {
return nil, err
}
Expand Down
29 changes: 16 additions & 13 deletions src/bll/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func (b *User) ListCachedLabels(ctx context.Context, uid, product string) *tpl.C
return res
}

productID, err := b.ms.Product.AcquireID(ctx, product)
readCtx := context.WithValue(ctx, model.ReadDB, true)
productID, err := b.ms.Product.AcquireID(readCtx, product)
if err != nil {
return res
}

user, err := b.ms.User.Acquire(ctx, uid)
user, err := b.ms.User.Acquire(readCtx, uid)
if err != nil {
if strings.HasPrefix(uid, "anon-") {
if labels, err := b.ms.LabelRule.ApplyRulesToAnonymous(ctx, uid, productID); err == nil {
Expand Down Expand Up @@ -115,7 +116,8 @@ func (b *User) ListLabels(ctx context.Context, uid string, pg tpl.Pagination) (*

// ListSettings ...
func (b *User) ListSettings(ctx context.Context, req tpl.MySettingsQueryURL) (*tpl.MySettingsRes, error) {
userID, err := b.ms.User.AcquireID(ctx, req.UID)
readCtx := context.WithValue(ctx, model.ReadDB, true)
userID, err := b.ms.User.AcquireID(readCtx, req.UID)
if err != nil {
return nil, err
}
Expand All @@ -125,20 +127,20 @@ func (b *User) ListSettings(ctx context.Context, req tpl.MySettingsQueryURL) (*t
var settingID int64

if req.Product != "" {
productID, err = b.ms.Product.AcquireID(ctx, req.Product)
productID, err = b.ms.Product.AcquireID(readCtx, req.Product)
if err != nil {
return nil, err
}
}
if productID > 0 && req.Module != "" {
moduleID, err = b.ms.Module.AcquireID(ctx, productID, req.Module)
moduleID, err = b.ms.Module.AcquireID(readCtx, productID, req.Module)
if err != nil {
return nil, err
}
}

if moduleID > 0 && req.Setting != "" {
settingID, err = b.ms.Setting.AcquireID(ctx, moduleID, req.Setting)
settingID, err = b.ms.Setting.AcquireID(readCtx, moduleID, req.Setting)
if err != nil {
return nil, err
}
Expand All @@ -162,16 +164,17 @@ func (b *User) ListSettings(ctx context.Context, req tpl.MySettingsQueryURL) (*t
// ListSettingsUnionAll ...
func (b *User) ListSettingsUnionAll(ctx context.Context, req tpl.MySettingsQueryURL) (*tpl.MySettingsRes, error) {
res := &tpl.MySettingsRes{Result: []tpl.MySetting{}}
readCtx := context.WithValue(ctx, model.ReadDB, true)

var productID int64
var moduleID int64
var settingID int64
productID, err := b.ms.Product.AcquireID(ctx, req.Product)
productID, err := b.ms.Product.AcquireID(readCtx, req.Product)
if err != nil {
return nil, err
}

user, err := b.ms.User.Acquire(ctx, req.UID)
user, err := b.ms.User.Acquire(readCtx, req.UID)
if err != nil {
if strings.HasPrefix(req.UID, "anon-") {
if settings, err := b.ms.SettingRule.ApplyRulesToAnonymous(ctx, req.UID, productID, req.Channel, req.Client); err == nil {
Expand All @@ -185,25 +188,25 @@ func (b *User) ListSettingsUnionAll(ctx context.Context, req tpl.MySettingsQuery
}

if req.Module != "" {
moduleID, err = b.ms.Module.AcquireID(ctx, productID, req.Module)
moduleID, err = b.ms.Module.AcquireID(readCtx, productID, req.Module)
if err != nil {
return nil, err
}
}
if req.Setting != "" {
settingID, err = b.ms.Setting.AcquireID(ctx, moduleID, req.Setting)
settingID, err = b.ms.Setting.AcquireID(readCtx, moduleID, req.Setting)
if err != nil {
return nil, err
}
}

groupIDs, err := b.ms.Group.FindIDsByUser(ctx, user.ID)
groupIDs, err := b.ms.Group.FindIDsByUser(readCtx, user.ID)
if err != nil {
return nil, err
}

pg := req.Pagination
settings, err := b.ms.User.FindSettingsUnionAll(ctx, groupIDs, user.ID, productID, moduleID, settingID, pg, req.Channel, req.Client)
settings, err := b.ms.User.FindSettingsUnionAll(readCtx, groupIDs, user.ID, productID, moduleID, settingID, pg, req.Channel, req.Client)
if err != nil {
return nil, err
}
Expand All @@ -226,7 +229,7 @@ func (b *User) ListSettingsUnionAll(ctx context.Context, req tpl.MySettingsQuery

// CheckExists ...
func (b *User) CheckExists(ctx context.Context, uid string) bool {
user, _ := b.ms.User.FindByUID(ctx, uid, "id")
user, _ := b.ms.User.FindByUID(context.WithValue(ctx, model.ReadDB, true), uid, "id")
return user != nil
}

Expand Down
1 change: 1 addition & 0 deletions src/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ConfigTpl struct {
KeyFile string `json:"key_file" yaml:"key_file"`
Logger Logger `json:"logger" yaml:"logger"`
MySQL SQL `json:"mysql" yaml:"mysql"`
MySQLRd SQL `json:"mysql_read" yaml:"mysql_read"`
CacheLabelExpire string `json:"cache_label_expire" yaml:"cache_label_expire"`
Channels []string `json:"channels" yaml:"channels"`
Clients []string `json:"clients" yaml:"clients"`
Expand Down
25 changes: 20 additions & 5 deletions src/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ func init() {
util.DigProvide(NewModels)
}

type dbMode string

// ReadDB ...
const ReadDB dbMode = "ReadDB"

// Model ...
type Model struct {
SQL *service.SQL
DB *goqu.Database
SQL *service.SQL
DB *goqu.Database
RdDB *goqu.Database
}

// Models ...
Expand All @@ -40,7 +46,7 @@ type Models struct {

// NewModels ...
func NewModels(sql *service.SQL) *Models {
m := &Model{SQL: sql, DB: sql.DB}
m := &Model{SQL: sql, DB: sql.DB, RdDB: sql.RdDB}
return &Models{
Model: m,
Healthz: &Healthz{m},
Expand Down Expand Up @@ -109,7 +115,12 @@ func (m *Model) findOneByID(ctx context.Context, table string, id int64, i inter
return fmt.Errorf("invalid id %d or table %s for findOneByID", id, table)
}

sd := m.DB.From(table).Where(goqu.C("id").Eq(id)).Order(goqu.C("id").Asc()).Limit(1)
db := m.DB
if ctx.Value(ReadDB) != nil {
db = m.RdDB
}
sd := db.From(table).Where(goqu.C("id").Eq(id)).Order(goqu.C("id").Asc()).Limit(1)

ok, err := sd.Executor().ScanStructContext(ctx, i)
if err != nil {
return err
Expand All @@ -125,7 +136,11 @@ func (m *Model) findOneByCols(ctx context.Context, table string, cls goqu.Ex, se
return false, fmt.Errorf("invalid clause %v for findOneByCols", cls)
}

sd := m.DB.From(table).Where(cls).Order(goqu.C("id").Asc()).Limit(1)
db := m.DB
if ctx.Value(ReadDB) != nil {
db = m.RdDB
}
sd := db.From(table).Where(cls).Order(goqu.C("id").Asc()).Limit(1)
if selectStr != "" {
sd = sd.Select(goqu.L(selectStr))
}
Expand Down
18 changes: 9 additions & 9 deletions src/model/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (m *Group) AcquireID(ctx context.Context, uid string) (int64, error) {
func (m *Group) Find(ctx context.Context, kind string, pg tpl.Pagination) ([]schema.Group, int, error) {
groups := make([]schema.Group, 0)
cursor := pg.TokenToID()
sdc := m.DB.From(schema.TableGroup)
sd := m.DB.From(schema.TableGroup).Where(goqu.C("id").Lte(cursor))
sdc := m.RdDB.From(schema.TableGroup)
sd := m.RdDB.From(schema.TableGroup).Where(goqu.C("id").Lte(cursor))
if kind != "" {
sdc = sdc.Where(goqu.C("kind").Eq(kind))
sd = sd.Where(goqu.C("kind").Eq(kind))
Expand All @@ -86,7 +86,7 @@ func (m *Group) FindLabels(ctx context.Context, groupID int64, pg tpl.Pagination
data := make([]tpl.MyLabel, 0)
cursor := pg.TokenToID()

sdc := m.DB.Select().
sdc := m.RdDB.Select().
From(
goqu.T(schema.TableGroupLabel).As("t1"),
goqu.T(schema.TableLabel).As("t2"),
Expand All @@ -95,7 +95,7 @@ func (m *Group) FindLabels(ctx context.Context, groupID int64, pg tpl.Pagination
goqu.I("t1.group_id").Eq(groupID),
goqu.I("t1.label_id").Eq(goqu.I("t2.id")))

sd := m.DB.Select(
sd := m.RdDB.Select(
goqu.I("t1.rls"),
goqu.I("t1.created_at").As("assigned_at"),
goqu.I("t2.id"),
Expand Down Expand Up @@ -150,15 +150,15 @@ func (m *Group) FindSettings(ctx context.Context, groupID, productID, moduleID,
data := []tpl.MySetting{}
cursor := pg.TokenToID()

sdc := m.DB.Select().
sdc := m.RdDB.Select().
From(
goqu.T(schema.TableGroupSetting).As("t1"),
goqu.T(schema.TableSetting).As("t2"),
goqu.T(schema.TableModule).As("t3"),
goqu.T(schema.TableProduct).As("t4")).
Where(goqu.I("t1.group_id").Eq(groupID))

sd := m.DB.Select(
sd := m.RdDB.Select(
goqu.I("t1.rls"),
goqu.I("t1.updated_at").As("assigned_at"),
goqu.I("t1.value"),
Expand Down Expand Up @@ -329,15 +329,15 @@ func (m *Group) FindMembers(ctx context.Context, groupID int64, pg tpl.Paginatio
data := []tpl.GroupMember{}
cursor := pg.TokenToID()

sdc := m.DB.Select().
sdc := m.RdDB.Select().
From(
goqu.T(schema.TableUserGroup).As("t1"),
goqu.T(schema.TableUser).As("t2")).
Where(
goqu.I("t1.group_id").Eq(groupID),
goqu.I("t1.user_id").Eq(goqu.I("t2.id")))

sd := m.DB.Select(
sd := m.RdDB.Select(
goqu.I("t1.id"),
goqu.I("t2.uid"),
goqu.I("t1.created_at"),
Expand Down Expand Up @@ -385,7 +385,7 @@ func (m *Group) FindMembers(ctx context.Context, groupID int64, pg tpl.Paginatio
// FindIDsByUser 根据 userID 查找加入的 Group ID 数组
func (m *Group) FindIDsByUser(ctx context.Context, userID int64) ([]int64, error) {
ids := make([]int64, 0)
sd := m.DB.From(schema.TableUserGroup).Where(goqu.C("user_id").Eq(userID)).Limit(1000)
sd := m.RdDB.From(schema.TableUserGroup).Where(goqu.C("user_id").Eq(userID)).Limit(1000)
if err := sd.PluckContext(ctx, &ids, "group_id"); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 0e2b722

Please sign in to comment.