Documentation
¶
Overview ¶
Package actorlayer provides the core types for a small transport-agnostic actor runtime.
The package defines the durable Envelope model, actor addresses, payload helpers, error classification, and retry helpers used by the higher-level dispatch, engine, and transport packages.
Package layout:
- actorlayer: envelope model, addresses, payload helpers, errors, retry
- dispatch: actor registration and address resolution
- engine: delivery execution, lane serialization, settlement hooks
- transport: dispatch, event, and drain contracts
- transport/memory: in-memory transport for tests, examples, and local use
Payload carries an explicit encoding plus raw bytes. Use MarshalPayload and UnmarshalPayload to encode and decode typed payloads. The default helpers use the built-in JSON codec unless a codec-aware helper is used explicitly. ReportTo is optional, but when present it must be a valid actor address.
Example (EndToEnd) ¶
package main
import (
"context"
"fmt"
"github.com/baldaworks/go-actorlayer"
"github.com/baldaworks/go-actorlayer/dispatch"
"github.com/baldaworks/go-actorlayer/engine"
"github.com/baldaworks/go-actorlayer/transport/memory"
)
type welcomePayload struct {
Name string `json:"name"`
}
type welcomeActor struct {
done chan struct{}
}
func (a welcomeActor) Address() string { return actorlayer.WildcardAddress("session") }
func (a welcomeActor) Handle(_ context.Context, env actorlayer.Envelope) error {
var payload welcomePayload
if err := actorlayer.UnmarshalPayload(env.Payload, &payload); err != nil {
return err
}
fmt.Printf("welcome %s\n", payload.Name)
close(a.done)
return nil
}
func main() {
bus := memory.New(1)
registry := dispatch.NewMemoryRegistry()
done := make(chan struct{})
_ = registry.Register(welcomeActor{done: done})
runtime, _ := engine.NewDispatchRuntime(engine.RuntimeConfig{
Registry: registry,
AddressOf: func(env engine.Envelope) (string, error) { return env.To.String() },
Retry: engine.RetryPolicy{
IsRetryable: actorlayer.IsRetryableError,
Backoff: actorlayer.RetryDelay,
},
})
ctx := context.Background()
runDone := make(chan error, 1)
go func() {
runDone <- runtime.Run(ctx, bus)
}()
payload, _ := actorlayer.MarshalPayload(welcomePayload{Name: "ada"})
_, _ = bus.Dispatch(ctx, actorlayer.Envelope{
ID: "env-1",
Namespace: "example.command",
Kind: "welcome",
From: actorlayer.SystemAddress("example"),
To: actorlayer.ActorAddress{Target: "session", Key: "demo"},
Payload: payload,
})
<-done
_ = bus.Drain(ctx)
<-runDone
}
Output: welcome ada
Index ¶
- Constants
- Variables
- func DecodeError(err error) error
- func DedupeKeyOrID(env Envelope) string
- func EncodeEnvelope(e Envelope) (string, error)
- func EncodeEnvelopeWithRegistry(e Envelope, reg CodecRegistry) (string, error)
- func ExternalDeliveryError(err error) error
- func IsRetryableError(err error) bool
- func PermanentError(err error) error
- func PolicyError(err error) error
- func RetryDelay(attempt int) time.Duration
- func RetryExhausted(attempt int, maxAttempts int) bool
- func TransientError(err error) error
- func UnmarshalPayload(p Payload, dst any) error
- func UnmarshalPayloadWithRegistry(reg CodecRegistry, p Payload, dst any) error
- func WildcardAddress(target string) string
- type ActorAddress
- type ActorError
- type Codec
- type CodecRegistry
- type Envelope
- type ErrorKind
- type Payload
Examples ¶
Constants ¶
const EncodingJSON = "json"
EncodingJSON identifies the built-in JSON payload codec.
Variables ¶
var ErrActorNotFound = errors.New("actor not found")
ErrActorNotFound reports that an actor address could not be resolved.
Functions ¶
func DedupeKeyOrID ¶
DedupeKeyOrID returns the explicit dedupe key, or the envelope ID when no dedupe key is set.
func EncodeEnvelope ¶
EncodeEnvelope validates and marshals an envelope using the default JSON-based wire projection.
func EncodeEnvelopeWithRegistry ¶
func EncodeEnvelopeWithRegistry(e Envelope, reg CodecRegistry) (string, error)
EncodeEnvelopeWithRegistry validates and marshals an envelope with the provided registry.
func ExternalDeliveryError ¶
ExternalDeliveryError marks err as retryable delivery infrastructure failure.
func IsRetryableError ¶
IsRetryableError reports whether err should be retried by default.
func PermanentError ¶
PermanentError marks err as non-retryable permanent failure.
func PolicyError ¶
PolicyError marks err as non-retryable policy failure.
func RetryDelay ¶
RetryDelay returns an exponential retry delay with bounded jitter.
func RetryExhausted ¶
RetryExhausted reports whether the current one-based attempt has reached the configured max attempts.
func UnmarshalPayload ¶
UnmarshalPayload unmarshals an actor payload into dst with the default registry.
func UnmarshalPayloadWithRegistry ¶
func UnmarshalPayloadWithRegistry(reg CodecRegistry, p Payload, dst any) error
UnmarshalPayloadWithRegistry unmarshals payload content using the provided registry.
func WildcardAddress ¶
WildcardAddress returns the normalized registry address for all keys in a target, such as "session:*".
Types ¶
type ActorAddress ¶
ActorAddress identifies an actor target and concrete key.
func SystemAddress ¶
func SystemAddress(key string) ActorAddress
SystemAddress returns an address in the reserved "system" target.
func (ActorAddress) String ¶
func (a ActorAddress) String() (string, error)
String returns the normalized full actor address.
type ActorError ¶
ActorError wraps an error with an actorlayer classification.
func (*ActorError) Error ¶
func (e *ActorError) Error() string
func (*ActorError) Unwrap ¶
func (e *ActorError) Unwrap() error
type Codec ¶
type Codec interface {
Encoding() string
Marshal(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
Validate(data []byte) error
}
Codec marshals and unmarshals actor payload values for one encoding.
var JSONCodec Codec = jsonCodec{}
JSONCodec is the default actorlayer codec and preserves current wire/storage compatibility.
type CodecRegistry ¶
CodecRegistry resolves codecs by encoding name.
func DefaultCodecRegistry ¶
func DefaultCodecRegistry() CodecRegistry
DefaultCodecRegistry returns the built-in codec registry.
func NewCodecRegistry ¶
func NewCodecRegistry(codecs ...Codec) CodecRegistry
NewCodecRegistry builds a registry from the provided codecs.
type Envelope ¶
type Envelope struct {
ID string
Namespace string
Kind string
From ActorAddress
To ActorAddress
CorrelationID string
CausationID string
Priority int
DedupeKey string
Attempt int
MaxAttempts int
NotBefore time.Time
ExpiresAt time.Time
Payload Payload
Meta map[string]string
ReportTo *ActorAddress
}
Envelope is the durable actor transport unit.
func DecodeEnvelope ¶
DecodeEnvelope unmarshals and validates an envelope JSON string.
func DecodeEnvelopeWithRegistry ¶
func DecodeEnvelopeWithRegistry(raw string, reg CodecRegistry) (Envelope, error)
DecodeEnvelopeWithRegistry unmarshals and validates an envelope string with the provided registry.
func (Envelope) Validate ¶
Validate verifies the envelope fields required by actorlayer runtimes and transports.
func (Envelope) ValidateWithRegistry ¶
func (e Envelope) ValidateWithRegistry(reg CodecRegistry) error
ValidateWithRegistry verifies the envelope with the provided registry.
type ErrorKind ¶
type ErrorKind string
ErrorKind classifies actor and delivery failures for retry decisions.
const ( // ErrorKindTransient marks a retryable temporary failure. ErrorKindTransient ErrorKind = "transient" // ErrorKindPolicy marks a non-retryable policy or authorization failure. ErrorKindPolicy ErrorKind = "policy" // ErrorKindPermanent marks a non-retryable permanent actor failure. ErrorKindPermanent ErrorKind = "permanent" // ErrorKindDecode marks malformed payload or envelope input. ErrorKindDecode ErrorKind = "decode" // ErrorKindExternalDelivery marks retryable infrastructure delivery failure. ErrorKindExternalDelivery ErrorKind = "external_delivery" )
func ClassifyError ¶
ClassifyError returns the actorlayer error kind for err.
Unclassified non-nil errors are treated as transient to preserve retry by default.
type Payload ¶
Payload is the format-neutral envelope body.
func MarshalPayload ¶
MarshalPayload marshals a typed actor payload with the default JSON codec.
func MarshalPayloadWithCodec ¶
MarshalPayloadWithCodec marshals a typed actor payload using the provided codec.
func NewPayload ¶
NewPayload validates and normalizes a payload value.
func (Payload) String ¶
String returns payload data as a string for text-based transports and legacy compatibility paths.
func (Payload) ValidateWithRegistry ¶
func (p Payload) ValidateWithRegistry(reg CodecRegistry) error
ValidateWithRegistry verifies the payload using the provided registry.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dispatch provides actor registration and address resolution helpers.
|
Package dispatch provides actor registration and address resolution helpers. |
|
Package engine provides transport-agnostic actor delivery execution.
|
Package engine provides transport-agnostic actor delivery execution. |
|
Package transport defines actorlayer transport interfaces.
|
Package transport defines actorlayer transport interfaces. |
|
memory
Package memory provides an in-memory actorlayer transport implementation.
|
Package memory provides an in-memory actorlayer transport implementation. |