Namespace: go.std.net.rpc
v1.0Contents
Summary
Provides a low-level interface to the net/rpc package.
Package rpc provides access to the exported methods of an object across a
network or other I/O connection. A server registers an object, making it visible
as a service with the name of the type of the object. After registration, exported
methods of the object will be accessible remotely. A server may register multiple
objects (services) of different types but it is an error to register multiple
objects of the same type.
Only methods that satisfy these criteria will be made available for remote access;
other methods will be ignored:
- the method's type is exported.
- the method is exported.
- the method has two arguments, both exported (or builtin) types.
- the method's second argument is a pointer.
- the method has return type error.
In effect, the method must look schematically like
func (t *T) MethodName(argType T1, replyType *T2) error
where T1 and T2 can be marshaled by encoding/gob.
These requirements apply even if a different codec is used.
(In the future, these requirements may soften for custom codecs.)
The method's first argument represents the arguments provided by the caller; the
second argument represents the result parameters to be returned to the caller.
The method's return value, if non-nil, is passed back as a string that the client
sees as if created by errors.New. If an error is returned, the reply parameter
will not be sent back to the client.
The server may handle requests on a single connection by calling ServeConn. More
typically it will create a network listener and call Accept or, for an HTTP
listener, HandleHTTP and http.Serve.
A client wishing to use the service establishes a connection and then invokes
NewClient on the connection. The convenience function Dial (DialHTTP) performs
both steps for a raw network connection (an HTTP connection). The resulting
Client object has two methods, Call and Go, that specify the service and method to
call, a pointer containing the arguments, and a pointer to receive the result
parameters.
The Call method waits for the remote call to complete while the Go method
launches the call asynchronously and signals completion using the Call
structure's Done channel.
Unless an explicit codec is set up, package encoding/gob is used to
transport the data.
Here is a simple example. A server wishes to export an object of type Arith:
package server
import "errors"
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
The server calls (for HTTP service):
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
At this point, clients can see a service "Arith" with methods "Arith.Multiply" and
"Arith.Divide". To invoke one, a client first dials the server:
client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
if err != nil {
log.Fatal("dialing:", err)
}
Then it can make a remote call:
// Synchronous call
args := &server.Args{7,8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply)
or
// Asynchronous call
quotient := new(Quotient)
divCall := client.Go("Arith.Divide", args, quotient, nil)
replyCall := <-divCall.Done // will be equal to divCall
// check errors, print, etc.
A server implementation will often provide a simple, type-safe wrapper for the
client.
The net/rpc package is frozen and is not accepting new features.
Index
- *Call
- *Client
- *Request
- *Response
- *Server
- *ServerError
- Accept
- Call
- Client
- ClientCodec
- DefaultDebugPath
- DefaultRPCPath
- DefaultServer
- Dial
- DialHTTP
- DialHTTPPath
- ErrShutdown
- HandleHTTP
- NewClient
- NewClientWithCodec
- NewServer
- Register
- RegisterName
- Request
- Response
- ServeCodec
- ServeConn
- ServeRequest
- Server
- ServerCodec
- ServerError
- arrayOfCall
- arrayOfClient
- arrayOfClientCodec
- arrayOfRequest
- arrayOfResponse
- arrayOfServer
- arrayOfServerCodec
- arrayOfServerError
Legend
-
Constant
Variable
Function
Macro
Special form
Type
GoVar
Receiver/Method
Constants
Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.-
DefaultDebugPath
String v1.0 -
DefaultRPCPath
String v1.0Defaults used by HandleHTTP
Variables
-
DefaultServer
Var v1.0DefaultServer is the default instance of *Server.
-
ErrShutdown
Var v1.0
Functions, Macros, and Special Forms
-
Accept
Function v1.0(Accept lis)
Accept 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.
Go input arguments: (lis net.Listener)
Joker input arguments: [^go.std.net/Listener lis] -
Dial
Function v1.0(Dial network address)
Dial connects to an RPC server at the specified network address.
Go input arguments: (network string, address string)
Go returns: (*Client, error)
Joker input arguments: [^String network, ^String address]
Joker returns: [^*Client, ^Error] -
DialHTTP
Function v1.0(DialHTTP network address)
DialHTTP connects to an HTTP RPC server at the specified network address
listening on the default HTTP RPC path.
Go input arguments: (network string, address string)
Go returns: (*Client, error)
Joker input arguments: [^String network, ^String address]
Joker returns: [^*Client, ^Error] -
DialHTTPPath
Function v1.0(DialHTTPPath network address path)
DialHTTPPath connects to an HTTP RPC server
at the specified network address and path.
Go input arguments: (network string, address string, path string)
Go returns: (*Client, error)
Joker input arguments: [^String network, ^String address, ^String path]
Joker returns: [^*Client, ^Error] -
HandleHTTP
Function v1.0(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.
Joker input arguments: [] -
NewClient
Function v1.0(NewClient conn)
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.
Go input arguments: (conn io.ReadWriteCloser)
Go returns: *Client
Joker input arguments: [^go.std.io/ReadWriteCloser conn]
Joker returns: ^*Client -
NewClientWithCodec
Function v1.0(NewClientWithCodec codec)
NewClientWithCodec is like NewClient but uses the specified
codec to encode requests and decode responses.
Go input arguments: (codec ClientCodec)
Go returns: *Client
Joker input arguments: [^ClientCodec codec]
Joker returns: ^*Client -
NewServer
Function v1.0(NewServer)
NewServer returns a new Server.
Go returns: *Server
Joker input arguments: []
Joker returns: ^*Server -
Register
Function v1.0(Register rcvr)
Register publishes the receiver's methods in the DefaultServer.
Go input arguments: (rcvr any)
Go returns: error
Joker input arguments: [^GoObject rcvr]
Joker returns: ^Error -
RegisterName
Function v1.0(RegisterName name rcvr)
RegisterName is like Register but uses the provided name for the type
instead of the receiver's concrete type.
Go input arguments: (name string, rcvr any)
Go returns: error
Joker input arguments: [^String name, ^GoObject rcvr]
Joker returns: ^Error -
ServeCodec
Function v1.0(ServeCodec codec)
ServeCodec is like ServeConn but uses the specified codec to
decode requests and encode responses.
Go input arguments: (codec ServerCodec)
Joker input arguments: [^ServerCodec codec] -
ServeConn
Function v1.0(ServeConn conn)
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.
Go input arguments: (conn io.ReadWriteCloser)
Joker input arguments: [^go.std.io/ReadWriteCloser conn] -
ServeRequest
Function v1.0(ServeRequest codec)
ServeRequest is like ServeCodec but synchronously serves a single request.
It does not close the codec upon completion.
Go input arguments: (codec ServerCodec)
Go returns: error
Joker input arguments: [^ServerCodec codec]
Joker returns: ^Error
Types
-
*Call
Concrete Type v1.0Call represents an active RPC.
-
*Client
Concrete Type v1.0Client 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.
-
Call
Receiver for *Client v1.0([serviceMethod args reply])
Call invokes the named function, waits for it to complete, and returns its error status.
-
Close
Receiver for *Client v1.0([])
Close calls the underlying codec's Close method. If the connection is already
shutting down, ErrShutdown is returned.
-
Go
Receiver for *Client v1.0([serviceMethod args reply done])
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.
-
*Request
Concrete Type v1.0Request 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.
-
*Response
Concrete Type v1.0Response 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.
-
*Server
Concrete Type v1.0Server represents an RPC Server.
-
Accept
Receiver for *Server v1.0([lis])
Accept 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.
-
HandleHTTP
Receiver for *Server v1.0([rpcPath debugPath])
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.
-
Register
Receiver for *Server v1.0([rcvr])
Register publishes in the server the set of methods of the
receiver value that satisfy the following conditions:
- exported method of exported type
- two arguments, both of exported type
- the second argument is a pointer
- one return value, of type error
It returns an error if the receiver is not an exported type or has
no suitable methods. It also logs the error using package log.
The client accesses each method using a string of the form "Type.Method",
where Type is the receiver's concrete type.
-
RegisterName
Receiver for *Server v1.0([name rcvr])
RegisterName is like Register but uses the provided name for the type
instead of the receiver's concrete type.
-
ServeCodec
Receiver for *Server v1.0([codec])
ServeCodec is like ServeConn but uses the specified codec to
decode requests and encode responses.
-
ServeConn
Receiver for *Server v1.0([conn])
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.
-
ServeHTTP
Receiver for *Server v1.0([w req])
ServeHTTP implements an http.Handler that answers RPC requests.
-
ServeRequest
Receiver for *Server v1.0([codec])
ServeRequest is like ServeCodec but synchronously serves a single request.
It does not close the codec upon completion.
-
*ServerError
Concrete Type v1.0ServerError represents an error that has been returned from
the remote side of the RPC connection.
-
Call
Concrete Type v1.0Call represents an active RPC.
-
Client
Concrete Type v1.0Client 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.
-
ClientCodec
Abstract Type v1.0A 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.
-
Close
Method for ClientCodec v1.0([])
-
ReadResponseBody
Method for ClientCodec v1.0([arg1])
-
ReadResponseHeader
Method for ClientCodec v1.0([arg1])
-
WriteRequest
Method for ClientCodec v1.0([arg1 arg2])
-
Request
Concrete Type v1.0Request 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.
-
Response
Concrete Type v1.0Response 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.
-
Server
Concrete Type v1.0Server represents an RPC Server.
-
ServerCodec
Abstract Type v1.0A 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.
-
Close
Method for ServerCodec v1.0([])
-
ReadRequestBody
Method for ServerCodec v1.0([arg1])
-
ReadRequestHeader
Method for ServerCodec v1.0([arg1])
-
WriteResponse
Method for ServerCodec v1.0([arg1 arg2])
-
ServerError
Concrete Type v1.0ServerError represents an error that has been returned from
the remote side of the RPC connection.
-
Error
Receiver for ServerError v1.0([])
-
arrayOfCall
Concrete Type v1.0Call represents an active RPC.
-
arrayOfClient
Concrete Type v1.0Client 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.
-
arrayOfClientCodec
Concrete Type v1.0A 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.
-
arrayOfRequest
Concrete Type v1.0Request 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.
-
arrayOfResponse
Concrete Type v1.0Response 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.
-
arrayOfServer
Concrete Type v1.0Server represents an RPC Server.
-
arrayOfServerCodec
Concrete Type v1.0A 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.
-
arrayOfServerError
Concrete Type v1.0ServerError represents an error that has been returned from
the remote side of the RPC connection.