rpc

Imports

Imports #

"bufio"
"encoding/gob"
"errors"
"io"
"log"
"net"
"net/http"
"sync"
"fmt"
"html/template"
"net/http"
"slices"
"strings"
"bufio"
"encoding/gob"
"errors"
"go/token"
"io"
"log"
"net"
"net/http"
"reflect"
"strings"
"sync"

Constants & Variables

DefaultDebugPath const #

const DefaultDebugPath = "/debug/rpc"

DefaultRPCPath const #

Defaults used by HandleHTTP

const DefaultRPCPath = "/_goRPC_"

DefaultServer var #

DefaultServer is the default instance of [*Server].

var DefaultServer = *ast.CallExpr

ErrShutdown var #

var ErrShutdown = *ast.CallExpr

connected var #

Can connect to RPC service using HTTP CONNECT to rpcPath.

var connected = "200 Connected to Go RPC"

debug var #

var debug = *ast.CallExpr

debugLog var #

If set, print log statements for internal and I/O errors.

var debugLog = false

debugText const #

const debugText = `
	
	Services
	{{range .}}
	
Service {{.Name}}
{{range .Method}} {{end}}
MethodCalls
{{.Name}}({{.Type.ArgType}}, {{.Type.ReplyType}}) error {{.Type.NumCalls}}
{{end}} `

invalidRequest var #

A value sent as a placeholder for the server's response value when the server receives an invalid request. It is never decoded by the client since the Response contains an error when it is used.

var invalidRequest = struct{...}{...}

logRegisterError const #

logRegisterError specifies whether to log problems during method registration. To debug registration, recompile the package with this set to true.

const logRegisterError = false

typeOfError var #

Precompute the reflect type for error.

var typeOfError = *ast.CallExpr

Type Aliases

ServerError type #

ServerError represents an error that has been returned from the remote side of the RPC connection.

type ServerError string

methodArray type #

type methodArray []debugMethod

serviceArray type #

type serviceArray []debugService

Interfaces

ClientCodec interface #

A ClientCodec implements writing of RPC requests and reading of RPC responses for the client side of an RPC session. The client calls [ClientCodec.WriteRequest] to write a request to the connection and calls [ClientCodec.ReadResponseHeader] and [ClientCodec.ReadResponseBody] in pairs to read responses. The client calls [ClientCodec.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 ClientCodec interface {
WriteRequest(*Request, any) error
ReadResponseHeader(*Response) error
ReadResponseBody(any) error
Close() error
}

ServerCodec interface #

A ServerCodec implements reading of RPC requests and writing of RPC responses for the server side of an RPC session. The server calls [ServerCodec.ReadRequestHeader] and [ServerCodec.ReadRequestBody] in pairs to read requests from the connection, and it calls [ServerCodec.WriteResponse] to write a response back. The server calls [ServerCodec.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 ServerCodec interface {
ReadRequestHeader(*Request) error
ReadRequestBody(any) error
WriteResponse(*Response, any) error
Close() error
}

Structs

Call struct #

Call represents an active RPC.

type Call struct {
ServiceMethod string
Args any
Reply any
Error error
Done chan *Call
}

Client struct #

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.

type Client struct {
codec ClientCodec
reqMutex sync.Mutex
request Request
mutex sync.Mutex
seq uint64
pending map[uint64]*Call
closing bool
shutdown bool
}

Request struct #

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 Request struct {
ServiceMethod string
Seq uint64
next *Request
}

Response struct #

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 Response struct {
ServiceMethod string
Seq uint64
Error string
next *Response
}

Server struct #

Server represents an RPC Server.

type Server struct {
serviceMap sync.Map
reqLock sync.Mutex
freeReq *Request
respLock sync.Mutex
freeResp *Response
}

debugHTTP struct #

type debugHTTP struct {
*Server
}

debugMethod struct #

type debugMethod struct {
Type *methodType
Name string
}

debugService struct #

type debugService struct {
Service *service
Name string
Method []debugMethod
}

gobClientCodec struct #

type gobClientCodec struct {
rwc io.ReadWriteCloser
dec *gob.Decoder
enc *gob.Encoder
encBuf *bufio.Writer
}

gobServerCodec struct #

type gobServerCodec struct {
rwc io.ReadWriteCloser
dec *gob.Decoder
enc *gob.Encoder
encBuf *bufio.Writer
closed bool
}

methodType struct #

type methodType struct {
sync.Mutex
method reflect.Method
ArgType reflect.Type
ReplyType reflect.Type
numCalls uint
}

service struct #

type service struct {
name string
rcvr reflect.Value
typ reflect.Type
method map[string]*methodType
}

Functions

Accept method #

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.

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

Accept function #

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.

func Accept(lis net.Listener)

Call method #

Call invokes the named function, waits for it to complete, and returns its error status.

func (client *Client) Call(serviceMethod string, args any, reply any) error

Close method #

func (c *gobClientCodec) Close() error

Close method #

func (c *gobServerCodec) Close() error

Close method #

Close calls the underlying codec's Close method. If the connection is already shutting down, [ErrShutdown] is returned.

func (client *Client) Close() error

Dial function #

Dial connects to an RPC server at the specified network address.

func Dial(network string, address string) (*Client, error)

DialHTTP function #

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

func DialHTTP(network string, address string) (*Client, error)

DialHTTPPath function #

DialHTTPPath connects to an HTTP RPC server at the specified network address and path.

func DialHTTPPath(network string, address string, path string) (*Client, error)

Error method #

func (e ServerError) Error() string

Go method #

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.

func (client *Client) Go(serviceMethod string, args any, reply any, done chan *Call) *Call

HandleHTTP method #

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 *Server) HandleHTTP(rpcPath string, debugPath string)

HandleHTTP function #

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 HandleHTTP()

Len method #

func (s serviceArray) Len() int

Len method #

func (m methodArray) Len() int

Less method #

func (m methodArray) Less(i int, j int) bool

Less method #

func (s serviceArray) Less(i int, j int) bool

NewClient function #

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 NewClient(conn io.ReadWriteCloser) *Client

NewClientWithCodec function #

NewClientWithCodec is like [NewClient] but uses the specified codec to encode requests and decode responses.

func NewClientWithCodec(codec ClientCodec) *Client

NewServer function #

NewServer returns a new [Server].

func NewServer() *Server

NumCalls method #

func (m *methodType) NumCalls() (n uint)

ReadRequestBody method #

func (c *gobServerCodec) ReadRequestBody(body any) error

ReadRequestHeader method #

func (c *gobServerCodec) ReadRequestHeader(r *Request) error

ReadResponseBody method #

func (c *gobClientCodec) ReadResponseBody(body any) error

ReadResponseHeader method #

func (c *gobClientCodec) ReadResponseHeader(r *Response) error

Register function #

Register publishes the receiver's methods in the [DefaultServer].

func Register(rcvr any) error

Register method #

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.

func (server *Server) Register(rcvr any) error

RegisterName method #

RegisterName is like [Register] but uses the provided name for the type instead of the receiver's concrete type.

func (server *Server) RegisterName(name string, rcvr any) error

RegisterName function #

RegisterName is like [Register] but uses the provided name for the type instead of the receiver's concrete type.

func RegisterName(name string, rcvr any) error

ServeCodec function #

ServeCodec is like [ServeConn] but uses the specified codec to decode requests and encode responses.

func ServeCodec(codec ServerCodec)

ServeCodec method #

ServeCodec is like [ServeConn] but uses the specified codec to decode requests and encode responses.

func (server *Server) ServeCodec(codec ServerCodec)

ServeConn function #

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 ServeConn(conn io.ReadWriteCloser)

ServeConn method #

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 *Server) ServeConn(conn io.ReadWriteCloser)

