codegen

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 21 Imported by: 92

Documentation

Overview

Package codegen contains functions and types used by the weaver_gen.go files generated by "weaver generate". The APIs in this package are not suitable for direct use by Service Weaver applications.

Index

Constants

View Source
const (
	MethodCountsName       = "serviceweaver_method_count"
	MethodErrorsName       = "serviceweaver_method_error_count"
	MethodLatenciesName    = "serviceweaver_method_latency_micros"
	MethodBytesRequestName = "serviceweaver_method_bytes_request"
	MethodBytesReplyName   = "serviceweaver_method_bytes_reply"
)

Names of automatically populated metrics.

Variables

This section is empty.

Functions

func CatchPanics

func CatchPanics(r interface{}) error

CatchPanics recovers from panic() calls that occur during encoding, decoding, and RPC execution.

func ComponentConfigValidator

func ComponentConfigValidator(path, cfg string) error

ComponentConfigValidator checks that cfg is a valid configuration for the component type whose fully qualified name is given by path.

TODO(mwhittaker): Move out of codegen package? It's not used by the generated code.

func ExtractEdges added in v0.9.0

func ExtractEdges(data []byte) [][2]string

ExtractEdges returns the edges corresponding to MakeEdgeString() results that occur in data.

func MakeEdgeString added in v0.9.0

func MakeEdgeString(src, dst string) string

MakeEdgeString returns a string that should be emitted into generated code to represent an edge from src to dst.

func MakeListenersString added in v0.15.0

func MakeListenersString(component string, listeners []string) string

MakeListenersString returns a string that should be emitted into generated code to represent the set of listeners associated with a given component.

func Register

func Register(reg Registration)

Register registers a Service Weaver component.

func RegisterSerializable added in v0.18.0

func RegisterSerializable[T AutoMarshal]()

RegisterSerializable records type T as serializable. This is needed to instantiate the appropriate concrete type when an interface is sent over the wire (currently only used for AutoMarshal errors returned from remote method calls). The registration is automatically done by generated code for custom error structs that embed weaver.AutoMarshal.

Types

type AutoMarshal

type AutoMarshal interface {
	WeaverMarshal(enc *Encoder)
	WeaverUnmarshal(dec *Decoder)
}

AutoMarshal is the interface implemented by structs with weaver.AutoMarshal declarations.

type CallEdge added in v0.7.2

type CallEdge struct {
	Caller reflect.Type
	Callee reflect.Type
}

CallEdge records that fact that the Caller component uses the Callee component. Both types are types of the corresponding component interfaces.

func CallGraph added in v0.7.2

func CallGraph() []CallEdge

CallGraph returns the component call graph (as a list of CallEdge values).

type ComponentListeners added in v0.15.0

type ComponentListeners struct {
	// Fully qualified component type name, e.g.,
	//   github.com/ServiceWeaver/weaver/Main.
	Component string

	// The list of listener names associated with the component.
	Listeners []string
}

ComponentListeners represents a set of listeners for a given component.

func ExtractListeners added in v0.15.0

func ExtractListeners(data []byte) []ComponentListeners

ExtractListeners returns the components and their listeners encoded using MakeListenersString() in data.

type Decoder

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

Decoder deserializes data from a byte slice data in the expected results.

func NewDecoder

func NewDecoder(data []byte) *Decoder

NewDecoder instantiates a new Decoder for a given byte slice.

func (*Decoder) Bool

func (d *Decoder) Bool() bool

Bool decodes a value of type bool.

func (*Decoder) Byte

func (d *Decoder) Byte() byte

Byte decodes a value of type byte.

func (*Decoder) Bytes

func (d *Decoder) Bytes() []byte

Bytes decodes a value of type []byte.

func (*Decoder) Complex128

func (d *Decoder) Complex128() complex128

Complex128 decodes a value of type complex128.

func (*Decoder) Complex64

func (d *Decoder) Complex64() complex64

Complex64 decodes a value of type complex64.

func (*Decoder) DecodeBinaryUnmarshaler

