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. )
6. var eg *xorm.EngineGroup
8. func main() {
9. conns := []string{
10. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
11. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
12. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
13. }
15. var err error
16. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RandomPolicy())
17. }
- WeightRandomPolicy
1. import (
2. _ "github.com/lib/pq"
3. "xorm.io/xorm"
4. )
6. var eg *xorm.EngineGroup
8. func main() {
9. conns := []string{
10. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
11. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
12. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
13. }
15. var err error
16. // set the weights
17. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
18. }
- RoundRobinPolicy
1. import (
2. _ "github.com/lib/pq"
3. "xorm.io/xorm"
4. )
6. var eg *xorm.EngineGroup
8. func main() {
9. conns := []string{
10. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
11. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
12. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
13. }
15. var err error
16. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
17. }
- WeightRoundRobinPolicy
1. import (
2. _ "github.com/lib/pq"
3. "xorm.io/xorm"
4. )
6. var eg *xorm.EngineGroup
8. func main() {
9. conns := []string{
10. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
11. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
12. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
13. }
15. var err error
16. // set the weights
17. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
18. }
- LeastConnPolicy
1. import (
2. _ "github.com/lib/pq"
3. "xorm.io/xorm"
4. )
6. var eg *xorm.EngineGroup
8. func main() {
9. conns := []string{
10. "postgres://postgres:[email protected]:5432/test?sslmode=disable;",
11. "postgres://postgres:[email protected]:5432/test1?sslmode=disable;",
12. "postgres://postgres:[email protected]:5432/test2?sslmode=disable",
13. }
15. var err error
16. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.LeastConnPolicy())
17. }
- Customerize Policy
You can also implement yourself policy according GroupPolicy interface.
1. type GroupPolicy interface {
2. Slave(*EngineGroup) *Engine
3. }
