client

package
v0.0.0-...-fa5d5e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2022 License: 0BSD Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//rpc透传时,map中的field名称.(放到const中主要是为了避免滥用冲突)
	ReqMetaDataKey_FIELD_srcName   = "srcName"   //传递rpc请求源的名称
	ReqMetaDataKey_FIELD_srcSvid   = "srcSvid"   //传递rpc请求源的svrid
	ReqMetaDataKey_FIELD_clientSeq = "clientSeq" //传递客户端请求序列号,rpc服务在必要时可以快速响应该seq的请求.(比如 game服快速响应操作)
	ReqMetaDataKey_FIELD_clientUid = "clientUid" //rpc调用时,传递客户端的这个socket连接对应的uid,便于业务做安全性检测
	ReqMetaDataKey_FIELD_dstSvid   = "dstSvid"   //rpcClient中记录下目标svrid,以便于选择合适的服务端
	ReqMetaDataKey_FIELD_remoteIp  = "remoteIp"  //请求方的ip地址;access转入登录的时候会用到。
)

rpc协议中,透传map中的自定义字段名称.

Variables

View Source
var (
	MyClients          = &Clients{mp: make(map[string]client.XClient), mpcb: make(map[string]RouteCallback)}
	MyClientsWithBytes = &Clients{mp: make(map[string]client.XClient), mpcb: make(map[string]RouteCallback)}
	BasePath           string

	FailMode   = client.Failfast
	SelectMode = client.RandomSelect

	//自定义配置,默认编码方式修改为PB,不使用maspack(因为:使用msgpack的时候,pb中修改了字段的话,用到了该结构体的多出服务都需要重启,否则会报如下错误)
	//2020/07/16 17:18:50 server.go:414: WARN : rpcx: failed to handle request: RPC获取userInfo出错! LoginService Login
	NormalOption = client.Option{
		Retries:        3,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.ProtoBuffer,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}

	//自定义配置,核心:SerializeType,透传的时候使用
	MyOptions = client.Option{
		Retries:        3,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.SerializeNone,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}
)

Functions

func AddValueToCTX

func AddValueToCTX(ctx context.Context, key, data string) context.Context

func GetClientUidByCTX

func GetClientUidByCTX(ctx context.Context) (int32, error)

从context中获取本次rpc来源的uid

func GetDstSvidByCTX

func GetDstSvidByCTX(ctx context.Context) (int32, error)

获取context透传下来的dstSvid 发起指定了svrid的调用时,会用到该方法。

func GetRemoteIpByCTX

func GetRemoteIpByCTX(ctx context.Context) (string, error)

从context中获取本次rpc来源的ip地址

func GetValueFromCTX

func GetValueFromCTX(ctx context.Context, key string) (string, bool)

func NewContextWithRemoteIp

func NewContextWithRemoteIp(ctx context.Context, remoteIp string) context.Context

把ip地址透传到下一个调用中去。

func NewContextWithSeq

func NewContextWithSeq(ctx context.Context, seq int32) context.Context

把seq也透传到下一个调用中去。

func NewContextWithUid

func NewContextWithUid(ctx context.Context, uid int64) context.Context

把uid也透传到下一个调用中去。

func XCall

func XCall(ctx context.Context, serviceName string, funcName string, args TheMarshaler, reply interface{}) error

封装好发起rpc调用的客户端!

func XCallBytes

func XCallBytes(ctx context.Context, serviceName string, funcName string, args interface{}, reply interface{}, cb RouteCallback) error

封装好发起rpc调用的客户端!(args和reply都是字节切片的格式),以插件的形式实现!

func XCallBytesWithSvid

func XCallBytesWithSvid(ctx context.Context, serviceName string, funcName string, args interface{}, reply interface{}, dstSvid int32) error

封装好发起rpc调用的客户端,且指定目标svrid!(args和reply都是字节切片的格式),以插件的形式实现!

func XCallWithRoutefunc

func XCallWithRoutefunc(ctx context.Context, serviceName string, funcName string, args TheMarshaler, reply interface{}, cb RouteCallback) error

func XCallWithSvid

func XCallWithSvid(ctx context.Context, serviceName string, funcName string, args TheMarshaler, reply interface{}, dstSvid int32) error

根据指定的svid发起rpc调用

Types

type Clients

type Clients struct {
	// contains filtered or unexported fields
}

封装了rpc客户端map的结构体

type MySelector

type MySelector struct {
	Dofunc RouteCallback
	// contains filtered or unexported fields
}

func (*MySelector) Select

func (s *MySelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string

func (*MySelector) UpdateServer

func (s *MySelector) UpdateServer(servers map[string]string)

type RouteCallback

type RouteCallback func(ms map[string]string, args interface{}) []string

type TheMarshaler

type TheMarshaler interface {
	Marshal() ([]byte, error)
}

传入的请求参数必须是实现了marshal的接口 修改说明:在改之前调用XCall时的args参数类型是interface{},此处添加TheMarshaler之后,就确保了传入的参数必须是实现了Marshal方法的结构体指针。避免了传入结构体的运行时错误。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL