gateway

package module
v0.0.0-...-c2dbefc Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WsService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "gateway.WsService",
	HandlerType: (*WsServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "ExistSession",
			Handler:    _WsService_ExistSession_Handler,
		},
		{
			MethodName: "SendMessage",
			Handler:    _WsService_SendMessage_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "ws.proto",
}

WsService_ServiceDesc is the grpc.ServiceDesc for WsService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func Debugf

func Debugf(format string, args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func ForwardLog

func ForwardLog(fp *ForwardLogParam)

func Infof

func Infof(format string, args ...interface{})

func RegisterWsServiceServer

func RegisterWsServiceServer(s grpc.ServiceRegistrar, srv WsServiceServer)

func Warningf

func Warningf(format string, args ...interface{})

Types

type Context

type Context struct {
	Writer  http.ResponseWriter
	Request *http.Request

	Param       map[string]string //传递参数
	IsWebsocket bool              //是否是ws请求
	ServiceId   string            //业务ID
	RemoteIp    string            //远程请求IP
	FullPath    string            //路径地址

	ServerType    string              //服务类型
	InstanceId    register.InstanceId //实例id
	ServerBase    *register.ServerBase
	ProxyInstance *ProxyInstance

	GrpcCtx      context.Context //rpc接口上下文
	MD           metadata.MD     //grpc 头部数据
	ServerStream grpc.ServerStream
	Conn         *grpc.ClientConn
	Error        error
	// contains filtered or unexported fields
}

代理上下文

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AbortErr

func (c *Context) AbortErr(err error)

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

func (*Context) AbortWithStatusJSON

func (c *Context) AbortWithStatusJSON(code int, jsonObj any)

func (*Context) Next

func (c *Context) Next()

func (*Context) Pass

func (c *Context) Pass()

type Empty

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

func (*Empty) Descriptor deprecated

func (*Empty) Descriptor() ([]byte, []int)

Deprecated: Use Empty.ProtoReflect.Descriptor instead.

func (*Empty) ProtoMessage

func (*Empty) ProtoMessage()

func (*Empty) ProtoReflect

func (x *Empty) ProtoReflect() protoreflect.Message

func (*Empty) Reset

func (x *Empty) Reset()

func (*Empty) String

func (x *Empty) String() string

type ForwardLogParam

type ForwardLogParam struct {
	Protocol     string //协议类型
	InstanceId   string //实例Id
	RemoteIp     string //远程ip
	RequestTime  int64  //响应时间
	ResponseTime int64
	RequestPath  string //请求地址
	// contains filtered or unexported fields
}

type HandlerFunc

type HandlerFunc func(*Context)

中间件函数

type HandlersChain

type HandlersChain []HandlerFunc

type HttpProxyInstance

type HttpProxyInstance struct {
	ReverseProxy *httputil.ReverseProxy //http服务反向代理
	Addr         *url.URL               //服务url
}

http代理实例

type Message

type Message struct {
	ServiceId string `protobuf:"bytes,1,opt,name=serviceId,proto3" json:"serviceId,omitempty"`
	SessionId string `protobuf:"bytes,2,opt,name=sessionId,proto3" json:"sessionId,omitempty"`
	UserId    string `protobuf:"bytes,3,opt,name=userId,proto3" json:"userId,omitempty"`
	Message   []byte `protobuf:"bytes,4,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

func (*Message) Descriptor deprecated

func (*Message) Descriptor() ([]byte, []int)

Deprecated: Use Message.ProtoReflect.Descriptor instead.

func (*Message) GetMessage

func (x *Message) GetMessage() []byte

func (*Message) GetServiceId

func (x *Message) GetServiceId() string

func (*Message) GetSessionId

func (x *Message) GetSessionId() string

func (*Message) GetUserId

func (x *Message) GetUserId() string

func (*Message) ProtoMessage

func (*Message) ProtoMessage()

func (*Message) ProtoReflect

func (x *Message) ProtoReflect() protoreflect.Message

func (*Message) Reset

func (x *Message) Reset()

func (*Message) String

func (x *Message) String() string

type MessageReq

type MessageReq struct {
	ServiceId  string `protobuf:"bytes,1,opt,name=serviceId,proto3" json:"serviceId,omitempty"`
	SessionId  string `protobuf:"bytes,2,opt,name=sessionId,proto3" json:"sessionId,omitempty"`
	UserId     string `protobuf:"bytes,3,opt,name=userId,proto3" json:"userId,omitempty"`
	Message    []byte `protobuf:"bytes,4,opt,name=message,proto3" json:"message,omitempty"`
	MethodName string `protobuf:"bytes,5,opt,name=methodName,proto3" json:"methodName,omitempty"`
	// contains filtered or unexported fields
}

func (*MessageReq) Descriptor deprecated

func (*MessageReq) Descriptor() ([]byte, []int)

Deprecated: Use MessageReq.ProtoReflect.Descriptor instead.

func (*MessageReq) GetMessage

func (x *MessageReq) GetMessage() []byte

func (*MessageReq) GetMethodName

func (x *MessageReq) GetMethodName() string

func (*MessageReq) GetServiceId

func (x *MessageReq) GetServiceId() string

func (*MessageReq) GetSessionId

func (x *MessageReq) GetSessionId() string

func (*MessageReq) GetUserId

func (x *MessageReq) GetUserId() string

func (*MessageReq) ProtoMessage

func (*MessageReq) ProtoMessage()

func (*MessageReq) ProtoReflect

func (x *MessageReq) ProtoReflect() protoreflect.Message

func (*MessageReq) Reset

func (x *MessageReq) Reset()

func (*MessageReq) String

func (x *MessageReq) String() string

type Options

type Options struct {
	RedisOption       *redis.Options //redis配置项
	GlobalTime        int            //注册中心全局健康检查间隔
	MemberPort        int            //注册中心集群本地实例端口号
	RequestFailResult string         //异常返回
	LogPath           string         //日志地址
}

type Proxy

type Proxy struct {
	RegisterCenter *register.RegisterCenter //服务注册中心

	UnimplementedWsServiceServer
	// contains filtered or unexported fields
}

网关实例

func NewGateway

func NewGateway(ctx context.Context, option *Options) (*Proxy, error)

NewGateway

@Description: 新建网关实例
@param ctx
@param option
@return *GatewayInstance
@return error

func (*Proxy) AddServerInstance

func (s *Proxy) AddServerInstance(id register.InstanceId, sb *register.ServerBase)

AddServerInstance

@Description: 添加实例事件
@receiver s
@param id
@param sb

func (*Proxy) ClearServerInstance

func (s *Proxy) ClearServerInstance(id register.InstanceId, sb *register.ServerBase)

ClearServerInstance

@Description: 清空服务实例事件
@receiver s
@param id
@param sb

func (*Proxy) DeleteServerInstance

func (s *Proxy) DeleteServerInstance(id register.InstanceId, sb *register.ServerBase)

DeleteServerInstance

@Description: 移除实例事件
@receiver s
@param id
@param sb

func (*Proxy) ExistSession

func (s *Proxy) ExistSession(ctx context.Context, req *SessionReq) (*SessionRes, error)

ExistSession

@Description: ws会话是否存在
@receiver s
@param ctx
@param req
@return *SessionRes
@return error

func (*Proxy) GrpcMiddleware

func (s *Proxy) GrpcMiddleware(middlewares ...HandlerFunc)

GrpcMiddleware

@Description: grpc中间件
@receiver s
@param middlewares

func (*Proxy) GrpcProxy

func (s *Proxy) GrpcProxy(_ interface{}, serverStream grpc.ServerStream) error

GrpcProxy

@Description: grpc请求代理
@receiver s
@param _
@param serverStream
@return error

func (*Proxy) HttpExistSession

func (s *Proxy) HttpExistSession(w http.ResponseWriter, r *http.Request)

HttpExistSession

@Description: 会话是否存在 http接口
@receiver s
@param w
@param r

func (*Proxy) HttpMiddleware

func (s *Proxy) HttpMiddleware(middlewares ...HandlerFunc)

HttpMiddleware

@Description: http中间件
@receiver s
@param middlewares

func (*Proxy) HttpProxy

func (s *Proxy) HttpProxy(w http.ResponseWriter, r *http.Request)

HttpProxy

@Description: http请求代理
@receiver s
@param w
@param r

func (*Proxy) HttpSendMessage

func (s *Proxy) HttpSendMessage(w http.ResponseWriter, r *http.Request)

HttpSendMessage

@Description: 发送会话消息 http接口
@receiver s
@param w
@param r

func (*Proxy) LoadServerBase

func (s *Proxy) LoadServerBase(serviceId string, recursionNum int) (*ProxyInstance, *register.ServerBase, error)

LoadServerBase

@Description: 获取一个注册实例,通过轮训机制
@receiver s
@param serviceId 业务ID
@param recursionNum 递归次数
@return *register.ServerBase
@return error

func (*Proxy) RunGrpcServer

func (s *Proxy) RunGrpcServer(lis net.Listener)

RunGrpcServer

@Description: 启动grpc服务端
@receiver s
@param lis

func (*Proxy) SendMessage

func (s *Proxy) SendMessage(ctx context.Context, req *MessageReq) (*Empty, error)

SendMessage

@Description: ws发送消息
@receiver s
@param ctx
@param req
@return *Empty
@return error

type ProxyInstance

type ProxyInstance struct {
	HttpTable     map[register.InstanceId]*HttpProxyInstance //http代理实例表
	HttpTableLock sync.RWMutex                               //http代理表读写锁
	GrpcTable     map[register.InstanceId]*grpc.ClientConn
	GrpcTableLock sync.RWMutex
	AtomicNum     uint64 //请求计数
	LoadType      int    //负载类型 0 轮训 1 故障转移
}

代理实例

type SessionReq

type SessionReq struct {
	ServiceId string `protobuf:"bytes,1,opt,name=serviceId,proto3" json:"serviceId,omitempty"`
	SessionId string `protobuf:"bytes,2,opt,name=sessionId,proto3" json:"sessionId,omitempty"`
	UserId    string `protobuf:"bytes,3,opt,name=userId,proto3" json:"userId,omitempty"`
	// contains filtered or unexported fields
}

func (*SessionReq) Descriptor deprecated

func (*SessionReq) Descriptor() ([]byte, []int)

Deprecated: Use SessionReq.ProtoReflect.Descriptor instead.

func (*SessionReq) GetServiceId

func (x *SessionReq) GetServiceId() string

func (*SessionReq) GetSessionId

func (x *SessionReq) GetSessionId() string

func (*SessionReq) GetUserId

func (x *SessionReq) GetUserId() string

func (*SessionReq) ProtoMessage

func (*SessionReq) ProtoMessage()

func (*SessionReq) ProtoReflect

func (x *SessionReq) ProtoReflect() protoreflect.Message

func (*SessionReq) Reset

func (x *SessionReq) Reset()

func (*SessionReq) String

func (x *SessionReq) String() string

type SessionRes

type SessionRes struct {
	Exist bool `protobuf:"varint,1,opt,name=exist,proto3" json:"exist,omitempty"`
	// contains filtered or unexported fields
}

func (*SessionRes) Descriptor deprecated

func (*SessionRes) Descriptor() ([]byte, []int)

Deprecated: Use SessionRes.ProtoReflect.Descriptor instead.

func (*SessionRes) GetExist

func (x *SessionRes) GetExist() bool

func (*SessionRes) ProtoMessage

func (*SessionRes) ProtoMessage()

func (*SessionRes) ProtoReflect

func (x *SessionRes) ProtoReflect() protoreflect.Message

func (*SessionRes) Reset

func (x *SessionRes) Reset()

func (*SessionRes) String

func (x *SessionRes) String() string

type UnimplementedWsServiceServer

type UnimplementedWsServiceServer struct {
}

UnimplementedWsServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedWsServiceServer) ExistSession

func (UnimplementedWsServiceServer) SendMessage

type UnsafeWsServiceServer

type UnsafeWsServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeWsServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to WsServiceServer will result in compilation errors.

type WsServiceClient

type WsServiceClient interface {
	//  会话是否存在
	ExistSession(ctx context.Context, in *SessionReq, opts ...grpc.CallOption) (*SessionRes, error)
	//  发送消息
	SendMessage(ctx context.Context, in *MessageReq, opts ...grpc.CallOption) (*Empty, error)
}

WsServiceClient is the client API for WsService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

func NewWsServiceClient

func NewWsServiceClient(cc grpc.ClientConnInterface) WsServiceClient

type WsServiceServer

type WsServiceServer interface {
	//  会话是否存在
	ExistSession(context.Context, *SessionReq) (*SessionRes, error)
	//  发送消息
	SendMessage(context.Context, *MessageReq) (*Empty, error)
	// contains filtered or unexported methods
}

WsServiceServer is the server API for WsService service. All implementations must embed UnimplementedWsServiceServer for forward compatibility

Jump to

Keyboard shortcuts

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