Load balance policy

Load balance policy

We can indicate a special load balance policy when we create engine group. There are 5 load balances on xorm. They are RandomPolicy, WeightRandomPolicy, RoundRobinPolicy, WeightRoundRobinPolicy and LeastConnPolicy. You can also implement yourself policy according GroupPolicy interface.

  • RandomPolicy
  1. import (
  2. _ "github.com/lib/pq"
  3. "xorm.io/xorm"
  4. )
  5. var eg *xorm.EngineGroup
  6. func main() {
  7. conns := []string{
  8. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
  9. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
  10. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
  11. }
  12. var err error
  13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RandomPolicy())
  14. }
  • WeightRandomPolicy
  1. import (
  2. _ "github.com/lib/pq"
  3. "xorm.io/xorm"
  4. )
  5. var eg *xorm.EngineGroup
  6. func main() {
  7. conns := []string{
  8. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
  9. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
  10. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
  11. }
  12. var err error
  13. // set the weights
  14. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
  15. }
  • RoundRobinPolicy
  1. import (
  2. _ "github.com/lib/pq"
  3. "xorm.io/xorm"
  4. )
  5. var eg *xorm.EngineGroup
  6. func main() {
  7. conns := []string{
  8. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
  9. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
  10. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
  11. }
  12. var err error
  13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
  14. }
  • WeightRoundRobinPolicy
  1. import (
  2. _ "github.com/lib/pq"
  3. "xorm.io/xorm"
  4. )
  5. var eg *xorm.EngineGroup
  6. func main() {
  7. conns := []string{
  8. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
  9. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
  10. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
  11. }
  12. var err error
  13. // set the weights
  14. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
  15. }
  • LeastConnPolicy
  1. import (
  2. _ "github.com/lib/pq"
  3. "xorm.io/xorm"
  4. )
  5. var eg *xorm.EngineGroup
  6. func main() {
  7. conns := []string{
  8. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
  9. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
  10. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
  11. }
  12. var err error
  13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.LeastConnPolicy())
  14. }
  • Customerize Policy

You can also implement yourself policy according GroupPolicy interface.

  1. type GroupPolicy interface {
  2. Slave(*EngineGroup) *Engine
  3. }