func (d *Decoder) DecodeBinaryUnmarshaler(value encoding.BinaryUnmarshaler)

DecodeBinaryUnmarshaler deserializes the value from a byte slice using UnmarshalBinary.

func (*Decoder) DecodeProto

func (d *Decoder) DecodeProto(value proto.Message)

DecodeProto deserializes the value from a byte slice using proto serialization.

func (*Decoder) Empty

func (d *Decoder) Empty() bool

Empty returns true iff all bytes in d have been consumed.

func (*Decoder) Error

func (d *Decoder) Error() error

Error decodes an error. We construct an instance of a special error value that provides Is and Unwrap support.

func (*Decoder) Float32

func (d *Decoder) Float32() float32

Float32 decodes a value of type float32.

func (*Decoder) Float64

func (d *Decoder) Float64() float64

Float64 decodes a value of type float64.

func (*Decoder) Int

func (d *Decoder) Int() int

Int decodes a value of type int. Int values are encoded as 64 bits.

func (*Decoder) Int16

func (d *Decoder) Int16() int16

Int16 decodes a value of type int16.

func (*Decoder) Int32

func (d *Decoder) Int32() int32

Int32 decodes a value of type int32.

func (*Decoder) Int64

func (d *Decoder) Int64() int64

Int64 decodes a value of type int64.

func (*Decoder) Int8

func (d *Decoder) Int8() int8

Int8 decodes a value of type int8.

func (*Decoder) Interface added in v0.18.0

func (d *Decoder) Interface() any

Interface decodes a value encoded by Encoder.Interface. Panics if the encoded value does not belong to a type registered using RegisterSerializable.

func (*Decoder) Len

func (d *Decoder) Len() int

Len attempts to decode an int32.

Panics if the result is negative (except -1).

NOTE that this method should be called only in the generated code, to avoid generating repetitive code that decodes the length of a non-basic type (e.g., slice, map).

func (*Decoder) Read

func (d *Decoder) Read(n int) []byte

Read reads and returns n bytes from the decoder and advances the decode past the read bytes.

func (*Decoder) Rune

func (d *Decoder) Rune() rune

Rune decodes a value of type rune.

func (*Decoder) String

func (d *Decoder) String() string

String decodes a value of type string.

func (*Decoder) Uint

func (d *Decoder) Uint() uint

Uint decodes a value of type uint. Uint values are encoded as 64 bits.

func (*Decoder) Uint16

func (d *Decoder) Uint16() uint16

Uint16 decodes a value of type uint16.

func (*Decoder) Uint32

func (d *Decoder) Uint32() uint32

Uint32 decodes a value of type uint32.

func (*Decoder) Uint64

func (d *Decoder) Uint64() uint64

Uint64 decodes a value of type uint64.

func (*Decoder) Uint8

func (d *Decoder) Uint8() uint8

Uint8 decodes a value of type uint8.

type Encoder

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

Encoder serializes data in a byte slice data.

func NewEncoder

func NewEncoder() *Encoder

func (*Encoder) Bool

func (e *Encoder) Bool(arg bool)

Bool encodes an arg of type bool. Serialize boolean values as an uint8 that encodes either 0 or 1.

func (*Encoder) Byte

func (e *Encoder) Byte(arg byte)

Byte encodes an arg of type byte.

func (*Encoder) Bytes

func (e *Encoder) Bytes(arg []byte)

Bytes encodes an arg of type []byte. For a byte slice, we encode its length, followed by the serialized content. If the slice is nil, we encode length as -1.

func (*Encoder) Complex128

func (e *Encoder) Complex128(arg complex128)

Complex128 encodes an arg of type complex128.

func (*Encoder) Complex64

func (e *Encoder) Complex64(arg complex64)

Complex64 encodes an arg of type complex64. We encode the real and the imaginary parts one after the other.

func (*Encoder) Data

func (e *Encoder) Data() []byte

Data returns the byte slice that contains the serialized arguments.

func (*Encoder) EncodeBinaryMarshaler

