func init() { config := map[string]string{ "Uri": "121.199.44.61:3307", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", } conn = rpc.NewRpcClient("tcp", config) }
func Test_RpcClient(t *testing.T) { config := map[string]string{ "Uri": "127.0.0.1:9901", "User": "******", "Password": "******", "Signature": "123", } conn := rpc.NewRpcClient("udp", config) fmt.Printf("%#v", conn) }
func Test_RemoteCallStatus(t *testing.T) { config := map[string]string{ "Uri": "127.0.0.1:9901", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", } conn := rpc.NewRpcClient("udp", config) data, _ := conn.Call("host", "status", nil) panic(fmt.Sprintf("%#v", data)) }
func main() { t1 := time.Now().UnixNano() config := map[string]string{ "Uri": "121.199.44.61:3307", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", } conn := rpc.NewRpcClient("tcp", config) data, _ := conn.Call("proxy", "exec", []interface{}{[]string{"CREATE TABLE IF NOT EXISTS `album` (`album_id` INT NOT NULL AUTO_INCREMENT primary key, `cover_pictureid` int not null default 0, `album_name` char(60) NOT NULL default '', `album_desc` char(200) NOT NULL default '', `created_time` int not null, `creator` int(11) not null comment '创建者id', `status` tinyint(1) not null default 0, `picture_total` int not null default 0, `is_allow_comment` tinyint(1) not null default 0, UNIQUE INDEX `name_UNIQUE` (`album_name` ASC) )"}}) t2 := time.Now().UnixNano() fmt.Printf("data: %#v\n time: %4.3f s\n", data, float64(t2-t1)/1000000000) }
func Test_ErrorRpcClient(t *testing.T) { defer func() { if err := recover(); err != nil { if fmt.Sprintf("%s", err) != "Please config the value for key: Signature" { t.Error("error type wrong!") } } }() config := map[string]string{ "Uri": "127.0.0.1:9901", "User": "******", "Password": "******", } conn := rpc.NewRpcClient("udp", config) fmt.Printf("%#v", conn) }
// To get best host by type // // type support: // a: 'master' or 'write' // b: 'slave' or 'read' // func (c *Client) GetBestHost(hosts []*host.Host, hType string) *host.Host { hLen := len(hosts) if hLen == 0 { return nil } if hLen == 1 { return hosts[0] } var bestHost *host.Host runChan := make(chan *chanPing, runtime.NumCPU()) for i := 0; i < hLen; i++ { go func(h *host.Host) { conn := rpc.NewRpcClient("udp", h.GetConfig("9901")) data, err := conn.Call("host", "pingto", []interface{}{c.IP}) if err != nil { runChan <- &chanPing{Host: h, Ping: nil} } runChan <- &chanPing{Host: h, Ping: data.Data} }(hosts[i]) } bestLevel := int64(-999999) for i := 0; i < hLen; i++ { pingCh := <-runChan h := pingCh.Host data := pingCh.Ping if res, isOk := data.(float64); isOk { curLevel := h.GetHostEvaluateLevel(res, hType) if curLevel > bestLevel { bestLevel = curLevel bestHost = h } } } close(runChan) return bestHost }
func Run() { configs := []map[string]string{{ "Uri": "10.161.185.137:8999", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", }, { "Uri": "127.0.0.1:8999", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", }, } for i, config := range configs { conn := rpc.NewRpcClient("udp", config) data := conn.Call("host", "status", nil) logger.Add("server %d: %s", i, data) } }
func Test_RemoteCall(t *testing.T) { config := map[string]string{ "Uri": "127.0.0.1:9901", "User": "******", "Password": "******", "Signature": "ab1f8e61026a7456289c550cb0cf77cda44302b4", } conn := rpc.NewRpcClient("udp", config) data, _ := conn.Call("host", "ping", nil) panic(fmt.Sprintf("%#v", data)) isOk := false switch data.Data.(type) { case string: if data.Data.(string) == "Pong" { isOk = true } } if !isOk { t.Error("test call failed!") } }