grpc
v0.0.0-20150305034656-fa9ecddd2c59
Documentation
pkg.go.dev ↗Package grpc implements an RPC system called gRPC.
See https://github.com/grpc/grpc for more information about gRPC.
Variables
ErrUnspecTarget (2 declarations)
var (
// ErrUnspecTarget indicates that the target address is unspecified.
ErrUnspecTarget = errors.New("grpc: target is unspecified")
// ErrClientConnClosing indicates that the operation is illegal because
// the session is closing.
ErrClientConnClosing = errors.New("grpc: the client connection is closing")
)
ErrServerStopped
var (
// ErrServerStopped indicates that the operation is now illegal because of
// the server being stopped.
ErrServerStopped = errors.New("grpc: the server has been stopped")
)
Functions
Code
func Code(err error) codes.Code
Code returns the error code for err if it was produced by the rpc system. Otherwise, it returns codes.Unknown.
Errorf
func Errorf(c codes.Code, format string, a ...interface{}) error
Errorf returns an error containing an error code and a description; Errorf returns nil if c is OK.
Invoke
func Invoke(ctx context.Context, method string, args, reply proto.Message, cc *ClientConn, opts ...CallOption) error
Invoke is called by the generated code. It sends the RPC request on the wire and returns after response is received.
SendHeader
func SendHeader(ctx context.Context, md metadata.MD) error
SendHeader sends header metadata. It may be called at most once from a unary RPC handler. The ctx is the RPC handler's Context or one derived from it.
SetTrailer
func SetTrailer(ctx context.Context, md metadata.MD) error
SetTrailer sets the trailer metadata that will be sent when an RPC returns. It may be called at most once from a unary RPC handler. The ctx is the RPC handler's Context or one derived from it.
Types
CallOption
type CallOption interface {
// contains filtered or unexported methods
}
CallOption configures a Call before it starts or extracts information from a Call after it completes.
Header
func Header(md *metadata.MD) CallOption
Header returns a CallOptions that retrieves the header metadata for a unary RPC.
Trailer
func Trailer(md *metadata.MD) CallOption
Trailer returns a CallOptions that retrieves the trailer metadata for a unary RPC.
ClientConn
type ClientConn struct {
// contains filtered or unexported fields
}
ClientConn represents a client connection to an RPC service.
Dial
func Dial(target string, opts ...DialOption) (*ClientConn, error)
Dial creates a client connection the given target. TODO(zhaoq): Have an option to make Dial return immediately without waiting for connection to complete.
ClientConn.Close
func (cc *ClientConn) Close()
Close starts to tear down the ClientConn. TODO(zhaoq): Make this synchronous to avoid unbounded memory consumption in some edge cases (e.g., the caller opens and closes many ClientConn's in a tight loop.
ClientStream
type ClientStream interface {
// Header returns the header metedata received from the server if there
// is any. It blocks if the metadata is not ready to read.
Header() (metadata.MD, error)
// Trailer returns the trailer metadata from the server. It must be called
// after stream.Recv() returns non-nil error (including io.EOF) for
// bi-directional streaming and server streaming or stream.CloseAndRecv()
// returns for client streaming in order to receive trailer metadata if
// present. Otherwise, it could returns an empty MD even though trailer
// is present.
Trailer() metadata.MD
// CloseSend closes the send direction of the stream. It closes the stream
// when non-nil error is met.
CloseSend() error
Stream
}
ClientStream defines the interface a client stream has to satify.
NewClientStream
func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error)
NewClientStream creates a new Stream for the client side. This is called by generated code.
DialOption
type DialOption func(*dialOptions)
DialOption configures how we set up the connection including auth credentials.
WithPerRPCCredentials
func WithPerRPCCredentials(creds credentials.Credentials) DialOption
WithPerRPCCredentials returns a DialOption which sets credentials which will place auth state on each outbound RPC.
WithTransportCredentials
func WithTransportCredentials(creds credentials.TransportAuthenticator) DialOption
WithTransportCredentials returns a DialOption which configures a connection level security credentials (e.g., TLS/SSL).
MethodDesc
type MethodDesc struct {
MethodName string
Handler methodHandler
}
MethodDesc represents an RPC service's method specification.
Server
type Server struct {
// contains filtered or unexported fields
}
Server is a gRPC server to serve RPC requests.
NewServer
func NewServer(opt ...ServerOption) *Server
NewServer creates a gRPC server which has no service registered and has not started to accept requests yet.
Server.RegisterService
func (s *Server) RegisterService(sd *ServiceDesc, ss interface{})
RegisterService register a service and its implementation to the gRPC server. Called from the IDL generated code. This must be called before invoking Serve.
Server.Serve
func (s *Server) Serve(lis net.Listener) error
Serve accepts incoming connections on the listener lis, creating a new ServerTransport and service goroutine for each. The service goroutines read gRPC request and then call the registered handlers to reply to them. Service returns when lis.Accept fails.
Server.Stop
func (s *Server) Stop()
Stop stops the gRPC server. Once Stop returns, the server stops accepting connection requests and closes all the connected connections.
Server.TestingCloseConns
func (s *Server) TestingCloseConns()
TestingCloseConns closes all exiting transports but keeps s.lis accepting new connections. This is for test only now.
ServerOption
type ServerOption func(*options)
A ServerOption sets options.
MaxConcurrentStreams
func MaxConcurrentStreams(n uint32) ServerOption
MaxConcurrentStreams returns an Option that will apply a limit on the number of concurrent streams to each ServerTransport.
ServerStream
type ServerStream interface {
// SendHeader sends the header metadata. It should not be called
// after SendProto. It fails if called multiple times or if
// called after SendProto.
SendHeader(metadata.MD) error
// SetTrailer sets the trailer metadata which will be sent with the
// RPC status.
SetTrailer(metadata.MD)
Stream
}
ServerStream defines the interface a server stream has to satisfy.
ServiceDesc
type ServiceDesc struct {
ServiceName string
// The pointer to the service interface. Used to check whether the user
// provided implementation satisfies the interface requirements.
HandlerType interface{}
Methods []MethodDesc
Streams []StreamDesc
}
ServiceDesc represents an RPC service's specification.
Stream
type Stream interface {
// Context returns the context for this stream.
Context() context.Context
// SendProto blocks until it sends m, the stream is done or the stream
// breaks.
// On error, it aborts the stream and returns an RPC status on client
// side. On server side, it simply returns the error to the caller.
// SendProto is called by generated code.
SendProto(m proto.Message) error
// RecvProto blocks until it receives a proto message or the stream is
// done. On client side, it returns io.EOF when the stream is done. On
// any other error, it aborts the streama nd returns an RPC status. On
// server side, it simply returns the error to the caller.
RecvProto(m proto.Message) error
}
Stream defines the common interface a client or server stream has to satisfy.
StreamDesc
type StreamDesc struct {
StreamName string
Handler streamHandler
// At least one of these is true.
ServerStreams bool
ClientStreams bool
}
StreamDesc represents a streaming RPC service's method specification.