func (e *Encoder) EncodeBinaryMarshaler(value encoding.BinaryMarshaler)

EncodeBinaryMarshaler serializes value into a byte slice using its MarshalBinary method.

func (*Encoder) EncodeProto

func (e *Encoder) EncodeProto(value proto.Message)

EncodeProto serializes value into a byte slice using proto serialization.

func (*Encoder) Error

func (e *Encoder) Error(err error)

Error encodes an arg of type error. We save enough type information to allow errors.Unwrap(), errors.Is(), and errors.As() to work correctly.

func (*Encoder) Float32

func (e *Encoder) Float32(arg float32)

Float32 encodes an arg of type float32.

func (*Encoder) Float64

func (e *Encoder) Float64(arg float64)

Float64 encodes an arg of type float64.

func (*Encoder) Grow

func (e *Encoder) Grow(bytesNeeded int) []byte

Grow increases the size of the encoder's data if needed. Only appends a new slice if there is not enough capacity to satisfy bytesNeeded. Returns the slice fragment that contains bytesNeeded.

func (*Encoder) Int

func (e *Encoder) Int(arg int)

Int encodes an arg of type int. Int can have 32 bits or 64 bits based on the machine type. To simplify our reasoning, we encode the highest possible value.

func (*Encoder) Int16

func (e *Encoder) Int16(arg int16)

Int16 encodes an arg of type int16.

func (*Encoder) Int32

func (e *Encoder) Int32(arg int32)

Int32 encodes an arg of type int32.

func (*Encoder) Int64

func (e *Encoder) Int64(arg int64)

Int64 encodes an arg of type int64.

func (*Encoder) Int8

func (e *Encoder) Int8(arg int8)

Int8 encodes an arg of type int8.

func (*Encoder) Interface added in v0.18.0

func (e *Encoder) Interface(value AutoMarshal)

Interface encodes value prefixed with its concrete type.

func (*Encoder) Len

func (e *Encoder) Len(l int)

Len attempts to encode l as an int32.

Panics if l is bigger than an int32 or a negative length (except -1).

NOTE that this method should be called only in the generated code, to avoid generating repetitive code that encodes the length of a non-basic type (e.g., slice, map).

func (*Encoder) Reset

func (e *Encoder) Reset(n int)

Reset resets the Encoder to use a buffer with a capacity of at least the provided size. All encoded data is lost.

func (*Encoder) Rune

func (e *Encoder) Rune(arg rune)

Rune encodes an arg of type rune.

func (*Encoder) String

func (e *Encoder) String(arg string)

String encodes an arg of type string. For a string, we encode its length, followed by the serialized content.

func (*Encoder) Uint

func (e *Encoder) Uint(arg uint)

Uint encodes an arg of type uint. Uint can have 32 bits or 64 bits based on the machine type. To simplify our reasoning, we encode the highest possible value.

func (*Encoder) Uint16

func (e *Encoder) Uint16(arg uint16)

Uint16 encodes an arg of type uint16.

func (*Encoder) Uint32

func (e *Encoder) Uint32(arg uint32)

Uint32 encodes an arg of type uint32.

func (*Encoder) Uint64

func (e *Encoder) Uint64(arg uint64)

Uint64 encodes an arg of type uint64.

func (*Encoder) Uint8

func (e *Encoder) Uint8(arg uint8)

Uint8 encodes an arg of type uint8.

type Hasher

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

Hasher computes a non-cryptographic hash of the sequence of values added to it.

If the same sequence of values is added to two differ Hashers, they will produce the same result, even if they are in different processes.

func (*Hasher) Sum64

func (h *Hasher) Sum64() uint64

Sum64 returns the 64-bit hash of the sequence of values added so far. The resulting is in the range [1,2^64-2], i.e., it is never 0 or math.MaxUint64.

func (*Hasher) WriteFloat32

func (h *Hasher) WriteFloat32(v float32)

WriteFloat32 adds a float32 to the hasher.

func (*Hasher) WriteFloat64

