rename Priority to Weight for gdb

This commit is contained in:
John
2019-07-06 16:14:45 +08:00
parent 86343abcf8
commit 9cf4ea5c91
9 changed files with 43 additions and 69 deletions

View File

@ -248,9 +248,9 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
slaveList = masterList
}
if master {
return getConfigNodeByPriority(masterList), nil
return getConfigNodeByWeight(masterList), nil
} else {
return getConfigNodeByPriority(slaveList), nil
return getConfigNodeByWeight(slaveList), nil
}
} else {
return nil, errors.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
@ -263,19 +263,19 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
// 2、那么节点1的权重范围为[0, 99]节点2的权重范围为[100, 199]比例为1:1
// 3、假如计算出的随机数为99;
// 4、那么选择的配置为节点1;
func getConfigNodeByPriority(cg ConfigGroup) *ConfigNode {
func getConfigNodeByWeight(cg ConfigGroup) *ConfigNode {
if len(cg) < 2 {
return &cg[0]
}
var total int
for i := 0; i < len(cg); i++ {
total += cg[i].Priority * 100
total += cg[i].Weight * 100
}
// 如果total为0表示所有连接都没有配置priority属性那么默认都是1
if total == 0 {
for i := 0; i < len(cg); i++ {
cg[i].Priority = 1
total += cg[i].Priority * 100
cg[i].Weight = 1
total += cg[i].Weight * 100
}
}
// 不能取到末尾的边界点
@ -286,7 +286,7 @@ func getConfigNodeByPriority(cg ConfigGroup) *ConfigNode {
min := 0
max := 0
for i := 0; i < len(cg); i++ {
max = min + cg[i].Priority*100
max = min + cg[i].Weight*100
//fmt.Printf("r: %d, min: %d, max: %d\n", r, min, max)
if r >= min && r < max {
return &cg[i]

View File

@ -33,8 +33,8 @@ type ConfigNode struct {
Name string // 数据库名称
Type string // 数据库类型mysql, sqlite, mssql, pgsql, oracle
Role string // (可选默认为master)数据库的角色用于主从操作分离至少需要有一个master参数值master, slave
Weight int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
Charset string // (可选,默认为 utf8)编码,默认为 utf8
Priority int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
LinkInfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能)
MaxIdleConnCount int // (可选)连接池最大限制的连接数
MaxOpenConnCount int // (可选)连接池最大打开的连接数
@ -48,37 +48,6 @@ var configs struct {
defaultGroup string // 默认数据库分组名称
}
// 数据库集群配置示例,支持主从处理,多数据库集群支持
/*
var DatabaseConfiguration = Config {
// 数据库集群配置名称
"default" : ConfigGroup {
{
Host : "192.168.1.100",
Port : "3306",
User : "root",
Pass : "123456",
Name : "test",
Type : "mysql",
Role : "master",
Charset : "utf8",
Priority : 100,
},
{
Host : "192.168.1.101",
Port : "3306",
User : "root",
Pass : "123456",
Name : "test",
Type : "mysql",
Role : "slave",
Charset : "utf8",
Priority : 100,
},
},
}
*/
// 包初始化
func init() {
configs.config = make(Config)

View File

@ -32,15 +32,15 @@ var (
// 测试前需要修改连接参数。
func init() {
node := gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "",
Name: "",
Type: "mysql",
Role: "master",
Charset: "utf8",
Priority: 1,
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "",
Name: "",
Type: "mysql",
Role: "master",
Charset: "utf8",
Weight: 1,
}
hostname, _ := os.Hostname()
// 本地测试hack

View File

@ -9,6 +9,8 @@ package gins
import (
"fmt"
"time"
"github.com/gogf/gf/g/container/gmap"
"github.com/gogf/gf/g/database/gdb"
"github.com/gogf/gf/g/database/gredis"
@ -19,7 +21,6 @@ import (
"github.com/gogf/gf/g/text/gregex"
"github.com/gogf/gf/g/text/gstr"
"github.com/gogf/gf/g/util/gconv"
"time"
)
const (
@ -117,8 +118,8 @@ func Database(name ...string) gdb.DB {
if value, ok := nodeMap["charset"]; ok {
node.Charset = gconv.String(value)
}
if value, ok := nodeMap["priority"]; ok {
node.Priority = gconv.Int(value)
if value, ok := nodeMap["weight"]; ok {
node.Weight = gconv.Int(value)
}
// Deprecated
if value, ok := nodeMap["linkinfo"]; ok {

View File

@ -8,11 +8,12 @@ package gins_test
import (
"fmt"
"testing"
"time"
"github.com/gogf/gf/g/frame/gins"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func Test_Database(t *testing.T) {
@ -31,8 +32,8 @@ test = "v=2"
name = "test"
type = "mysql"
role = "master"
weight = "1"
charset = "utf8"
priority = "1"
[[database.test]]
host = "127.0.0.1"
port = "3306"
@ -42,8 +43,8 @@ test = "v=2"
name = "test"
type = "mysql"
role = "master"
weight = "1"
charset = "utf8"
priority = "1"
# Redis数据库配置
[redis]
default = "127.0.0.1:6379,0"

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"time"
//_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/database/gdb"
@ -49,7 +50,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.2",
@ -59,7 +60,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.3",
@ -69,7 +70,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.4",
@ -79,7 +80,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// },
//})

View File

@ -9,5 +9,5 @@
name = "test"
type = "mysql"
role = "master"
weight = "1"
charset = "utf8"
priority = "1"

View File

@ -2,9 +2,10 @@ package main
import (
"fmt"
"time"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/database/gdb"
"time"
)
// 本文件用于gf框架的mysql数据库操作示例不作为单元测试使用
@ -48,7 +49,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.2",
@ -58,7 +59,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.3",
@ -68,7 +69,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.4",
@ -78,7 +79,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// },
//})

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"time"
//_ "github.com/mattn/go-oci8"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/database/gdb"
@ -48,7 +49,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.2",
@ -58,7 +59,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.3",
@ -68,7 +69,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// gdb.ConfigNode {
// Host : "127.0.0.4",
@ -78,7 +79,7 @@ func init() {
// Name : "test",
// Type : "mysql",
// Role : "master",
// Priority : 100,
// Weight : 100,
// },
// },
//})