engine_group_policy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "math/rand"
  7. "sync"
  8. "time"
  9. )
  10. type GroupPolicy interface {
  11. Slave(*EngineGroup) *Engine
  12. }
  13. type RandomPolicy struct {
  14. r *rand.Rand
  15. }
  16. func NewRandomPolicy() *RandomPolicy {
  17. return &RandomPolicy{
  18. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  19. }
  20. }
  21. func (policy *RandomPolicy) Slave(g *EngineGroup) *Engine {
  22. return g.Slaves()[policy.r.Intn(len(g.Slaves()))]
  23. }
  24. type WeightRandomPolicy struct {
  25. weights []int
  26. rands []int
  27. r *rand.Rand
  28. }
  29. func NewWeightRandomPolicy(weights []int) *WeightRandomPolicy {
  30. var rands = make([]int, 0, len(weights))
  31. for i := 0; i < len(weights); i++ {
  32. for n := 0; n < weights[i]; n++ {
  33. rands = append(rands, i)
  34. }
  35. }
  36. return &WeightRandomPolicy{
  37. weights: weights,
  38. rands: rands,
  39. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  40. }
  41. }
  42. func (policy *WeightRandomPolicy) Slave(g *EngineGroup) *Engine {
  43. var slaves = g.Slaves()
  44. idx := policy.rands[policy.r.Intn(len(policy.rands))]
  45. if idx >= len(slaves) {
  46. idx = len(slaves) - 1
  47. }
  48. return slaves[idx]
  49. }
  50. type RoundRobinPolicy struct {
  51. pos int
  52. lock sync.Mutex
  53. }
  54. func NewRoundRobinPolicy() *RoundRobinPolicy {
  55. return &RoundRobinPolicy{pos: -1}
  56. }
  57. func (policy *RoundRobinPolicy) Slave(g *EngineGroup) *Engine {
  58. var slaves = g.Slaves()
  59. var pos int
  60. policy.lock.Lock()
  61. policy.pos++
  62. if policy.pos >= len(slaves) {
  63. policy.pos = 0
  64. }
  65. pos = policy.pos
  66. policy.lock.Unlock()
  67. return slaves[pos]
  68. }
  69. type WeightRoundRobinPolicy struct {
  70. weights []int
  71. rands []int
  72. r *rand.Rand
  73. lock sync.Mutex
  74. pos int
  75. }
  76. func NewWeightRoundRobinPolicy(weights []int) *WeightRoundRobinPolicy {
  77. var rands = make([]int, 0, len(weights))
  78. for i := 0; i < len(weights); i++ {
  79. for n := 0; n < weights[i]; n++ {
  80. rands = append(rands, i)
  81. }
  82. }
  83. return &WeightRoundRobinPolicy{
  84. weights: weights,
  85. rands: rands,
  86. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  87. pos: -1,
  88. }
  89. }
  90. func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine {
  91. var slaves = g.Slaves()
  92. var pos int
  93. policy.lock.Lock()
  94. policy.pos++
  95. if policy.pos >= len(policy.rands) {
  96. policy.pos = 0
  97. }
  98. pos = policy.pos
  99. policy.lock.Unlock()
  100. idx := policy.rands[pos]
  101. if idx >= len(slaves) {
  102. idx = len(slaves) - 1
  103. }
  104. return slaves[idx]
  105. }
  106. type LeastConnPolicy struct {
  107. }
  108. func NewLeastConnPolicy() *LeastConnPolicy {
  109. return &LeastConnPolicy{}
  110. }
  111. func (policy *LeastConnPolicy) Slave(g *EngineGroup) *Engine {
  112. var slaves = g.Slaves()
  113. connections := 0
  114. idx := 0
  115. for i, _ := range slaves {
  116. open_connections := slaves[i].DB().Stats().OpenConnections
  117. if i == 0 {
  118. connections = open_connections
  119. idx = i
  120. } else if open_connections <= connections {
  121. connections = open_connections
  122. idx = i
  123. }
  124. }
  125. return slaves[idx]
  126. }