ServeHTTP method #

ServeHTTP implements an [http.Handler] that answers RPC requests.

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

ServeHTTP method #

Runs at /debug/rpc

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

ServeRequest function #

ServeRequest is like [ServeCodec] but synchronously serves a single request. It does not close the codec upon completion.

func ServeRequest(codec ServerCodec) error

ServeRequest method #

ServeRequest is like [ServeCodec] but synchronously serves a single request. It does not close the codec upon completion.

func (server *Server) ServeRequest(codec ServerCodec) error

Swap method #

func (s serviceArray) Swap(i int, j int)

Swap method #

func (m methodArray) Swap(i int, j int)

WriteRequest method #

func (c *gobClientCodec) WriteRequest(r *Request, body any) (err error)

WriteResponse method #

func (c *gobServerCodec) WriteResponse(r *Response, body any) (err error)

call method #

func (s *service) call(server *Server, sending *sync.Mutex, wg *sync.WaitGroup, mtype *methodType, req *Request, argv reflect.Value, replyv reflect.Value, codec ServerCodec)

done method #

func (call *Call) done()

freeRequest method #

func (server *Server) freeRequest(req *Request)

freeResponse method #

func (server *Server) freeResponse(resp *Response)

getRequest method #

func (server *Server) getRequest() *Request

getResponse method #

func (server *Server) getResponse() *Response

input method #

func (client *Client) input()

isExportedOrBuiltinType function #

Is this type exported or a builtin?

func isExportedOrBuiltinType(t reflect.Type) bool

readRequest method #

func (server *Server) readRequest(codec ServerCodec) (service *service, mtype *methodType, req *Request, argv reflect.Value, replyv reflect.Value, keepReading bool, err error)

readRequestHeader method #

func (server *Server) readRequestHeader(codec ServerCodec) (svc *service, mtype *methodType, req *Request, keepReading bool, err error)

register method #

func (server *Server) register(rcvr any, name string, useName bool) error

send method #

func (client *Client) send(call *Call)

sendResponse method #

func (server *Server) sendResponse(sending *sync.Mutex, req *Request, reply any, codec ServerCodec, errmsg string)

suitableMethods function #

suitableMethods returns suitable Rpc methods of typ. It will log errors if logErr is true.

func suitableMethods(typ reflect.Type, logErr bool) map[string]*methodType

Generated with Arrow