Skip to content

Commit

Permalink
✨ feat: added db resolver #18
Browse files Browse the repository at this point in the history
  • Loading branch information
arisnguyen215 committed Dec 10, 2023
1 parent 3a93d82 commit 7b30f7d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.16.0
github.com/sivaosorg/db.resolver v0.0.3
github.com/sivaosorg/govm v1.2.8
github.com/sivaosorg/mysqlconn v1.0.5
github.com/sivaosorg/postgresconn v1.0.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sivaosorg/db.resolver v0.0.3 h1:Awo3iI8zJSX+3IDf+g8azHWZ4Ikt8+8wlg9Byn37Q0o=
github.com/sivaosorg/db.resolver v0.0.3/go.mod h1:2NK1c4aASa0h4qGQmIHrsdK3J322YxXrUup6jhDIIu8=
github.com/sivaosorg/govm v1.2.8 h1:SyVwq7PNUKQQNqI9PbjZ6zY9b33lks75y/qzAzxDNyI=
github.com/sivaosorg/govm v1.2.8/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
github.com/sivaosorg/mysqlconn v1.0.5 h1:VHVfk6d6NQaMNsaLhojyzxF3PV+TA0I4wekIF5fU9QI=
Expand Down
33 changes: 28 additions & 5 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/gin-gonic/gin"
dbresolver "github.com/sivaosorg/db.resolver"
syncconf "github.com/sivaosorg/gocell/internal/syncConf"
"github.com/sivaosorg/gocell/pkg/constant"
"github.com/sivaosorg/govm/blueprint"
Expand All @@ -32,6 +33,7 @@ type CoreCommand struct {
handlers *coreHandler
rabbitmq *rabbitmqconn.RabbitMq
rabbitmqStatus dbx.Dbx
resolver *dbresolver.MultiTenantDBResolver
}

func (c *CoreCommand) Name() string {
Expand All @@ -50,6 +52,7 @@ func (c *CoreCommand) Execute(args []string) error {
if err != nil {
return err
}
c.seeker()
c.conn()
c.notify()
c.handler()
Expand All @@ -61,11 +64,6 @@ func (c *CoreCommand) conn() {
if syncconf.Conf == nil {
return
}
syncconf.Conf.Postgres.SetTimeout(10 * time.Second)
syncconf.Conf.MySql.SetTimeout(10 * time.Second)
syncconf.Conf.Redis.SetTimeout(10 * time.Second)
syncconf.Conf.RabbitMq.SetTimeout(10 * time.Second)

// Instances async
var wg sync.WaitGroup
wg.Add(1)
Expand Down Expand Up @@ -96,9 +94,34 @@ func (c *CoreCommand) conn() {
c.rabbitmq = rabbitmq
c.rabbitmqStatus = s
}()
wg.Add(1)
go func() {
defer wg.Done()
c.resolver.
AddPsqlConnectors(syncconf.Conf.PostgresSeekers...).
AddMsqlConnectors(syncconf.Conf.MySqlSeekers...).
SetDefaultConnector(dbresolver.NewPostgresConnector(syncconf.Conf.Postgres))
}()
wg.Wait()
}

func (c *CoreCommand) seeker() {
c.resolver = dbresolver.NewMultiTenantDBResolver()
timeout := 10 * time.Second
syncconf.Conf.Postgres.SetTimeout(timeout)
syncconf.Conf.MySql.SetTimeout(timeout)
syncconf.Conf.Redis.SetTimeout(timeout)
syncconf.Conf.RabbitMq.SetTimeout(timeout)

// updating timeout for context db ping
for idx := range syncconf.Conf.PostgresSeekers {
syncconf.Conf.PostgresSeekers[idx].Config.SetTimeout(timeout)
}
for idx := range syncconf.Conf.MySqlSeekers {
syncconf.Conf.MySqlSeekers[idx].Config.SetTimeout(timeout)
}
}

func (c *CoreCommand) run() {
gin.SetMode(syncconf.Conf.Server.Mode)
core := gin.New()
Expand Down
2 changes: 1 addition & 1 deletion internal/core/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type coreHandler struct {
}

func (c *CoreCommand) handler() {
commonRepository := repository.NewCommonRepository(c.psql, c.psqlStatus)
commonRepository := repository.NewCommonRepository(c.resolver)
commonSvc := service.NewCommonService(commonRepository)
commonHandler := handlers.NewCommonHandler(commonSvc)

Expand Down
14 changes: 7 additions & 7 deletions internal/repository/common_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package repository

import (
"github.com/sivaosorg/govm/dbx"
"github.com/sivaosorg/postgresconn"

dbresolver "github.com/sivaosorg/db.resolver"
)

type CommonRepository interface {
GetPsqlStatus() dbx.Dbx
}

type commonRepositoryImpl struct {
psql *postgresconn.Postgres
psqlStatus dbx.Dbx
resolver *dbresolver.MultiTenantDBResolver
}

func NewCommonRepository(psql *postgresconn.Postgres, psqlStatus dbx.Dbx) CommonRepository {
func NewCommonRepository(resolver *dbresolver.MultiTenantDBResolver) CommonRepository {
return &commonRepositoryImpl{
psql: psql,
psqlStatus: psqlStatus,
resolver: resolver,
}
}

func (repo *commonRepositoryImpl) GetPsqlStatus() dbx.Dbx {
return repo.psqlStatus
_, s := repo.resolver.GetDefaultConnector()
return s
}

0 comments on commit 7b30f7d

Please sign in to comment.