zrpc

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

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

Go to latest
Published: Nov 30, 2022 License: MIT Imports: 17 Imported by: 0

README

zrpc

学习用途的简易的rpc框架

Documentation

Overview

server

Index

Constants

This section is empty.

Variables

View Source
var DefaultOption = &Option{
	CodecType:      codec.GobType,
	ConnectTimeout: time.Second * 10,
}

DefaultOption 默认option

View Source
var DefaultServer = NewServer()

DefaultServer 默认服务器对象

View Source
var ErrShutdown = errors.New("connection is shut down")

Functions

func Accept

func Accept(lis net.Listener)

func HandleHTTP

func HandleHTTP()

HandleHTTP is a convenient approach for default server to register HTTP handlers

func Register

func Register(rcvr interface{}) error

Register 快捷注册方法,创建默认服务器注册服务

Types

type Call

type Call struct {
	Seq           uint64      // 请求ID
	ServiceMethod string      // 格式 "<service>.<method>"
	Args          interface{} // 参数
	Reply         interface{} // 回复
	Error         error       // 发送的错误
	Done          chan *Call  // 调用完成时写值
}

Call 代表一次rpc调用的信息

type Client

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

Client rpc客户端,主要完成发送请求和接收返回结果

func Dial

func Dial(network, address string, opts ...*Option) (client *Client, err error)

Dial 连接RPC服务器

func DialHTTP

func DialHTTP(network, address string, opts ...*Option) (*Client, error)

DialHTTP connects to an HTTP RPC server at the specified network address listening on the default HTTP RPC path.

func NewClient

func NewClient(conn net.Conn, opt *Option) (*Client, error)

NewClient 创建Clint实例

func NewHTTPClient

func NewHTTPClient(conn net.Conn, opt *Option) (*Client, error)

NewHTTPClient new a Client instance via HTTP as transport protocol

func XDial

func XDial(rpcAddr string, opts ...*Option) (*Client, error)

XDial calls different functions to connect to a RPC server according the first parameter rpcAddr. rpcAddr is a general format (protocol@addr) to represent a rpc server eg, http@10.0.0.1:7001, tcp@10.0.0.1:9999, unix@/tmp/geerpc.sock

func (*Client) Call

func (client *Client) Call(ctx context.Context, serviceMethod string, args, reply interface{}) error

Call 发起rpc调用,等待完成

func (*Client) Close

func (client *Client) Close() error

Close 关闭连接

func (*Client) Go

func (client *Client) Go(serviceMethod string, args, reply interface{}, done chan *Call) *Call

Go 异步调用接口,返回调用对象

func (*Client) IsAvailable

func (client *Client) IsAvailable() bool

IsAvailable 客户端工作正常返回true

type Option

type Option struct {
	CodecType      codec.Type // 数据编码类型
	ConnectTimeout time.Duration
	HandleTimeout  time.Duration
}

Option 连接服务器的协商字段 连接服务器后,服务器读取出option的内容,根据option的内容处理后续的数据 | Option | Header1 | Body1 | Header2 | Body2 | ... 如上,一次tcp连接,Option协商数据内容信息,一对heade、body代表一次tpc方法调用,可以不断的发送rpc方法调用

type Server

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

Server 逻辑实现

func NewServer

func NewServer() *Server

func (*Server) Accept

func (server *Server) Accept(lis net.Listener)

Accept 接受连接,然后交给ServeConn处理

func (*Server) HandleHTTP

func (server *Server) HandleHTTP()

HandleHTTP registers an HTTP handler for RPC messages on rpcPath. It is still necessary to invoke http.Serve(), typically in a go statement.

func (*Server) Register

func (server *Server) Register(rcvr interface{}) error

Register 注册服务

func (*Server) ServeConn

func (server *Server) ServeConn(conn io.ReadWriteCloser)

ServeConn 处理连接

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP 实现 http.Handler 接口, 处理RPC请求.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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