エンジングループを作成するときに、特別な負荷分散戦略を示すことができます。xormには5つの負荷分散があります。彼らはRandomPolicy
、WeightRandomPolicy
、RoundRobinPolicy
、WeightRoundRobinPolicy
とLeastConnPolicy
。インターフェースに基づいて独自の戦略を実装することもできGroupPolicy
ます。
- ランダムポリシー
import (
_ "github.com/lib/pq"
"xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
conns := []string{
"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
}
var err error
eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RandomPolicy())
}
- ウェイトランダムポリシー
import (
_ "github.com/lib/pq"
"xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
conns := []string{
"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
}
var err error
// set the weights
eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
}
- RoundRobinPolicy
import (
_ "github.com/lib/pq"
"xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
conns := []string{
"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
}
var err error
eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
}
- WeightRoundRobinPolicy
import (
_ "github.com/lib/pq"
"xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
conns := []string{
"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
}
var err error
// set the weights
eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
}
- 最小接続ポリシー
import (
_ "github.com/lib/pq"
"xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
conns := []string{
"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
}
var err error
eg, err = xorm.NewEngineGroup("postgres", conns, xorm.LeastConnPolicy())
}