Documentation
¶
Overview ¶
Package grpcx makes gRPC calls without generated code: the schema comes from .proto files (compiled in pure Go) or from the server's reflection service, messages are JSON converted with protojson and dynamicpb, and a unary or server-streaming method is called over one short-lived connection.
A non-OK gRPC status is a normal Result (its code and message), not an error; errors are for what stops a call from being made: a schema that cannot be loaded, a body that does not fit the message, a method that is not supported yet.
Index ¶
Constants ¶
const DefaultTimeout = 30 * time.Second
DefaultTimeout is the deadline of a call that sets none.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BadBodyError ¶
type BadBodyError struct{ Detail string }
BadBodyError means the JSON does not fit the request message; nothing was sent.
func (*BadBodyError) Error ¶
func (e *BadBodyError) Error() string
type Call ¶
type Call struct {
// Server is grpc://host:port, grpcs://host:port or host:port (see
// model.GRPCTarget for which of those is plaintext).
Server string
// Method is package.Service/Method.
Method string
// JSON is the request message; empty means {}.
JSON []byte
// Metadata is sent with the call; names are lower-cased, and -bin names are
// refused.
Metadata []transport.Header
TLS *tls.Config
// Root is the collection directory the Protos and Imports are relative to.
Root string
// Protos are .proto files relative to Root. Empty: the schema is asked of the
// server by reflection.
Protos []string
// Imports are the roots the files' imports are resolved against, relative to
// Root. Root itself is always one.
Imports []string
// Receive says which messages of a server stream to keep and how long to
// wait for them; unused for a unary call.
Receive *transport.Receive
// Web makes the call over gRPC-Web: an HTTP POST to Server (an http:// or
// https:// address) instead of an HTTP/2 connection, for proxies and
// gateways that speak gRPC-Web. It needs Protos and cannot stream from the client.
Web bool
// Timeout is the deadline of the whole call; zero means DefaultTimeout.
Timeout time.Duration
Clock func() time.Time // nil: time.Now
}
Call is one gRPC call: where, what and with which schema.
type Method ¶
type Method struct {
Service string // acme.orders.v1.Orders
Name string // Get
ClientStream bool
ServerStream bool
Input string // full name of the request message
Output string
// Example is a request message as compact JSON with every field at its
// zero value, for a starting point.
Example string
}
Method is one method of a service, for pickers and validation.
type ProtoFile ¶
type ProtoFile struct {
Path string // as imports name it: "orders.proto", "common/types.proto"
Data []byte
}
ProtoFile is a .proto file with its import path.
type ProtoImport ¶
ProtoImport is what a .proto file brings: the files a collection must hold for it to compile (the file and the local files it imports, transitively) and the methods of the services it declares.
func ReadProto ¶
ReadProto compiles the .proto file name (its text is data) with no protoc. Files it imports are read from dir, as imports name them; the well-known google/protobuf types are built in. dir may be empty when nothing is imported. Only the services declared by the file itself are returned.
type Result ¶
type Result struct {
Messages []transport.Message
Header []transport.Header // response metadata
Trailer []transport.Header
Code codes.Code
Message string // the status message
// Warnings are things the call went ahead despite: a field of the JSON
// that the request message does not have, which was left out.
Warnings []string
Connect time.Duration
Total time.Duration
}
Result is what a call did. Messages holds the request as sent, then the responses kept. It is also returned with transport.ErrTimeout, holding what arrived before the wait ended.
func Run ¶
Run calls the method and returns what came back. All four kinds of method work. A method that streams from the client takes a JSON array as its body, one message for each element (a lone object is one message); a stream from the server is collected with the same rules as an async receive (count, timeout, match, live OnMessage).
Goroutine ownership: a server stream starts one reader goroutine, and a bidirectional call one more that sends; Run cancels the call and waits for both before it returns, and closes the connection last.
type SchemaError ¶
type SchemaError struct {
Detail string
// Proto is true when the .proto files are at fault (they did not compile or
// register), not the method or the server.
Proto bool
}
SchemaError means the schema could not be loaded or does not hold the method: a missing import, a server without reflection, an unknown method.
func (*SchemaError) Error ¶
func (e *SchemaError) Error() string