From 0e2b72285ad14d1ce70253e034d3c4572c01bcda Mon Sep 17 00:00:00 2001 From: Yan Qing Date: Wed, 8 Jul 2020 19:41:12 +0800 Subject: [PATCH] Support primary and secondary mysql connections. --- CHANGELOG.md | 5 +++++ Makefile | 2 +- config/default.yml | 8 ++++++++ src/bll/group.go | 17 +++++++++-------- src/bll/user.go | 29 ++++++++++++++++------------- src/conf/config.go | 1 + src/model/common.go | 25 ++++++++++++++++++++----- src/model/group.go | 18 +++++++++--------- src/model/label.go | 12 ++++++------ src/model/label_rule.go | 8 ++++---- src/model/module.go | 4 ++-- src/model/product.go | 10 +++++----- src/model/setting.go | 12 ++++++------ src/model/setting_rule.go | 8 ++++---- src/model/user.go | 14 +++++++------- src/service/mysql.go | 27 ++++++++++++++++++--------- 16 files changed, 121 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3119a09..accb3e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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:** diff --git a/Makefile b/Makefile index 621871c..d63147d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/config/default.yml b/config/default.yml index 097d30b..1624349 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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 diff --git a/src/bll/group.go b/src/bll/group.go index dc75565..7fc67f7 100644 --- a/src/bll/group.go +++ b/src/bll/group.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/src/bll/user.go b/src/bll/user.go index 933d947..35e2dd4 100644 --- a/src/bll/user.go +++ b/src/bll/user.go @@ -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 { @@ -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 } @@ -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 } @@ -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 { @@ -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 } @@ -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 } diff --git a/src/conf/config.go b/src/conf/config.go index 569f98a..0fc0dfb 100644 --- a/src/conf/config.go +++ b/src/conf/config.go @@ -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"` diff --git a/src/model/common.go b/src/model/common.go index e4b471f..fa9b740 100644 --- a/src/model/common.go +++ b/src/model/common.go @@ -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 ... @@ -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}, @@ -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 @@ -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)) } diff --git a/src/model/group.go b/src/model/group.go index f499218..c297bad 100644 --- a/src/model/group.go +++ b/src/model/group.go @@ -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)) @@ -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"), @@ -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"), @@ -150,7 +150,7 @@ 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"), @@ -158,7 +158,7 @@ func (m *Group) FindSettings(ctx context.Context, groupID, productID, moduleID, 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"), @@ -329,7 +329,7 @@ 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")). @@ -337,7 +337,7 @@ func (m *Group) FindMembers(ctx context.Context, groupID int64, pg tpl.Paginatio 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"), @@ -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 } diff --git a/src/model/label.go b/src/model/label.go index 5661ac6..04affc4 100644 --- a/src/model/label.go +++ b/src/model/label.go @@ -77,13 +77,13 @@ func (m *Label) Find(ctx context.Context, productID int64, pg tpl.Pagination) ([ labels := make([]schema.Label, 0) cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From(goqu.T(schema.TableLabel)). Where( goqu.C("product_id").Eq(productID), goqu.C("offline_at").IsNull()) - sd := m.DB.Select(). + sd := m.RdDB.Select(). From(goqu.T(schema.TableLabel)). Where( goqu.C("product_id").Eq(productID), @@ -304,7 +304,7 @@ func (m *Label) ListUsers(ctx context.Context, labelID int64, pg tpl.Pagination) data := []tpl.LabelUserInfo{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableUserLabel).As("t1"), goqu.T(schema.TableUser).As("t2")). @@ -312,7 +312,7 @@ func (m *Label) ListUsers(ctx context.Context, labelID int64, pg tpl.Pagination) goqu.I("t1.label_id").Eq(labelID), goqu.I("t1.user_id").Eq(goqu.I("t2.id"))) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.created_at").As("assigned_at"), goqu.I("t1.rls"), @@ -362,7 +362,7 @@ func (m *Label) ListUsers(ctx context.Context, labelID int64, pg tpl.Pagination) func (m *Label) ListGroups(ctx context.Context, labelID int64, pg tpl.Pagination) ([]tpl.LabelGroupInfo, int, error) { data := []tpl.LabelGroupInfo{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableGroupLabel).As("t1"), goqu.T(schema.TableGroup).As("t2")). @@ -370,7 +370,7 @@ func (m *Label) ListGroups(ctx context.Context, labelID int64, pg tpl.Pagination goqu.I("t1.label_id").Eq(labelID), goqu.I("t1.group_id").Eq(goqu.I("t2.id"))) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.created_at").As("assigned_at"), goqu.I("t1.rls"), diff --git a/src/model/label_rule.go b/src/model/label_rule.go index 3bf3fd9..3508422 100644 --- a/src/model/label_rule.go +++ b/src/model/label_rule.go @@ -21,7 +21,7 @@ type LabelRule struct { func (m *LabelRule) ApplyRules(ctx context.Context, userID int64, excludeLabels []int64) (int, error) { rules := []schema.LabelRule{} // 不把 excludeLabels 放入查询条件,从而尽量复用查询缓存 - sd := m.DB.From(schema.TableLabelRule). + sd := m.RdDB.From(schema.TableLabelRule). Where(goqu.C("kind").Eq("userPercent")).Order(goqu.C("updated_at").Desc()).Limit(200) err := sd.Executor().ScanStructsContext(ctx, &rules) if err != nil { @@ -66,7 +66,7 @@ func (m *LabelRule) ApplyRules(ctx context.Context, userID int64, excludeLabels // ApplyRulesToAnonymous ... func (m *LabelRule) ApplyRulesToAnonymous(ctx context.Context, anonymousID string, productID int64) ([]schema.UserCacheLabel, error) { rules := []schema.LabelRule{} - sd := m.DB.From(schema.TableLabelRule). + sd := m.RdDB.From(schema.TableLabelRule). Where( goqu.C("kind").Eq("userPercent"), goqu.C("product_id").Eq(productID)). @@ -88,7 +88,7 @@ func (m *LabelRule) ApplyRulesToAnonymous(ctx context.Context, anonymousID strin data := make([]schema.UserCacheLabel, 0) if len(labelIDs) > 0 { - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.name"), goqu.I("t1.channels"), @@ -143,7 +143,7 @@ func (m *LabelRule) Acquire(ctx context.Context, labelRuleID int64) (*schema.Lab // Find ... func (m *LabelRule) Find(ctx context.Context, productID, labelID int64) ([]schema.LabelRule, error) { labelRules := make([]schema.LabelRule, 0) - sd := m.DB.From(schema.TableLabelRule). + sd := m.RdDB.From(schema.TableLabelRule). Where(goqu.C("product_id").Eq(productID), goqu.C("label_id").Eq(labelID)). Order(goqu.C("id").Desc()).Limit(10) diff --git a/src/model/module.go b/src/model/module.go index c6e5222..36e9b4f 100644 --- a/src/model/module.go +++ b/src/model/module.go @@ -64,13 +64,13 @@ func (m *Module) AcquireID(ctx context.Context, productID int64, moduleName stri func (m *Module) Find(ctx context.Context, productID int64, pg tpl.Pagination) ([]schema.Module, int, error) { modules := make([]schema.Module, 0) cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From(goqu.T(schema.TableModule)). Where( goqu.C("product_id").Eq(productID), goqu.C("offline_at").IsNull()) - sd := m.DB.Select(). + sd := m.RdDB.Select(). From(goqu.T(schema.TableModule)). Where( goqu.C("id").Lte(cursor), diff --git a/src/model/product.go b/src/model/product.go index b40c51d..d7ff480 100644 --- a/src/model/product.go +++ b/src/model/product.go @@ -70,13 +70,13 @@ func (m *Product) AcquireID(ctx context.Context, productName string) (int64, err func (m *Product) Find(ctx context.Context, pg tpl.Pagination) ([]schema.Product, int, error) { products := make([]schema.Product, 0) cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From(goqu.T(schema.TableProduct)). Where( goqu.C("deleted_at").IsNull(), goqu.C("offline_at").IsNull()) - sd := m.DB.Select(). + sd := m.RdDB.Select(). From(goqu.T(schema.TableProduct)). Where( goqu.C("id").Lte(cursor), @@ -161,7 +161,7 @@ func (m *Product) Delete(ctx context.Context, productID int64) error { // Statistics 返回产品的统计数据 func (m *Product) Statistics(ctx context.Context, productID int64) (*tpl.ProductStatistics, error) { res := &tpl.ProductStatistics{} - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.COUNT("id").As("labels"), goqu.L("IFNULL(SUM(`status`), 0)").As("status"), goqu.L("IFNULL(SUM(`rls`), 0)").As("release")). @@ -175,7 +175,7 @@ func (m *Product) Statistics(ctx context.Context, productID int64) (*tpl.Product } moduleIDs := make([]int64, 0) - sd = m.DB.Select("id"). + sd = m.RdDB.Select("id"). From(goqu.T(schema.TableModule)). Where( goqu.C("product_id").Eq(productID), @@ -186,7 +186,7 @@ func (m *Product) Statistics(ctx context.Context, productID int64) (*tpl.Product if len(moduleIDs) > 0 { res.Modules = int64(len(moduleIDs)) - sd = m.DB.Select( + sd = m.RdDB.Select( goqu.COUNT("id").As("settings"), goqu.L("IFNULL(SUM(`status`), 0)").As("status"), goqu.L("IFNULL(SUM(`rls`), 0)").As("release")). diff --git a/src/model/setting.go b/src/model/setting.go index a5319fe..b209424 100644 --- a/src/model/setting.go +++ b/src/model/setting.go @@ -79,7 +79,7 @@ func (m *Setting) Find(ctx context.Context, productID, moduleID int64, pg tpl.Pa data := make([]schema.Setting, 0) cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableSetting).As("t1"), goqu.T(schema.TableModule).As("t2")). @@ -87,7 +87,7 @@ func (m *Setting) Find(ctx context.Context, productID, moduleID int64, pg tpl.Pa goqu.I("t2.product_id").Eq(productID), goqu.I("t2.offline_at").IsNull()) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.created_at"), goqu.I("t1.updated_at"), @@ -373,7 +373,7 @@ func (m *Setting) ListUsers(ctx context.Context, settingID int64, pg tpl.Paginat data := []tpl.SettingUserInfo{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableUserSetting).As("t1"), goqu.T(schema.TableUser).As("t2")). @@ -381,7 +381,7 @@ func (m *Setting) ListUsers(ctx context.Context, settingID int64, pg tpl.Paginat goqu.I("t1.setting_id").Eq(settingID), goqu.I("t1.user_id").Eq(goqu.I("t2.id"))) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.updated_at").As("assigned_at"), goqu.I("t1.rls"), @@ -433,7 +433,7 @@ func (m *Setting) ListUsers(ctx context.Context, settingID int64, pg tpl.Paginat func (m *Setting) ListGroups(ctx context.Context, settingID int64, pg tpl.Pagination) ([]tpl.SettingGroupInfo, int, error) { data := []tpl.SettingGroupInfo{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableGroupSetting).As("t1"), goqu.T(schema.TableGroup).As("t2")). @@ -441,7 +441,7 @@ func (m *Setting) ListGroups(ctx context.Context, settingID int64, pg tpl.Pagina goqu.I("t1.setting_id").Eq(settingID), goqu.I("t1.group_id").Eq(goqu.I("t2.id"))) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.id"), goqu.I("t1.updated_at").As("assigned_at"), goqu.I("t1.rls"), diff --git a/src/model/setting_rule.go b/src/model/setting_rule.go index a5ec528..40782b5 100644 --- a/src/model/setting_rule.go +++ b/src/model/setting_rule.go @@ -18,7 +18,7 @@ type SettingRule struct { // ApplyRules ... func (m *SettingRule) ApplyRules(ctx context.Context, productID, userID int64) error { rules := []schema.SettingRule{} - sd := m.DB.From(schema.TableSettingRule). + sd := m.RdDB.From(schema.TableSettingRule). Where(goqu.C("product_id").Eq(productID), goqu.C("kind").Eq("userPercent")). Order(goqu.C("updated_at").Desc()).Limit(1000) err := sd.Executor().ScanStructsContext(ctx, &rules) @@ -73,7 +73,7 @@ func (m *SettingRule) ApplyRules(ctx context.Context, productID, userID int64) e // ApplyRulesToAnonymous ... func (m *SettingRule) ApplyRulesToAnonymous(ctx context.Context, anonymousID string, productID int64, channel, client string) ([]tpl.MySetting, error) { rules := []schema.SettingRule{} - sd := m.DB.From(schema.TableSettingRule). + sd := m.RdDB.From(schema.TableSettingRule). Where(goqu.C("product_id").Eq(productID), goqu.C("kind").Eq("userPercent")). Order(goqu.C("updated_at").Desc()).Limit(1000) err := sd.Executor().ScanStructsContext(ctx, &rules) @@ -93,7 +93,7 @@ func (m *SettingRule) ApplyRulesToAnonymous(ctx context.Context, anonymousID str data := make([]tpl.MySetting, 0) if len(ids) > 0 { - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.rls"), goqu.I("t1.updated_at").As("assigned_at"), goqu.I("t1.value"), @@ -160,7 +160,7 @@ func (m *SettingRule) Acquire(ctx context.Context, settingRuleID int64) (*schema // Find ... func (m *SettingRule) Find(ctx context.Context, productID, settingID int64) ([]schema.SettingRule, error) { settingRules := make([]schema.SettingRule, 0) - sd := m.DB.From(schema.TableSettingRule). + sd := m.RdDB.From(schema.TableSettingRule). Where(goqu.C("product_id").Eq(productID), goqu.C("setting_id").Eq(settingID)). Order(goqu.C("id").Desc()).Limit(10) diff --git a/src/model/user.go b/src/model/user.go index f0edd19..24b5def 100644 --- a/src/model/user.go +++ b/src/model/user.go @@ -61,8 +61,8 @@ func (m *User) AcquireID(ctx context.Context, uid string) (int64, error) { func (m *User) Find(ctx context.Context, pg tpl.Pagination) ([]schema.User, int, error) { users := make([]schema.User, 0) cursor := pg.TokenToID() - sdc := m.DB.From(schema.TableUser) - sd := m.DB.From(schema.TableUser).Where(goqu.C("id").Lte(cursor)) + sdc := m.RdDB.From(schema.TableUser) + sd := m.RdDB.From(schema.TableUser).Where(goqu.C("id").Lte(cursor)) var total int64 var err error @@ -210,7 +210,7 @@ func (m *User) FindSettingsUnionAll(ctx context.Context, groupIDs []int64, userI set := make(map[int64]struct{}) size := pg.PageSize + 1 - s := m.DB.Select( + s := m.RdDB.Select( goqu.I("t1.rls"), goqu.I("t1.updated_at").As("assigned_at"), goqu.I("t1.value"), @@ -344,7 +344,7 @@ func (m *User) FindLabels(ctx context.Context, userID int64, pg tpl.Pagination) data := []tpl.MyLabel{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableUserLabel).As("t1"), goqu.T(schema.TableLabel).As("t2"), @@ -353,7 +353,7 @@ func (m *User) FindLabels(ctx context.Context, userID int64, pg tpl.Pagination) goqu.I("t1.user_id").Eq(userID), 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"), @@ -407,7 +407,7 @@ func (m *User) FindLabels(ctx context.Context, userID int64, pg tpl.Pagination) func (m *User) FindSettings(ctx context.Context, userID, productID, moduleID, settingID int64, pg tpl.Pagination, channel, client string) ([]tpl.MySetting, int, error) { data := []tpl.MySetting{} cursor := pg.TokenToID() - sdc := m.DB.Select(). + sdc := m.RdDB.Select(). From( goqu.T(schema.TableUserSetting).As("t1"), goqu.T(schema.TableSetting).As("t2"), @@ -415,7 +415,7 @@ func (m *User) FindSettings(ctx context.Context, userID, productID, moduleID, se goqu.T(schema.TableProduct).As("t4")). Where(goqu.I("t1.user_id").Eq(userID)) - sd := m.DB.Select( + sd := m.RdDB.Select( goqu.I("t1.rls"), goqu.I("t1.updated_at").As("assigned_at"), goqu.I("t1.value"), diff --git a/src/service/mysql.go b/src/service/mysql.go index f3dde85..6ceb724 100644 --- a/src/service/mysql.go +++ b/src/service/mysql.go @@ -23,8 +23,9 @@ func init() { // SQL ... type SQL struct { - db *sql.DB - DB *goqu.Database + db *sql.DB + DB *goqu.Database + RdDB *goqu.Database } // DBStats ... @@ -34,8 +35,21 @@ func (s *SQL) DBStats() sql.DBStats { // NewDB ... func NewDB() *SQL { - cfg := conf.Config.MySQL + db := connectDB(conf.Config.MySQL) + rdDB := db + if conf.Config.MySQLRd.Host != "" { + rdDB = connectDB(conf.Config.MySQLRd) + } + + dialect := goqu.Dialect("mysql") + return &SQL{ + db: db, + DB: dialect.DB(db), + RdDB: dialect.DB(rdDB), + } +} +func connectDB(cfg conf.SQL) *sql.DB { if cfg.MaxIdleConns <= 0 { cfg.MaxIdleConns = 8 } @@ -75,12 +89,7 @@ func NewDB() *SQL { db.SetMaxOpenConns(cfg.MaxOpenConns) // SetConnMaxLifetiment 设置连接的最大可复用时间。 // db.SetConnMaxLifetime(time.Hour) - - dialect := goqu.Dialect("mysql") - return &SQL{ - db: db, - DB: dialect.DB(db), - } + return db } // DeResult ...