Skip to content

v1.3.6

Choose a tag to compare

@gongzili456 gongzili456 released this 26 Sep 02:24
· 16 commits to master since this release

脚手架更新,ripple generate pb.go时,新增plugin功能
rpcx client Call调用前后的Plugin, 用来统一处理rpc调用前和调用后的逻辑

// Player is a client wrapped XClient.
type PlayerClient struct {
	XClientPool *client.XClientPool
	Plugins     client.PluginContainer
}

type PlayerClientOption struct {
	Plugins client.PluginContainer
}

// WithPluginsForPlayerClient 设置插件
func WithPluginsForPlayerClient(plugins client.PluginContainer) func(*PlayerClientOption) {
	return func(opt *PlayerClientOption) {
		opt.Plugins = plugins
	}
}

// NewPlayerClient wraps a XClient as PlayerClient.
// You can pass a shared XClient object created by NewXClientForPlayer.
func NewPlayerClient(options ...func(*PlayerClientOption)) *PlayerClient {
	pool, err := newXClientPoolForPlayer()
	if err != nil {
		fmt.Println(fmt.Sprintf("Create rpcx client err: playerserver", err.Error()))
		return &PlayerClient{}
	}
	opt := &PlayerClientOption{}
	for _, option := range options {
		option(opt)
	}
	return &PlayerClient{XClientPool: pool, Plugins: opt.Plugins}
}

// GetPlayerInfo is client rpc method as defined
func (c *PlayerClient) GetPlayerInfo(ctx context.Context, req *GetPlayerInfoReq) (reply *GetPlayerInfoResponse, err error) {
	reply = &GetPlayerInfoResponse{}

	if c.XClientPool == nil {
		return nil, errors.New("rpcx client pool is nil")
	}

	xcli := c.XClientPool.Get()

	c.Plugins.DoPreCall(ctx, "PlayerRpc", "GetPlayerInfo", req)
	err = xcli.Call(ctx, "GetPlayerInfo", req, reply)
	c.Plugins.DoPostCall(ctx, "PlayerRpc", "GetPlayerInfo", req, reply, err)
	return reply, err
}