package rpcx import ( "context" "errors" "strings" "github.com/smallnest/rpcx/client" "github.com/smallnest/rpcx/share" ) func newClient(rpcx *RPCX) (c *Client) { c = &Client{ rpcx: rpcx, clients: make(map[string]client.XClient), // msgChan: make(chan *protocol.Message, 1000), } return } type Client struct { rpcx *RPCX clients map[string]client.XClient // msgChan chan *protocol.Message // 接收rpcXServer推送消息 } // DoMessage 服务端消息处理 func (this *Client) DoMessage() { // for msg := range this.msgChan { // } } func (this *Client) Start() (err error) { return } func (this *Client) Stop() (err error) { for _, v := range this.clients { v.Close() } return } //同步调用 func (this *Client) Call(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { if servicePath == "" { err = errors.New("servicePath no cant null") return } var ( spath []string d *client.ConsulDiscovery c client.XClient ok bool ) spath = strings.Split(servicePath, "/") if c, ok = this.clients[spath[0]]; !ok { if d, err = client.NewConsulDiscovery(this.rpcx.options.ServiceTag, spath[0], this.rpcx.options.ConsulServers, nil); err != nil { return } c = client.NewXClient(spath[0], client.Failfast, client.RandomSelect, d, client.DefaultOption) c.SetSelector(newSelector()) this.clients[spath[0]] = c } ctx = context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{ CallRoutRulesKey: servicePath, ServiceAddrKey: "tcp@" + this.rpcx.options.ServiceAddr, ServiceMetaKey: this.rpcx.metadata, }) err = c.Call(ctx, serviceMethod, args, reply) return } //异步调用 func (this *Client) Go(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (call *client.Call, err error) { // return this.xclient.Go(ctx, string(serviceMethod), args, reply, done) if servicePath == "" { err = errors.New("servicePath no cant null") return } var ( spath []string d *client.ConsulDiscovery c client.XClient ok bool ) spath = strings.Split(servicePath, "/") if c, ok = this.clients[spath[0]]; !ok { if d, err = client.NewConsulDiscovery(this.rpcx.options.ServiceTag, spath[0], this.rpcx.options.ConsulServers, nil); err != nil { return } c = client.NewXClient(spath[0], client.Failfast, client.RandomSelect, d, client.DefaultOption) c.SetSelector(newSelector()) this.clients[spath[0]] = c } ctx = context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{ CallRoutRulesKey: servicePath, ServiceAddrKey: "tcp@" + this.rpcx.options.ServiceAddr, ServiceMetaKey: this.rpcx.metadata, }) return c.Go(ctx, string(serviceMethod), args, reply, done) }