func (h *Hasher) WriteFloat64(v float64)

WriteFloat64 adds a float64 to the hasher.

func (*Hasher) WriteInt

func (h *Hasher) WriteInt(v int)

WriteInt adds a int to the hasher.

func (*Hasher) WriteInt16

func (h *Hasher) WriteInt16(v int16)

WriteInt16 adds a int16 to the hasher.

func (*Hasher) WriteInt32

func (h *Hasher) WriteInt32(v int32)

WriteInt32 adds a int32 to the hasher.

func (*Hasher) WriteInt64

func (h *Hasher) WriteInt64(v int64)

WriteInt64 adds a int64 to the hasher.

func (*Hasher) WriteInt8

func (h *Hasher) WriteInt8(v int8)

WriteInt8 adds a int8 to the hasher.

func (*Hasher) WriteString

func (h *Hasher) WriteString(v string)

WriteString adds a string to the hasher.

func (*Hasher) WriteUint

func (h *Hasher) WriteUint(v uint)

WriteUint adds a uint to the hasher.

func (*Hasher) WriteUint16

func (h *Hasher) WriteUint16(v uint16)

WriteUint16 adds a uint16 to the hasher.

func (*Hasher) WriteUint32

func (h *Hasher) WriteUint32(v uint32)

WriteUint32 adds a uint32 to the hasher.

func (*Hasher) WriteUint64

func (h *Hasher) WriteUint64(v uint64)

WriteUint64 adds a uint64 to the hasher.

func (*Hasher) WriteUint8

func (h *Hasher) WriteUint8(v uint8)

WriteUint8 adds a uint8 to the hasher.

type LatestVersion added in v0.10.0

type LatestVersion = Version[[version.CodegenMajor][version.CodegenMinor]struct{}]

type MethodCallHandle added in v0.17.0

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

MethodCallHandle holds information needed to finalize metric updates for a method call.

type MethodLabels

type MethodLabels struct {
	Caller    string // full calling component name
	Component string // full callee component name
	Method    string // callee component method's name
	Remote    bool   // Is this a remote call?
}

type MethodMetrics

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

MethodMetrics contains metrics for a single Service Weaver component method.

func MethodMetricsFor

func MethodMetricsFor(labels MethodLabels) *MethodMetrics

MethodMetricsFor returns metrics for the specified method.

func (*MethodMetrics) Begin added in v0.17.0

func (m *MethodMetrics) Begin() MethodCallHandle

Begin starts metric update recording for a call to method m.

func (*MethodMetrics) End added in v0.17.0

func (m *MethodMetrics) End(h MethodCallHandle, failed bool, requestBytes, replyBytes int)

End ends metric update recording for a call to method m.

type OrderedCode

type OrderedCode string

OrderedCode is an order preserving encoded value.

const Infinity OrderedCode = "\xFF"

Infinity is the greatest element of the set of all OrderedCodes. That is, for every OrderedCode x produced by an Encoder, Infinity > x.

type OrderedEncoder

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

OrderedEncoder serializes values in an order preserving fashion. When multiple values are serialized together, their lexicographic ordering is preserved. For example,

var e orderedcode.OrderedEncoder
e.WriteUint8(1)
e.WriteFloat32(2.0)
e.OrderedCode()

func (*OrderedEncoder) Encode

func (e *OrderedEncoder) Encode() OrderedCode

Encode returns the encoding.

func (*OrderedEncoder) Reset

func (e *OrderedEncoder) Reset()

Reset resets the encoder to be empty, but retains any previously used space for future serialization.

func (*OrderedEncoder) WriteFloat32

func (e *OrderedEncoder) WriteFloat32(f float32)

WriteFloat32 serializes a value of type float32.

func (*OrderedEncoder) WriteFloat64

func (e *OrderedEncoder) WriteFloat64(f float64)

WriteFloat64 serializes a value of type float64.

func (*OrderedEncoder) WriteInt

func (e *OrderedEncoder) WriteInt(x int)

