1
0

config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package database
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. "gorm.io/plugin/dbresolver"
  6. )
  7. type DBConfig struct {
  8. dsn string
  9. connMaxIdleTime int
  10. connMaxLifetime int
  11. maxIdleConns int
  12. maxOpenConns int
  13. registers []ResolverConfigure
  14. }
  15. // NewConfigure 初始化 Configure
  16. func NewConfigure(
  17. dsn string,
  18. maxIdleConns,
  19. maxOpenConns,
  20. connMaxIdleTime,
  21. connMaxLifetime int,
  22. registers []ResolverConfigure) Configure {
  23. return &DBConfig{
  24. dsn: dsn,
  25. connMaxIdleTime: connMaxIdleTime,
  26. connMaxLifetime: connMaxLifetime,
  27. maxIdleConns: maxIdleConns,
  28. maxOpenConns: maxOpenConns,
  29. registers: registers,
  30. }
  31. }
  32. // Init 获取db,⚠️注意:读写分离只能配置一组
  33. func (e *DBConfig) Init(config *gorm.Config, open func(string) gorm.Dialector) (*gorm.DB, error) {
  34. db, err := gorm.Open(open(e.dsn), config)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var register *dbresolver.DBResolver
  39. for i := range e.registers {
  40. register = e.registers[i].Init(register, open)
  41. }
  42. if register == nil {
  43. register = dbresolver.Register(dbresolver.Config{})
  44. }
  45. if e.connMaxIdleTime > 0 {
  46. register = register.SetConnMaxIdleTime(time.Duration(e.connMaxIdleTime) * time.Second)
  47. }
  48. if e.connMaxLifetime > 0 {
  49. register = register.SetConnMaxLifetime(time.Duration(e.connMaxLifetime) * time.Second)
  50. }
  51. if e.maxOpenConns > 0 {
  52. register = register.SetMaxOpenConns(e.maxOpenConns)
  53. }
  54. if e.maxIdleConns > 0 {
  55. register = register.SetMaxIdleConns(e.maxIdleConns)
  56. }
  57. if register != nil {
  58. err = db.Use(register)
  59. }
  60. return db, err
  61. }
  62. type DBResolverConfig struct {
  63. sources []string
  64. replicas []string
  65. policy string
  66. tables []interface{}
  67. }
  68. // NewResolverConfigure 初始化 ResolverConfigure
  69. func NewResolverConfigure(sources, replicas []string, policy string, tables []string) ResolverConfigure {
  70. data := make([]interface{}, len(tables))
  71. for i := range tables {
  72. data[i] = tables[i]
  73. }
  74. return &DBResolverConfig{
  75. sources: sources,
  76. replicas: replicas,
  77. policy: policy,
  78. tables: data,
  79. }
  80. }
  81. func (e *DBResolverConfig) Init(
  82. register *dbresolver.DBResolver,
  83. open func(string) gorm.Dialector) *dbresolver.DBResolver {
  84. if len(e.tables) == 0 && len(e.sources) == 0 && len(e.replicas) == 0 {
  85. return register
  86. }
  87. var config dbresolver.Config
  88. if len(e.sources) > 0 {
  89. config.Sources = make([]gorm.Dialector, len(e.sources))
  90. for i := range e.sources {
  91. config.Sources[i] = open(e.sources[i])
  92. }
  93. }
  94. if len(e.replicas) > 0 {
  95. config.Replicas = make([]gorm.Dialector, len(e.replicas))
  96. for i := range e.replicas {
  97. config.Replicas[i] = open(e.replicas[i])
  98. }
  99. }
  100. if e.policy != "" {
  101. policy, ok := policies[e.policy]
  102. if ok {
  103. config.Policy = policy
  104. }
  105. }
  106. if register == nil {
  107. register = dbresolver.Register(config, e.tables...)
  108. return register
  109. }
  110. register = register.Register(config, e.tables...)
  111. return register
  112. }