Skip to content

Commit 7b30f7d

Browse files
committed
✨ feat: added db resolver #18
1 parent 3a93d82 commit 7b30f7d

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-playground/locales v0.14.1
88
github.com/go-playground/universal-translator v0.18.1
99
github.com/go-playground/validator/v10 v10.16.0
10+
github.com/sivaosorg/db.resolver v0.0.3
1011
github.com/sivaosorg/govm v1.2.8
1112
github.com/sivaosorg/mysqlconn v1.0.5
1213
github.com/sivaosorg/postgresconn v1.0.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv
119119
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
120120
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
121121
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
122+
github.com/sivaosorg/db.resolver v0.0.3 h1:Awo3iI8zJSX+3IDf+g8azHWZ4Ikt8+8wlg9Byn37Q0o=
123+
github.com/sivaosorg/db.resolver v0.0.3/go.mod h1:2NK1c4aASa0h4qGQmIHrsdK3J322YxXrUup6jhDIIu8=
122124
github.com/sivaosorg/govm v1.2.8 h1:SyVwq7PNUKQQNqI9PbjZ6zY9b33lks75y/qzAzxDNyI=
123125
github.com/sivaosorg/govm v1.2.8/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
124126
github.com/sivaosorg/mysqlconn v1.0.5 h1:VHVfk6d6NQaMNsaLhojyzxF3PV+TA0I4wekIF5fU9QI=

internal/core/core.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/gin-gonic/gin"
10+
dbresolver "github.com/sivaosorg/db.resolver"
1011
syncconf "github.com/sivaosorg/gocell/internal/syncConf"
1112
"github.com/sivaosorg/gocell/pkg/constant"
1213
"github.com/sivaosorg/govm/blueprint"
@@ -32,6 +33,7 @@ type CoreCommand struct {
3233
handlers *coreHandler
3334
rabbitmq *rabbitmqconn.RabbitMq
3435
rabbitmqStatus dbx.Dbx
36+
resolver *dbresolver.MultiTenantDBResolver
3537
}
3638

3739
func (c *CoreCommand) Name() string {
@@ -50,6 +52,7 @@ func (c *CoreCommand) Execute(args []string) error {
5052
if err != nil {
5153
return err
5254
}
55+
c.seeker()
5356
c.conn()
5457
c.notify()
5558
c.handler()
@@ -61,11 +64,6 @@ func (c *CoreCommand) conn() {
6164
if syncconf.Conf == nil {
6265
return
6366
}
64-
syncconf.Conf.Postgres.SetTimeout(10 * time.Second)
65-
syncconf.Conf.MySql.SetTimeout(10 * time.Second)
66-
syncconf.Conf.Redis.SetTimeout(10 * time.Second)
67-
syncconf.Conf.RabbitMq.SetTimeout(10 * time.Second)
68-
6967
// Instances async
7068
var wg sync.WaitGroup
7169
wg.Add(1)
@@ -96,9 +94,34 @@ func (c *CoreCommand) conn() {
9694
c.rabbitmq = rabbitmq
9795
c.rabbitmqStatus = s
9896
}()
97+
wg.Add(1)
98+
go func() {
99+
defer wg.Done()
100+
c.resolver.
101+
AddPsqlConnectors(syncconf.Conf.PostgresSeekers...).
102+
AddMsqlConnectors(syncconf.Conf.MySqlSeekers...).
103+
SetDefaultConnector(dbresolver.NewPostgresConnector(syncconf.Conf.Postgres))
104+
}()
99105
wg.Wait()
100106
}
101107

108+
func (c *CoreCommand) seeker() {
109+
c.resolver = dbresolver.NewMultiTenantDBResolver()
110+
timeout := 10 * time.Second
111+
syncconf.Conf.Postgres.SetTimeout(timeout)
112+
syncconf.Conf.MySql.SetTimeout(timeout)
113+
syncconf.Conf.Redis.SetTimeout(timeout)
114+
syncconf.Conf.RabbitMq.SetTimeout(timeout)
115+
116+
// updating timeout for context db ping
117+
for idx := range syncconf.Conf.PostgresSeekers {
118+
syncconf.Conf.PostgresSeekers[idx].Config.SetTimeout(timeout)
119+
}
120+
for idx := range syncconf.Conf.MySqlSeekers {
121+
syncconf.Conf.MySqlSeekers[idx].Config.SetTimeout(timeout)
122+
}
123+
}
124+
102125
func (c *CoreCommand) run() {
103126
gin.SetMode(syncconf.Conf.Server.Mode)
104127
core := gin.New()

internal/core/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type coreHandler struct {
1414
}
1515

1616
func (c *CoreCommand) handler() {
17-
commonRepository := repository.NewCommonRepository(c.psql, c.psqlStatus)
17+
commonRepository := repository.NewCommonRepository(c.resolver)
1818
commonSvc := service.NewCommonService(commonRepository)
1919
commonHandler := handlers.NewCommonHandler(commonSvc)
2020

internal/repository/common_repository.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ package repository
22

33
import (
44
"github.com/sivaosorg/govm/dbx"
5-
"github.com/sivaosorg/postgresconn"
5+
6+
dbresolver "github.com/sivaosorg/db.resolver"
67
)
78

89
type CommonRepository interface {
910
GetPsqlStatus() dbx.Dbx
1011
}
1112

1213
type commonRepositoryImpl struct {
13-
psql *postgresconn.Postgres
14-
psqlStatus dbx.Dbx
14+
resolver *dbresolver.MultiTenantDBResolver
1515
}
1616

17-
func NewCommonRepository(psql *postgresconn.Postgres, psqlStatus dbx.Dbx) CommonRepository {
17+
func NewCommonRepository(resolver *dbresolver.MultiTenantDBResolver) CommonRepository {
1818
return &commonRepositoryImpl{
19-
psql: psql,
20-
psqlStatus: psqlStatus,
19+
resolver: resolver,
2120
}
2221
}
2322

2423
func (repo *commonRepositoryImpl) GetPsqlStatus() dbx.Dbx {
25-
return repo.psqlStatus
24+
_, s := repo.resolver.GetDefaultConnector()
25+
return s
2626
}

0 commit comments

Comments
 (0)