std/grpc
On this page
Index
- type
Code- method
Code.number
- method
- type
FrameErr - type
Status- method
Status.display
- method
- type
Call - type
RecvState - type
Recv- method
Recv.next
- method
- type
Send- method
Send.send - method
Send.set_trailer
- method
- type
Server- method
Server.with_max_recv - method
Server.unary - method
Server.stream - method
Server.router
- method
- type
Client- method
Client.with_gzip - method
Client.unary - method
Client.call
- method
- type
ClientCall- method
ClientCall.send - method
ClientCall.close_send - method
ClientCall.recv
- method
- type
CallState - fn
code_from - fn
frame - fn
unframe - fn
encode_timeout - fn
decode_timeout - fn
encode_message - fn
encode_message_bytes - fn
decode_message - fn
decode_message_bytes - fn
bin_encode - fn
bin_decode - fn
server - fn
client
Types
Code
enum Code {
Ok
Canceled
Unknown
InvalidArgument
DeadlineExceeded
NotFound
AlreadyExists
PermissionDenied
ResourceExhausted
FailedPrecondition
Aborted
OutOfRange
Unimplemented
Internal
Unavailable
DataLoss
Unauthenticated
} derive DisplayThe 17 canonical status codes (their numbers are the wire values of grpc-status).
Code.number
fn (c: Code) number() -> IntThe wire number of a code (OK is 0, UNAUTHENTICATED is 16).
FrameErr
enum FrameErr {
Truncated
BadFlag(flag: Int)
TooLarge(len: Int)
} derive DisplayWhy a frame was refused.
Status
struct Status {
code: Code
message: Str = ""
details: Bytes = b""
}A call’s outcome that is not OK, the error of every call: the code, the status message and the raw
grpc-status-details-bin payload when the peer sent one.
Status.display
fn (s: Status) display() -> StrDisplays as rpc error: code = NotFound desc = no such user (grpc-go’s text).
Call
struct Call {
method: Str
metadata: Map[Str, Str]
timeout: Duration?
}The call a handler is serving.
RecvState
struct RecvState {
buf: Bytes = b""
at: Int = 0
done: Bool = false
gzip: Bool = false
}Recv
struct Recv {
body: http.BodyReader
st: Mutex[RecvState]
max: Int
}The messages of a call’s request stream, as a handler reads them.
Recv.next
fn (r: Recv) next() -> Bytes? ! StatusThe next message; None when the client has finished sending. A message above the receive limit is
ResourceExhausted, a cut stream or a bad frame Internal, a compressed message Unimplemented.
Send
struct Send {
w: http.BodyWriter
gzip: Bool = false
}The messages a handler sends back.
Send.send
fn (s: Send) send(msg: Bytes) -> () ! StatusSends one message (a frame is flushed at once).
Send.set_trailer
fn (s: Send) set_trailer(name: Str, value: Str)A trailer sent after the last message, next to grpc-status and grpc-message.
Server
struct Server {
routes: http.Router
max_recv: Int
methods: List[Str]
clock: Clock
}A gRPC server: methods registered on it are served through router() by an HTTP/2 http.Server.
Server.with_max_recv
fn (s: Server) with_max_recv(n: Int) -> ServerThe server with another receive limit in bytes.
Server.unary
fn (var s: Server) unary[fx: Effects](method: Str, f: fn(Call, Bytes) -> Bytes ! Status uses fx) uses (fx, clock)Registers a unary method (/package.Service/Method).
Server.stream
fn (var s: Server) stream[fx: Effects](method: Str, f: fn(Call, Recv, Send) -> () ! Status uses fx) uses (fx, clock)Registers a method of any kind: a handler reads the request messages from Recv and writes the responses to
Send; returning Ok ends the call with OK, an error with its status.
Server.router
fn (s: Server) router() -> http.RouterThe routes to give http.server, with http.Protocol.UnencryptedHttp2 (or TLS and ALPN h2) in its protocols.
Client
struct Client {
http: http.Client
base: Str
gzip: Bool = false
}A gRPC client over an http.Client.
Client.with_gzip
fn (c: Client) with_gzip() -> ClientThe client sending its messages gzip compressed (grpc-encoding: gzip); responses are read compressed or not.
Client.unary
fn (c: Client) unary(method: Str, req: Bytes, metadata: Map[Str, Str] = {}, timeout: Duration? = None) -> Bytes ! Status uses net.dialOne unary call: the request message, the response message, or the call’s status. timeout is sent as
grpc-timeout and bounds the call (DeadlineExceeded).
Client.call
fn (c: Client) call[R, fx: Effects](method: Str, metadata: Map[Str, Str], timeout: Duration?, f: fn(ClientCall) -> R ! Status uses fx) -> R ! Status uses (fx, net.dial)Runs f on a streaming call: f sends with send, ends the request with close_send and reads with recv.
The call’s status is the result of recv after the last message (an error when it is not OK).
ClientCall
struct ClientCall {
w: http.PipeWriter
head: Chan[(http.ClientStream?, Status?)]
st: Mutex[CallState]
max: Int
gzip: Bool = false
}The client side of a streaming call, valid inside Client.call.
ClientCall.send
fn (c: ClientCall) send(msg: Bytes) -> () ! StatusSends one message.
ClientCall.close_send
fn (c: ClientCall) close_send()Ends the request stream.
ClientCall.recv
fn (c: ClientCall) recv() -> Bytes? ! Status uses net.dialThe next response message; None at the end of an OK call, the status as an error otherwise.
CallState
struct CallState {
stream: http.ClientStream? = None
failed: Status? = None
recv: RecvState = RecvState{}
final: Status? = None
}Functions
code_from
fn code_from(n: Int) -> Code?The code of a grpc-status number; None above 16.
frame
fn frame(payload: Bytes, compressed: Bool = false) -> BytesOne message frame: a flag byte (1 for a compressed payload), a 4 byte big endian length, the payload.
unframe
fn unframe(data: Bytes, at: Int, max_len: Int = 4194304) -> (Bool, Bytes, Int) ! FrameErrReads the frame at at: (compressed, payload, next offset). A length above max_len is TooLarge (grpc-go’s
default receive limit is 4 MiB).
encode_timeout
fn encode_timeout(ns: Int) -> StrThe grpc-timeout header value for a duration in nanoseconds: at most 8 digits and the largest unit that keeps
it (n, u, m, S, M, H), as grpc-go’s encodeTimeout.
decode_timeout
fn decode_timeout(s: Str) -> Int?The nanoseconds of a grpc-timeout value; None for a value that is not 1 to 8 digits and one unit letter.
encode_message
fn encode_message(s: Str) -> Strgrpc-message encoding: bytes outside 0x20 to 0x7e and % are written as %XX (upper case hex).
encode_message_bytes
fn encode_message_bytes(b: Bytes) -> Strencode_message of arbitrary bytes (a status message from another language need not be UTF-8).
decode_message
fn decode_message(s: Str) -> StrThe inverse; a malformed escape is kept as written (grpc-go), and bytes that are not UTF-8 become U+FFFD.
decode_message_bytes
fn decode_message_bytes(s: Str) -> BytesThe inverse as raw bytes, exactly what grpc-go’s client reports.
bin_encode
fn bin_encode(data: Bytes) -> StrA binary metadata value as grpc-go sends it: unpadded standard base64, for a name that ends in -bin.
bin_decode
fn bin_decode(value: Str) -> Bytes?The bytes of a -bin metadata value, padded or not (grpc-go accepts both); None when it is not base64.
server
fn server(clock: Clock) -> ServerA server with grpc-go’s 4 MiB receive limit. clock times the deadlines clients send: a handler still running at
its grpc-timeout is cancelled and the call ends with DeadlineExceeded.
client
fn client(c: http.Client, base: Str) -> ClientA client of base (http://host:port, plain HTTP/2 with prior knowledge, or https://host:port).