Documentation ¶
Index ¶
- Constants
- Variables
- func HandleHTTP()
- func Serve(lis net.Listener) error
- func ServeCodec(codec ServerCodec)
- func ServeConn(conn io.ReadWriteCloser)
- func ServeRequest(codec ServerCodec) error
- func Shutdown(ctx context.Context) error
- type API
- type Call
- type Client
- func Dial(network, address string, codecFunc ...func(io.ReadWriteCloser) ClientCodec) (*Client, error)
- func DialHTTP(network, address string) (*Client, error)
- func DialHTTPPath(network, address, path string, ...) (*Client, error)
- func NewClient(conn io.ReadWriteCloser, codecFunc ...func(io.ReadWriteCloser) ClientCodec) *Client
- func NewClientWithCodec(codec ClientCodec) *Client
- type ClientCodec
- type Codec
- type Context
- type Options
- type RPCPool
- type Request
- type Response
- type Server
- func (server *Server) Close() error
- func (server *Server) HandleHTTP(rpcPath, debugPath string)
- func (server *Server) RegisterAPI(path string, api API)
- func (server *Server) Serve(lis net.Listener) error
- func (server *Server) ServeCodec(codec ServerCodec)
- func (server *Server) ServeConn(conn io.ReadWriteCloser)
- func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (server *Server) ServeRequest(codec ServerCodec) error
- func (server *Server) Shutdown(ctx context.Context) error
- type ServerCodec
- type ServerError
Constants ¶
const ( // DefaultRPCPath used by HandleHTTP DefaultRPCPath = "/_goRPC_" // DefaultDebugPath used by HandleHTTP DefaultDebugPath = "/debug/rpc" )
Variables ¶
var DefaultServer = NewServer()
DefaultServer is the default instance of *Server.
var ErrServerClosed = errors.New("cprpc: Server closed")
ErrServerClosed is returned by the Server's Serve method after a call to Shutdown or Close.
var ErrShutdown = errors.New("connection is shut down")
ErrShutdown connection is closed.
Functions ¶
func HandleHTTP ¶
func HandleHTTP()
HandleHTTP registers an HTTP handler for RPC messages to DefaultServer on DefaultRPCPath and a debugging handler on DefaultDebugPath. It is still necessary to invoke http.Serve(), typically in a go statement.
func Serve ¶
Serve accepts connections on the listener and serves requests to DefaultServer for each incoming connection. Accept blocks; the caller typically invokes it in a go statement.
func ServeCodec ¶
func ServeCodec(codec ServerCodec)
ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.
func ServeConn ¶
func ServeConn(conn io.ReadWriteCloser)
ServeConn runs the DefaultServer on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement. ServeConn uses the gob wire format (see package gob) on the connection. To use an alternate codec, use ServeCodec. See NewClient's comment for information about concurrent access.
func ServeRequest ¶
func ServeRequest(codec ServerCodec) error
ServeRequest is like ServeCodec but synchronously serves a single request. It does not close the codec upon completion.
func Shutdown ¶
Shutdown gracefully shuts down the DefaultServer without interrupting any active connection. Shutdown works by first closing all DefaultServer's open listeners, then waiting indefinitely for connections to transition to an idle state before closing them and shutting down. If the provided context expires before the shutdown is complete, Shutdown forcibly closes active connections and returns the context's error, otherwise it returns any error returned from closing the DefaultServer's underlying Listener(s).
Types ¶
type Call ¶
type Call struct { Path string Args interface{} // The argument to the function (*struct). Reply interface{} // The reply from the function (*struct). Error error // After completion, the error status. Done chan *Call // Strobes when call is complete. }
Call represents an active RPC.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents an RPC Client. There may be multiple outstanding Calls associated with a single Client, and a Client may be used by multiple goroutines simultaneously.
func Dial ¶
func Dial(network, address string, codecFunc ...func(io.ReadWriteCloser) ClientCodec) (*Client, error)
Dial connects to an RPC server at the specified network address.
func DialHTTP ¶
DialHTTP connects to an HTTP RPC server at the specified network address listening on the default HTTP RPC path.
func DialHTTPPath ¶
func DialHTTPPath(network, address, path string, codecFunc ...func(io.ReadWriteCloser) ClientCodec) (*Client, error)
DialHTTPPath connects to an HTTP RPC server at the specified network address and path.
func NewClient ¶
func NewClient(conn io.ReadWriteCloser, codecFunc ...func(io.ReadWriteCloser) ClientCodec) *Client
NewClient returns a new Client to handle requests to the set of services at the other end of the connection. It adds a buffer to the write side of the connection so the header and payload are sent as a unit.
The read and write halves of the connection are serialized independently, so no interlocking is required. However each half may be accessed concurrently so the implementation of conn should protect against concurrent reads or concurrent writes.
func NewClientWithCodec ¶
func NewClientWithCodec(codec ClientCodec) *Client
NewClientWithCodec is like NewClient but uses the specified codec to encode requests and decode responses.
func (*Client) Call ¶
Call invokes the named function, waits for it to complete, and returns its error status.
func (*Client) Close ¶
Close calls the underlying codec's Close method. If the connection is already shutting down, ErrShutdown is returned.
func (*Client) Go ¶
Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.
type ClientCodec ¶
type ClientCodec interface { WriteRequest(*Request, interface{}) error ReadResponseHeader(*Response) error ReadResponseBody(interface{}) error Close() error }
A ClientCodec implements writing of RPC requests and reading of RPC responses for the client side of an RPC session. The client calls WriteRequest to write a request to the connection and calls ReadResponseHeader and ReadResponseBody in pairs to read responses. The client calls Close when finished with the connection. ReadResponseBody may be called with a nil argument to force the body of the response to be read and then discarded. See NewClient's comment for information about concurrent access.
type Codec ¶
type Codec struct { Timeout time.Duration Closer io.ReadWriteCloser Decoder *gob.Decoder Encoder *gob.Encoder EncBuf *bufio.Writer }
Codec ...
func (*Codec) ReadResponseBody ¶
ReadResponseBody ...
func (*Codec) ReadResponseHeader ¶
ReadResponseHeader ...
func (*Codec) WriteRequest ¶
WriteRequest ...
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context including Server, seq, ServerCodec and sync.Mutex.
type Options ¶
type Options struct { // InitTargets init targets InitTargets []string // init connection InitCap int // max connections MaxCap int DialTimeout time.Duration IdleTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration CodecFunc func(io.ReadWriteCloser) ClientCodec // contains filtered or unexported fields }
Options pool options
func NewOptions ¶
func NewOptions() *Options
NewOptions returns a new newOptions instance with sane defaults.
type RPCPool ¶
type RPCPool struct { Mu sync.Mutex IdleTimeout time.Duration // contains filtered or unexported fields }
RPCPool pool info
type Request ¶
type Request struct { Path string Seq uint64 // sequence number chosen by client // contains filtered or unexported fields }
Request is a header written before every RPC call. It is used internally but documented here as an aid to debugging, such as when analyzing network traffic.
type Response ¶
type Response struct { Path string Seq uint64 // echoes that of the request Error string // error, if any. // contains filtered or unexported fields }
Response is a header written before every RPC return. It is used internally but documented here as an aid to debugging, such as when analyzing network traffic.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an RPC Server.
func NewServer ¶
func NewServer(codecFunc ...func(io.ReadWriteCloser) ServerCodec) *Server
NewServer returns a new Server.
func (*Server) Close ¶
Close immediately closes all active net.Listeners and any connections in state StateNew, StateActive, or StateIdle. For a graceful shutdown, use Shutdown.
func (*Server) HandleHTTP ¶
HandleHTTP registers an HTTP handler for RPC messages on rpcPath, and a debugging handler on debugPath. It is still necessary to invoke http.Serve(), typically in a go statement.
func (*Server) RegisterAPI ¶
RegisterAPI bind the path to api.
func (*Server) Serve ¶
Serve accepts connections on the listener and serves requests for each incoming connection. Accept blocks until the listener returns a non-nil error. The caller typically invokes Accept in a go statement.
func (*Server) ServeCodec ¶
func (server *Server) ServeCodec(codec ServerCodec)
ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.
func (*Server) ServeConn ¶
func (server *Server) ServeConn(conn io.ReadWriteCloser)
ServeConn runs the server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement. ServeConn uses the gob wire format (see package gob) on the connection. To use an alternate codec, use ServeCodec. See NewClient's comment for information about concurrent access.
func (*Server) ServeHTTP ¶
func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements an http.Handler that answers RPC requests.
func (*Server) ServeRequest ¶
func (server *Server) ServeRequest(codec ServerCodec) error
ServeRequest is like ServeCodec but synchronously serves a single request. It does not close the codec upon completion.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Server's underlying Listener(s).
type ServerCodec ¶
type ServerCodec interface { ReadRequestHeader(*Request) error ReadRequestBody(interface{}) error WriteResponse(*Response, interface{}) error // Close can be called multiple times and must be idempotent. Close() error GetRwc() io.ReadWriteCloser }
A ServerCodec implements reading of RPC requests and writing of RPC responses for the server side of an RPC session. The server calls ReadRequestHeader and ReadRequestBody in pairs to read requests from the connection, and it calls WriteResponse to write a response back. The server calls Close when finished with the connection. ReadRequestBody may be called with a nil argument to force the body of the request to be read and discarded. See NewClient's comment for information about concurrent access.
type ServerError ¶
type ServerError string
ServerError represents an error that has been returned from the remote side of the RPC connection.
func (ServerError) Error ¶
func (e ServerError) Error() string