You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
6 years ago
|
package tidb
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
6 years ago
|
"github.com/bilibili/kratos/pkg/conf/env"
|
||
|
"github.com/bilibili/kratos/pkg/log"
|
||
|
"github.com/bilibili/kratos/pkg/naming"
|
||
|
"github.com/bilibili/kratos/pkg/naming/discovery"
|
||
6 years ago
|
)
|
||
|
|
||
|
var _schema = "tidb://"
|
||
|
|
||
|
func (db *DB) nodeList() (nodes []string) {
|
||
|
var (
|
||
|
insInfo *naming.InstancesInfo
|
||
6 years ago
|
insMap map[string][]*naming.Instance
|
||
|
ins []*naming.Instance
|
||
|
ok bool
|
||
6 years ago
|
)
|
||
|
if insInfo, ok = db.dis.Fetch(context.Background()); !ok {
|
||
|
return
|
||
|
}
|
||
|
insMap = insInfo.Instances
|
||
|
if ins, ok = insMap[env.Zone]; !ok || len(ins) == 0 {
|
||
|
return
|
||
|
}
|
||
|
for _, in := range ins {
|
||
|
for _, addr := range in.Addrs {
|
||
|
if strings.HasPrefix(addr, _schema) {
|
||
|
addr = strings.Replace(addr, _schema, "", -1)
|
||
|
nodes = append(nodes, addr)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
log.Info("tidb get %s instances(%v)", db.appid, nodes)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (db *DB) disc() (nodes []string) {
|
||
|
db.dis = discovery.Build(db.appid)
|
||
|
e := db.dis.Watch()
|
||
|
select {
|
||
|
case <-e:
|
||
|
nodes = db.nodeList()
|
||
|
case <-time.After(10 * time.Second):
|
||
|
panic("tidb init discovery err")
|
||
|
}
|
||
|
if len(nodes) == 0 {
|
||
|
panic(fmt.Sprintf("tidb %s no instance", db.appid))
|
||
|
}
|
||
|
go db.nodeproc(e)
|
||
|
log.Info("init tidb discvoery info successfully")
|
||
|
return
|
||
|
}
|