WriteInt serializes a value of type int.

func (*OrderedEncoder) WriteInt16

func (e *OrderedEncoder) WriteInt16(x int16)

WriteInt16 serializes a value of type int16.

func (*OrderedEncoder) WriteInt32

func (e *OrderedEncoder) WriteInt32(x int32)

WriteInt32 serializes a value of type int32.

func (*OrderedEncoder) WriteInt64

func (e *OrderedEncoder) WriteInt64(x int64)

WriteInt64 serializes a value of type int64.

func (*OrderedEncoder) WriteInt8

func (e *OrderedEncoder) WriteInt8(x int8)

WriteInt8 serializes a value of type int8.

func (*OrderedEncoder) WriteString

func (e *OrderedEncoder) WriteString(s string)

WriteString serializes a value of type string.

func (*OrderedEncoder) WriteUint

func (e *OrderedEncoder) WriteUint(x uint)

WriteUint serializes a value of type uint.

func (*OrderedEncoder) WriteUint16

func (e *OrderedEncoder) WriteUint16(x uint16)

WriteUint16 serializes a value of type uint16.

func (*OrderedEncoder) WriteUint32

func (e *OrderedEncoder) WriteUint32(x uint32)

WriteUint32 serializes a value of type uint32.

func (*OrderedEncoder) WriteUint64

func (e *OrderedEncoder) WriteUint64(x uint64)

WriteUint64 serializes a value of type uint64.

func (*OrderedEncoder) WriteUint8

func (e *OrderedEncoder) WriteUint8(x uint8)

WriteUint8 serializes a value of type uint8.

type Registration

type Registration struct {
	Name      string       // full package-prefixed component name
	Iface     reflect.Type // interface type for the component
	Impl      reflect.Type // implementation type (struct)
	Routed    bool         // True if calls to this component should be routed
	Listeners []string     // the names of any weaver.Listeners
	NoRetry   []int        // indices of methods that should not be retried

	// Functions that return different types of stubs.
	LocalStubFn   func(impl any, caller string, tracer trace.Tracer) any
	ClientStubFn  func(stub Stub, caller string) any
	ServerStubFn  func(impl any, load func(key uint64, load float64)) Server
	ReflectStubFn func(func(method string, ctx context.Context, args []any, returns []any) error) any

	// RefData holds a string containing the result of MakeEdgeString(Name, Dst)
	// for all components named Dst used by this component.
	RefData string
}

Registration is the configuration needed to register a Service Weaver component.

func Find added in v0.22.0

func Find(name string) (*Registration, bool)

Find returns the registration of the named component.

func Registered

func Registered() []*Registration

Registered returns the components registered with Register.

type Server

type Server interface {
	// GetStubFn returns a handler function for the given method. For example,
	// if a Service Weaver component defined an Echo method, then GetStubFn("Echo")
	// would return a handler that deserializes the arguments, executes the
	// method, and serializes the results.
	//
	// TODO(mwhittaker): Rename GetHandler? This is returning a call.Handler.
	GetStubFn(method string) func(ctx context.Context, args []byte) ([]byte, error)
}

A Server allows a Service Weaver component in one process to receive and execute methods via RPC from a Service Weaver component in a different process. It is the dual of a Stub.

type Stub

type Stub interface {
	// Tracer returns a new tracer.
	//
	// TODO(mwhittaker): Move tracer out of stub? It doesn't really fit with
	// the abstraction?
	Tracer() trace.Tracer

	// Run executes the provided method with the provided serialized arguments.
	// At code generation time, an object's methods are deterministically
	// ordered. method is the index into this slice. args and results are the
	// serialized arguments and results, respectively. shardKey is the shard
	// key for routed components, and 0 otherwise.
	Run(ctx context.Context, method int, args []byte, shardKey uint64) (results []byte, err error)
}

A Stub allows a Service Weaver component in one process to invoke methods via RPC on a Service Weaver component in a different process.

type Version added in v0.10.0

type Version[_ any] string

Jump to

Keyboard shortcuts

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