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
- func CatchPanics(r interface{}) error
- func ComponentConfigValidator(path, cfg string) error
- func ExtractEdges(data []byte) [][2]string
- func MakeEdgeString(src, dst string) string
- func MakeListenersString(component string, listeners []string) string
- func Register(reg Registration)
- func RegisterSerializable[T AutoMarshal]()
- type AutoMarshal
- type CallEdge
- type ComponentListeners
- type Decoder
- func (d *Decoder) Bool() bool
- func (d *Decoder) Byte() byte
- func (d *Decoder) Bytes() []byte
- func (d *Decoder) Complex128() complex128
- func (d *Decoder) Complex64() complex64
- func (d *Decoder) DecodeBinaryUnmarshaler(value encoding.BinaryUnmarshaler)
- func (d *Decoder) DecodeProto(value proto.Message)
- func (d *Decoder) Empty() bool
- func (d *Decoder) Error() error
- func (d *Decoder) Float32() float32
- func (d *Decoder) Float64() float64
- func (d *Decoder) Int() int
- func (d *Decoder) Int16() int16
- func (d *Decoder) Int32() int32
- func (d *Decoder) Int64() int64
- func (d *Decoder) Int8() int8
- func (d *Decoder) Interface() any
- func (d *Decoder) Len() int
- func (d *Decoder) Read(n int) []byte
- func (d *Decoder) Rune() rune
- func (d *Decoder) String() string
- func (d *Decoder) Uint() uint
- func (d *Decoder) Uint16() uint16
- func (d *Decoder) Uint32() uint32
- func (d *Decoder) Uint64() uint64
- func (d *Decoder) Uint8() uint8
- type Encoder
- func (e *Encoder) Bool(arg bool)
- func (e *Encoder) Byte(arg byte)
- func (e *Encoder) Bytes(arg []byte)
- func (e *Encoder) Complex128(arg complex128)
- func (e *Encoder) Complex64(arg complex64)
- func (e *Encoder) Data() []byte
- func (e *Encoder) EncodeBinaryMarshaler(value encoding.BinaryMarshaler)
- func (e *Encoder) EncodeProto(value proto.Message)
- func (e *Encoder) Error(err error)
- func (e *Encoder) Float32(arg float32)
- func (e *Encoder) Float64(arg float64)
- func (e *Encoder) Grow(bytesNeeded int) []byte
- func (e *Encoder) Int(arg int)
- func (e *Encoder) Int16(arg int16)
- func (e *Encoder) Int32(arg int32)
- func (e *Encoder) Int64(arg int64)
- func (e *Encoder) Int8(arg int8)
- func (e *Encoder) Interface(value AutoMarshal)
- func (e *Encoder) Len(l int)
- func (e *Encoder) Reset(n int)
- func (e *Encoder) Rune(arg rune)
- func (e *Encoder) String(arg string)
- func (e *Encoder) Uint(arg uint)
- func (e *Encoder) Uint16(arg uint16)
- func (e *Encoder) Uint32(arg uint32)
- func (e *Encoder) Uint64(arg uint64)
- func (e *Encoder) Uint8(arg uint8)
- type Hasher
- func (h *Hasher) Sum64() uint64
- func (h *Hasher) WriteFloat32(v float32)
- func (h *Hasher) WriteFloat64(v float64)
- func (h *Hasher) WriteInt(v int)
- func (h *Hasher) WriteInt16(v int16)
- func (h *Hasher) WriteInt32(v int32)
- func (h *Hasher) WriteInt64(v int64)
- func (h *Hasher) WriteInt8(v int8)
- func (h *Hasher) WriteString(v string)
- func (h *Hasher) WriteUint(v uint)
- func (h *Hasher) WriteUint16(v uint16)
- func (h *Hasher) WriteUint32(v uint32)
- func (h *Hasher) WriteUint64(v uint64)
- func (h *Hasher) WriteUint8(v uint8)
- type LatestVersion
- type MethodCallHandle
- type MethodLabels
- type MethodMetrics
- type OrderedCode
- type OrderedEncoder
- func (e *OrderedEncoder) Encode() OrderedCode
- func (e *OrderedEncoder) Reset()
- func (e *OrderedEncoder) WriteFloat32(f float32)
- func (e *OrderedEncoder) WriteFloat64(f float64)
- func (e *OrderedEncoder) WriteInt(x int)
- func (e *OrderedEncoder) WriteInt16(x int16)
- func (e *OrderedEncoder) WriteInt32(x int32)
- func (e *OrderedEncoder) WriteInt64(x int64)
- func (e *OrderedEncoder) WriteInt8(x int8)
- func (e *OrderedEncoder) WriteString(s string)
- func (e *OrderedEncoder) WriteUint(x uint)
- func (e *OrderedEncoder) WriteUint16(x uint16)
- func (e *OrderedEncoder) WriteUint32(x uint32)
- func (e *OrderedEncoder) WriteUint64(x uint64)
- func (e *OrderedEncoder) WriteUint8(x uint8)
- type Registration
- type Server
- type Stub
- type Version
Constants ¶
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 ¶
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
ExtractEdges returns the edges corresponding to MakeEdgeString() results that occur in data.
func MakeEdgeString ¶ added in v0.9.0
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
MakeListenersString returns a string that should be emitted into generated code to represent the set of listeners associated with a given 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 ¶
AutoMarshal is the interface implemented by structs with weaver.AutoMarshal declarations.
type CallEdge ¶ added in v0.7.2
CallEdge records that fact that the Caller component uses the Callee component. Both types are types of the corresponding component interfaces.
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 ¶
NewDecoder instantiates a new Decoder for a given byte slice.
func (*Decoder) Complex128 ¶
func (d *Decoder) Complex128() complex128
Complex128 decodes a value of type complex128.
func (*Decoder) DecodeBinaryUnmarshaler ¶
func (d *Decoder) DecodeBinaryUnmarshaler(value encoding.BinaryUnmarshaler)
DecodeBinaryUnmarshaler deserializes the value from a byte slice using UnmarshalBinary.
func (*Decoder) DecodeProto ¶
DecodeProto deserializes the value from a byte slice using proto serialization.
func (*Decoder) Error ¶
Error decodes an error. We construct an instance of a special error value that provides Is and Unwrap support.
func (*Decoder) Interface ¶ added in v0.18.0
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 ¶
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 ¶
Read reads and returns n bytes from the decoder and advances the decode past the read bytes.
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 ¶
Bool encodes an arg of type bool. Serialize boolean values as an uint8 that encodes either 0 or 1.
func (*Encoder) Bytes ¶
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 ¶
Complex64 encodes an arg of type complex64. We encode the real and the imaginary parts one after the other.
func (*Encoder) EncodeBinaryMarshaler ¶
func (e *Encoder) EncodeBinaryMarshaler(value encoding.BinaryMarshaler)
EncodeBinaryMarshaler serializes value into a byte slice using its MarshalBinary method.
func (*Encoder) EncodeProto ¶
EncodeProto serializes value into a byte slice using proto serialization.
func (*Encoder) 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) Grow ¶
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 ¶
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) Interface ¶ added in v0.18.0
func (e *Encoder) Interface(value AutoMarshal)
Interface encodes value prefixed with its concrete type.
func (*Encoder) Len ¶
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 ¶
Reset resets the Encoder to use a buffer with a capacity of at least the provided size. All encoded data is lost.
func (*Encoder) String ¶
String encodes an arg of type string. For a string, we encode its length, followed by the serialized content.
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 ¶
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 ¶
WriteFloat32 adds a float32 to the hasher.
func (*Hasher) WriteFloat64 ¶
WriteFloat64 adds a float64 to the hasher.
func (*Hasher) WriteInt16 ¶
WriteInt16 adds a int16 to the hasher.
func (*Hasher) WriteInt32 ¶
WriteInt32 adds a int32 to the hasher.
func (*Hasher) WriteInt64 ¶
WriteInt64 adds a int64 to the hasher.
func (*Hasher) WriteString ¶
WriteString adds a string to the hasher.
func (*Hasher) WriteUint16 ¶
WriteUint16 adds a uint16 to the hasher.
func (*Hasher) WriteUint32 ¶
WriteUint32 adds a uint32 to the hasher.
func (*Hasher) WriteUint64 ¶
WriteUint64 adds a uint64 to the hasher.
func (*Hasher) WriteUint8 ¶
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 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 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.