grpcx

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package grpcx is Stampede's gRPC driver. It calls unary and server-streaming methods without generated code: method descriptors come from server reflection or from .protoset/.proto files, messages are built from JSON with protojson, and responses are rendered back to JSON so checks and extractors work as they do for HTTP.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMessage = errors.New("message does not match the method's input type")

ErrInvalidMessage wraps a request message that does not fit the method's input type.

View Source
var ErrUnknownMethod = errors.New("unknown gRPC method")

ErrUnknownMethod wraps lookups of a service or method the descriptors do not define, or of a kind of method that cannot be called.

Functions

func CodeByName

func CodeByName(name string) (codes.Code, bool)

CodeByName maps a status code name to its code. Names are matched without regard to case or underscores, so NOT_FOUND, NotFound and not_found all work.

func CodeName

func CodeName(c codes.Code) string

CodeName is a code's canonical name, such as NOT_FOUND.

func CompileSources

func CompileSources(ctx context.Context, sources map[string][]byte, importPaths []string) (*protoregistry.Files, error)

CompileSources compiles .proto sources held in memory, keyed by the file name imports use. Imports not among the sources are the built-in well-known types or, when importPaths is set, files found under those directories.

func FindMethod

func FindMethod(files *protoregistry.Files, service, method string) (protoreflect.MethodDescriptor, error)

FindMethod finds a callable method (unary or server streaming) in compiled descriptors.

func FullMethod

func FullMethod(m protoreflect.MethodDescriptor) string

FullMethod is the method's path on the wire: /package.Service/Method.

func NewRequest

func NewRequest(m protoreflect.MethodDescriptor, js []byte) (*dynamicpb.Message, error)

NewRequest builds the method's input message from JSON.

func Reflect

func Reflect(ctx context.Context, conn grpc.ClientConnInterface, service string) (*protoregistry.Files, error)

Reflect fetches the files defining service, and everything they import, from the server's reflection service (v1, falling back to v1alpha).

Types

type Descriptors

type Descriptors struct {
	// contains filtered or unexported fields
}

Descriptors resolves and caches method descriptors for a run.

func NewDescriptors

func NewDescriptors() *Descriptors

NewDescriptors returns an empty cache.

func (*Descriptors) LoadFiles

func (d *Descriptors) LoadFiles(ctx context.Context, src Source) error

LoadFiles loads a source's descriptor files, for validating them before a run.

func (*Descriptors) Method

func (d *Descriptors) Method(ctx context.Context, conn grpc.ClientConnInterface, t Target, src Source, service, method string) (protoreflect.MethodDescriptor, error)

Method finds a method by service full name and method name. File sources are loaded once; reflection asks the target the first time a service is used. Client and bidirectional streaming methods are rejected, as only unary and server streaming calls are supported.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool holds client connections shared by every virtual user, as a service's gRPC client would.

func NewPool

func NewPool(o PoolOptions) *Pool

NewPool creates an empty pool; connections open on first use.

func (*Pool) Close

func (p *Pool) Close()

Close closes every connection.

func (*Pool) Conn

func (p *Pool) Conn(t Target, user int) (*grpc.ClientConn, error)

Conn returns the connection user should use for t.

type PoolOptions

type PoolOptions struct {
	// Dial replaces the network dialer (DNS cache, tests).
	Dial               func(ctx context.Context, network, addr string) (net.Conn, error)
	InsecureSkipVerify bool
	UserAgent          string
	// PerTarget is how many HTTP/2 connections are opened to each target
	// (default 4). Users are spread over them, so one connection's
	// concurrent-stream limit does not cap the load.
	PerTarget int
}

PoolOptions configures client connections.

type Result

type Result struct {
	Start, End time.Time
	Code       codes.Code
	// Body is the response as protojson; a server stream's messages are
	// a JSON array.
	Body []byte
	// Header and Trailer are the response metadata.
	Header, Trailer metadata.MD
	// Messages counts received messages; First is when the first arrived.
	Messages int
	First    time.Time
	BytesIn  int64
	BytesOut int64
	// Err is the call's error, if any; Code is its status code.
	Err error
}

Result is a completed call.

func Invoke

Invoke calls a unary or server-streaming method. Up to maxBody bytes of response JSON are kept.

type Source

type Source struct {
	Protoset    string
	Proto       []string
	ImportPaths []string
}

Source says where a method's descriptor comes from: files when Protoset or Proto is set, otherwise the server's reflection service.

func (Source) Files

func (s Source) Files() bool

Files reports whether descriptors come from files.

type Target

type Target struct {
	Addr string
	TLS  bool
}

Target is where calls go: a host:port and whether to use TLS.

func ParseTarget

func ParseTarget(s string) (Target, error)

ParseTarget reads grpc://host:port (plaintext, HTTP/2 prior knowledge) or grpcs://host:port (TLS). An http(s) base URL is accepted too, so a scenario's target.baseURL can serve gRPC steps. A missing port means 443 with TLS and 80 without.

func (Target) String

func (t Target) String() string

String renders the target as a URL.

func (Target) URL

func (t Target) URL() *url.URL

URL is the target as a URL, for host policy checks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL